我想使用Java压缩jpeg文件。我是这样做的:
BufferedImage
好的,这看起来很简单,但我发现ICC颜色配置文件和EXIF信息在新文件中消失了,图像的DPI从240降到72.它看起来与原始图像不同。我在OS X中使用像预览这样的工具。它可以完美地改变图像的质量而不会影响其他信息。
我可以用Java完成吗?至少保留ICC颜色配置文件并让图像颜色看起来与原始照片相同?
答案 0 :(得分:0)
最后找到一种方法来做到这一点。
使用javax.imageio.IIOImage。它可以由JpegImageReader重写。
但是它有一个错误,这个错误持续了6年。 :(
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4924909
幸运的是,如果你做了一些黑客攻击(伪造JFIF部分),它就可以了。 :d
所以,这个问题可以这样解决:
好吧,它似乎没问题(保存每个信息),除了一件事,输出图像比原始图像更亮(或者更确切地说是苍白)(当我使用OS X的预览时不会发生压缩照片,所以问题必须在我的代码或java中,或者我的错误用法:()。
所以,我在java中压缩jpeg的问题还没有解决。
有什么建议吗?
答案 1 :(得分:0)
/**
* @param inputFilenameWithPath : binary filepath
* @param outputFilepath : output image path
* @param start : from where the image start in binary file
* @param len : length of the image
* @throws ImageAccessException
*/
public void extractImageFromBinaryFile(String inputFilenameWithPath, String outputFilepath, int start, int len) throws ImageAccessException
{
try
{
File file = new File(inputFilenameWithPath);
FileImageInputStream iis = new FileImageInputStream(file);
// Added
byte[] b = new byte[start];
iis.read(b, 0, start);
byte[] fb = new byte[]{};
iis.read(fb);
IIOByteBuffer iiob = new IIOByteBuffer(fb, start, len);
iis.readBytes(iiob, len);
OutputStream os = new FileOutputStream(outputFilepath);
os.write(iiob.getData());
iis.close();
os.close();
}
catch (IOException ioe)
{`enter code here`
throw new ImageAccessException("Image File read/write error");
}
}