When I try to compress the a jpg image, most of the time it work perfectly, however some jpg image turn green after the compression. Here is my code
当我尝试压缩jpg图像时,大部分时间它完美地工作,但是一些jpg图像在压缩后变为绿色。这是我的代码
public void compressImage(String filename, String fileExtension) {
BufferedImage img = null;
try {
File file = new File(filename);
img = ImageIO.read(file);
if (fileExtension.toLowerCase().equals(".png") || fileExtension.toLowerCase().equals(".gif")) {
//Since there might be transparent pixel, if I dont do this,
//the image will be all black.
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int rgb = img.getRGB(x, y);
int alpha = (rgb >> 24) & 0xff;
if (alpha != 255) {
img.setRGB(x, y, -1); //set white
}
}
}
}
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
//Then, choose the first image writer available
ImageWriter writer = (ImageWriter) iter.next();
//instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
//Set the compression quality
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.8f);
//delete the file. If I dont the file size will stay the same
file.delete();
ImageOutputStream output = ImageIO.createImageOutputStream(new File(filename));
writer.setOutput(output);
IIOImage image = new IIOImage(img, null, null);
writer.write(null, image, iwp);
writer.dispose();
} catch (IOException ioe) {
logger.log(Level.SEVERE, ioe.getMessage());
}
}
2 个解决方案
#1
0
From experience, I know that green is the color of freshly formatted YUV memory (YV12, in particular). So my guess is some step is failing, and you get luma information but the chroma gets botched. Looks to me like it's failing before it gets to the Cr plane.
根据经验,我知道绿色是新格式YUV存储器的颜色(特别是YV12)。所以我的猜测是一些步骤失败,你得到亮度信息,但色度变得拙劣。在它到达Cr飞机之前,我觉得它已经失败了。
Anyway, good luck, that's a tough one. Your code looks strange though--what's with the weird png specific code at the top? AFAIK, if you're using .NET you can pretty much treat any registered image format just as though it's an image without any funny work.
无论如何,祝你好运,这是一个艰难的。你的代码看起来很奇怪 - 顶部的奇怪的png特定代码是什么? AFAIK,如果你使用.NET,你几乎可以对任何注册的图像格式进行处理,就像它是一个没有任何有趣工作的图像一样。
#2
0
I have the same problem. In my test server run java 7 oracle and work fine. In my production server run openJDK 1.7, and compress images turn green...It´s seems bug in some JAVA versions.
我也有同样的问题。在我的测试服务器上运行java 7 oracle并且工作正常。在我的生产服务器上运行openJDK 1.7,压缩图像变为绿色...这似乎是一些JAVA版本中的bug。
#1
0
From experience, I know that green is the color of freshly formatted YUV memory (YV12, in particular). So my guess is some step is failing, and you get luma information but the chroma gets botched. Looks to me like it's failing before it gets to the Cr plane.
根据经验,我知道绿色是新格式YUV存储器的颜色(特别是YV12)。所以我的猜测是一些步骤失败,你得到亮度信息,但色度变得拙劣。在它到达Cr飞机之前,我觉得它已经失败了。
Anyway, good luck, that's a tough one. Your code looks strange though--what's with the weird png specific code at the top? AFAIK, if you're using .NET you can pretty much treat any registered image format just as though it's an image without any funny work.
无论如何,祝你好运,这是一个艰难的。你的代码看起来很奇怪 - 顶部的奇怪的png特定代码是什么? AFAIK,如果你使用.NET,你几乎可以对任何注册的图像格式进行处理,就像它是一个没有任何有趣工作的图像一样。
#2
0
I have the same problem. In my test server run java 7 oracle and work fine. In my production server run openJDK 1.7, and compress images turn green...It´s seems bug in some JAVA versions.
我也有同样的问题。在我的测试服务器上运行java 7 oracle并且工作正常。在我的生产服务器上运行openJDK 1.7,压缩图像变为绿色...这似乎是一些JAVA版本中的bug。