[英]How can I compress jpeg images in Java without losing any metadata in that image?
我想使用Java压缩jpeg文件。 我是这样做的:
BufferedImage
好吧,这看起来很简单,但我发现ICC颜色配置文件和EXIF信息在新文件中消失了,图像的DPI从240降到72.它看起来与原始图像不同。 我在OS X中使用像预览这样的工具。它可以完美地改变图像的质量而不会影响其他信息。
我可以用Java完成吗? 至少保留ICC颜色配置文件并让图像颜色看起来与原始照片相同?
/**
* @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");
}
}
最后找到一种方法来做到这一点。
使用javax.imageio.IIOImage。 它可以由JpegImageReader重写。
但它有一个错误,一个错误持续了6年。 :(
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4924909
幸运的是,如果你做了一些黑客攻击(伪造JFIF部分),它就可以了。 :d
所以,这个问题可以这样解决:
好吧,它似乎没问题(保存每个信息),除了一件事,输出图像比原始图像更亮(或者更确切地说是苍白)(当我使用OS X预览压缩照片时不会发生这种情况,所以问题必须在我的代码或java,或我的错误用法:()。
所以,我在java中压缩jpeg的问题还没有解决。
有什么建议么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.