JAI tiff 图像转换在 Windows 7 上生成更大的文件大小
尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论