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

发布于 2024-11-01 03:38:54 字数 2739 浏览 0 评论 0原文

尝试使用 JAI 和 TIFF 格式压缩图像。它在 Windows XP 上运行良好,但在 Windows 7 上生成的文件大小是 Windows XP 的 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;
    }
}

Trying to compress the image using JAI with TIFF format. It works fine on Windows XP, But it produces 10 times bigger size file on Windows 7 compared to Windows XP.

Using JAI 1.1 and JRE 1.6_0_16

What might be the issue? Appreciate your assistance.

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文