压缩JPG使图像变为绿色

当我尝试压缩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());
    }
}
    
已邀请:
根据经验,我知道绿色是新格式YUV存储器的颜色(特别是YV12)。所以我的猜测是一些步骤失败,你得到亮度信息,但色度变得拙劣。在它到达Cr飞机之前,我觉得它失败了。 无论如何,祝你好运,这是一个艰难的。你的代码看起来很奇怪 - 顶部的奇怪的png特定代码是什么? AFAIK,如果您使用的是.NET,您几乎可以将任何已注册的图像格式视为没有任何有趣工作的图像。     
我也有同样的问题。在我的测试服务器上运行java 7 oracle并且工作正常。在我的生产服务器上运行openJDK 1.7,压缩图像变为绿色...这似乎是一些JAVA版本中的错误。     

要回复问题请先登录注册