<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)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 久久精品国产亚洲香蕉| 免费无码黄动漫在线观看| 亚洲乱码国产一区网址| 亚洲国产成人精品青青草原| 免费观看一区二区三区| 日韩精品电影一区亚洲| 亚洲AV网一区二区三区| 久久精品免费一区二区喷潮 | 美女黄网站人色视频免费国产| 亚洲乱亚洲乱淫久久| 国产精品免费观看调教网| 青青草原亚洲视频| 一级人做人a爰免费视频| 免费在线观看一级毛片| 春暖花开亚洲性无区一区二区| 24小时日本在线www免费的| 精品亚洲国产成人| 欧美男同gv免费网站观看 | 亚洲国产高清在线一区二区三区| 亚洲精品天堂成人片AV在线播放| 成年女人色毛片免费看| 久久亚洲中文字幕无码| 久久久久亚洲?V成人无码| a在线视频免费观看| 亚洲国产精品综合一区在线| 最近最新的免费中文字幕| 国产精品亚洲а∨无码播放麻豆| 亚洲?V无码乱码国产精品| 国产综合免费精品久久久| 久久夜色精品国产噜噜噜亚洲AV| 免费H网站在线观看的| 国产尤物在线视精品在亚洲| 亚洲乱码精品久久久久..| 国产精品成人免费福利| 曰批全过程免费视频免费看| 久久精品国产亚洲麻豆| 国产又大又粗又硬又长免费 | 亚洲视频在线观看免费视频| 亚洲色精品VR一区区三区| 亚洲另类激情综合偷自拍图| 成年女人免费视频播放体验区 |