<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 閱讀(2204) 評論(0)  編輯  收藏

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


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 国内精品99亚洲免费高清| 国产高清免费在线| 亚洲成A人片在线观看无码不卡 | 中文字幕乱码亚洲无线三区| 国产91免费在线观看| 亚洲AV综合色区无码二区爱AV| 成人免费大片免费观看网站| 亚洲免费网站在线观看| aa级一级天堂片免费观看| 亚洲人成电影网站免费| 四虎免费久久影院| 国产免费A∨在线播放| 亚洲av无码专区在线播放| 亚洲黄色片免费看| 亚洲综合无码无在线观看| 免费看国产一级片| 国产一级黄片儿免费看| 91亚洲精品视频| 国内一级一级毛片a免费| 免费无码国产在线观国内自拍中文字幕 | 一级做α爱过程免费视频| 国产亚洲精品a在线无码| 亚洲免费闲人蜜桃| 国产精品亚洲一区二区三区久久 | 女同免费毛片在线播放| 99人中文字幕亚洲区| 在线看片人成视频免费无遮挡| 小说区亚洲自拍另类| 亚洲精品无码不卡在线播HE| 91免费在线播放| 免费一区二区三区在线视频 | 亚洲AV无码AV男人的天堂| 9久9久女女免费精品视频在线观看| 最新亚洲人成无码网www电影| 亚洲午夜久久久久久久久久| 日韩中文字幕精品免费一区| 日韩免费在线中文字幕| 亚洲色图校园春色| 亚洲精品无码永久在线观看| 国产男女爽爽爽爽爽免费视频| 特黄aa级毛片免费视频播放|