<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) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類(lèi)

    文章檔案

    相冊(cè)

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 曰批全过程免费视频网址 | 亚洲精品午夜无码专区| 另类专区另类专区亚洲| 四虎永久免费观看| jizz在线免费播放| 亚洲中文字幕无码不卡电影| a毛片免费观看完整| 亚洲嫩草影院久久精品| 6080午夜一级毛片免费看| 亚洲精品视频免费看| 日韩欧美一区二区三区免费观看| 亚洲精品免费网站| 国产jizzjizz免费看jizz| 羞羞视频在线观看免费| 亚洲av伊人久久综合密臀性色| 狼群影院在线观看免费观看直播 | 亚洲AV日韩AV一区二区三曲| 国产一区二区三区免费在线观看| 国产福利在线观看永久免费| 亚洲第一中文字幕| 免费看美女让人桶尿口| 国产精品成人啪精品视频免费 | 亚洲国产视频久久| 国产乱子伦精品免费女 | 99久久综合国产精品免费| 久久亚洲精品无码gv| 亚洲乱码无码永久不卡在线| 3344永久在线观看视频免费首页| 亚洲av无码偷拍在线观看| 久久国产成人亚洲精品影院| 1000部无遮挡拍拍拍免费视频观看 | 久久国产精品亚洲一区二区| 免费可以看黄的视频s色| 国产91成人精品亚洲精品| 亚洲色图在线观看| 国产亚洲福利一区二区免费看| 免费福利电影在线观看| 亚洲av无码成人精品区一本二本| 亚洲国产日韩在线视频| 巨胸喷奶水视频www网免费| 两个人看的www免费|