<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 25,  trackbacks - 0

    The project requirements: Should convert TIF into JPG with JAVA, But seems only IMAGEIO can't support TIF.
    So should use JAI, Java Advantage Image jars.

    JAI api download page: http://download.java.net/media/jai/builds/release/1_1_3/

    J
    AI API document page: http://download.oracle.com/docs/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/index.html



    C
    ode as below:
    package com.ebay.test1;
    import java.awt.image.RenderedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.imageio.ImageIO;
    import javax.media.jai.JAI;
    import javax.media.jai.RenderedOp;
    import org.junit.Test;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageDecoder;
    import com.sun.media.jai.codec.ImageEncoder;
    import com.sun.media.jai.codec.JPEGEncodeParam;
    public class Snippet {
    @Test
    public void testTif2JPG2(){
    try{
    String filename =  "F:\\Downloads\\CCITT_2.TIF";
    String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
    RenderedOp source = JAI.create("fileload", filename);
    FileOutputStream stream = null;
    stream = new FileOutputStream(jpegFileName );
    com.sun.media.jai.codec.JPEGEncodeParam JPEGparam = new
    com.sun.media.jai.codec.JPEGEncodeParam();
    ImageEncoder encoder =
    ImageCodec.createImageEncoder("jpeg",stream,JPEGparam);
    encoder.encode(source);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    @Test
    public void testTif2JPG3(){
    try{
    String filename =  "F:\\Downloads\\CCITT_2.TIF";
    String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
    FileOutputStream stream = null;
    stream = new FileOutputStream(jpegFileName );
    byte[] b = getBytesFromFile(new File(filename));
    InputStream bais = new ByteArrayInputStream(b);
    ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", bais, null);
                RenderedImage ri = decoder.decodeAsRenderedImage();
    // RenderedOp source = JAI.create("TIFF", bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JPEGEncodeParam JPEGparam = new JPEGEncodeParam();
    JPEGparam.setQuality(Float.MIN_VALUE);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg",baos,JPEGparam);
    encoder.encode(ri);
    byte[] byteArray = baos.toByteArray();
    FileOutputStream fos = new FileOutputStream(new File(jpegFileName));
    fos.write(byteArray);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
     @Test
    public void testTif2JPG4(){
    try{
    String filename =  "F:\\Downloads\\CCITT_2.TIF";
    String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
    FileOutputStream stream = null;
    stream = new FileOutputStream(jpegFileName );
    byte[] b = getBytesFromFile(new File(filename));
    InputStream bais = new ByteArrayInputStream(b);
                ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", bais, null);
                RenderedImage ri = decoder.decodeAsRenderedImage();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ImageIO.write(ri, "JPEG", outputStream);
                stream.write(outputStream.toByteArray());
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    // Returns the contents of the file in a byte array.
        public static byte[] getBytesFromFile(File file) throws IOException {
            InputStream is = new FileInputStream(file);
        
            // Get the size of the file
            long length = file.length();
        
            // You cannot create an array using a long type.
            // It needs to be an int type.
            // Before converting to an int type, check
            // to ensure that file is not larger than Integer.MAX_VALUE.
            if (length > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("File is too big, can't support.");
            }
        
            // Create the byte array to hold the data
            byte[] bytes = new byte[(int)length];
        
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
        
            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "+file.getName());
            }
        
            // Close the input stream and return bytes
            is.close();
            return bytes;
        }
    }
    posted on 2011-05-27 22:39 Daniel 閱讀(2215) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 亚洲欧洲久久久精品| 女人隐私秘视频黄www免费| 国内精品免费麻豆网站91麻豆| 337p日本欧洲亚洲大胆裸体艺术| 国产成人精品日本亚洲语音| 日韩激情无码免费毛片| 亚洲字幕AV一区二区三区四区| 1024免费福利永久观看网站| 久久亚洲精品AB无码播放| 无码精品人妻一区二区三区免费看 | 亚洲国产一成久久精品国产成人综合| 亚洲成a人片在线不卡一二三区| 四虎影院免费在线播放| 亚洲另类自拍丝袜第五页| 亚洲国产精品嫩草影院在线观看 | 亚洲av片劲爆在线观看| 免费视频成人片在线观看| 亚洲精品在线网站| 国产免费av片在线看| 亚洲日韩在线中文字幕综合| 亚洲精品国产高清嫩草影院| 男女一进一出抽搐免费视频| 亚洲不卡中文字幕无码| 国产免费一区二区三区| 亚洲av综合av一区二区三区| 亚洲精品网站在线观看不卡无广告 | 亚洲国产综合精品中文字幕 | 亚洲AV无码片一区二区三区 | 免费人成视频在线观看免费| 国产亚洲人成网站在线观看| 久久中文字幕免费视频| 久久综合久久综合亚洲| 国产又粗又猛又爽又黄的免费视频 | 久草免费福利视频| 亚洲国产精品综合久久20| 国产国产人免费视频成69大陆 | 亚洲乱码一区av春药高潮| 国产一级淫片免费播放电影| 99麻豆久久久国产精品免费 | 国产精品福利在线观看免费不卡| 亚洲天堂在线播放|