JAI tiff图像转换在Windows 7上产生更大的文件大小

时间:2011-04-12 14:58:18

标签: windows-7 size jai

尝试使用TIFF格式的JAI压缩图像。它在Windows XP上运行良好,但与Windows XP相比,它在Windows 7上生成的文件大10倍。

使用JAI 1.1和JRE 1.6_0_16

可能是什么问题?感谢您的帮助。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File image = new File("abc.tiff");
        File tempFile = new File("compressed.tiff");

        try {
            BufferedImage bi = ImageIO.read(image);
            byte[] tiffArray = toTiff(bi, tempFile,"packBits" );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static byte[] toTiff(BufferedImage bi, File tempFile, String compType) {
        byte[] bImg = null;
        try{
            tempFile.delete();
            String format = "TIF";
            Iterator writers = ImageIO.getImageWritersByFormatName(format);
            if(writers == null || !writers.hasNext()) {
                throw new IllegalArgumentException("Unsupported format (" + format + ")");
            }
            ImageWriter writer = (ImageWriter)writers.next();
            IIOImage iioImg = new IIOImage(bi, null, null);
            TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.ENGLISH);
            writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            writeParam.setCompressionType(compType);
            ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);
            writer.setOutput(ios);
            writer.write(null, iioImg, writeParam);
            ios.close();
            writer.dispose();
            bImg = readImage(tempFile);
        }catch(Exception e){
            e.printStackTrace();
        }
        return bImg;
    }
    public static byte[] readImage(File f) throws Exception {
        byte[] bImg = null;
        try {
            long fLength = f.length();
            if(fLength > Integer.MAX_VALUE){
                throw new RuntimeException("File is too large to upload....!");
            }
            bImg = new byte[(int)fLength];
            FileInputStream fin = new FileInputStream(f);
            fin.read(bImg);
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return bImg;
    }
}

0 个答案:

没有答案