Java ImageIO.write是着色灰度图像

时间:2013-09-16 03:53:39

标签: java image jpeg javax.imageio

我遇到的问题让我疯狂了几天。希望有人在这里可以帮助我了解正在发生的事情。我正在尝试编写一个简单的Java程序,它将获取JPEG目录,将它们转换为灰度,并将它们保存到同一文件夹中。

我的步骤是将每个像素的红色,绿色和蓝色分量设置为该像素的亮度值。代码运行良好,似乎做我想要的。如果我在JFrame中查看完成的图像,它会显示黑色和白色。但是,当我保存图像时(使用ImageIO.write()),由于某种原因,它变得着色并且看起来很红。我想发布图片,但我想我的名声不够好......

由于我无法放置图像,我会尽力解释它。这就是我所知道的:

  • 如果我使用Java程序查看新创建的图像,它会显示为我想要的黑白。
  • 如果我保存图像并尝试使用外部程序查看它,它根本不会显示为黑白,只是看起来像原始图像的淡化版本。
  • 如果我使用Java程序打开同一个保存的图像(应该是黑白但不是),它确实显示为黑白。
  • 如果我将文件另存为png,一切正常。

如果有人愿意,我会使用相关代码:

    import java.io.*;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.*;

    public class ImageEZ {
        public static void displayImage(BufferedImage img) {
            class ImageFrame extends JFrame {
                ImageFrame(BufferedImage img) {
                    super();
                    class ImagePanel extends JPanel {
                        BufferedImage image;
                        ImagePanel(BufferedImage image) {
                           this.image = ImageEZ.duplicate(image);
                        }
                        protected void paintComponent(Graphics g) {
                            super.paintComponent(g);
                            g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
                        }
                    }
                    ImagePanel panel = new ImagePanel(img);
                    add(panel);
                }
            }
            JFrame frame = new ImageFrame(img);
            frame.setSize(img.getWidth(), img.getHeight());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    public static BufferedImage duplicate(BufferedImage img) {
        BufferedImage dup = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        dup.setRGB(0, 0, img.getWidth(), img.getHeight(), ImageEZ.getRGB(img), 0, img.getWidth());
        return dup;
    }

    public static int[] getRedArray(BufferedImage img) {
        int[] tArray = ImageEZ.getRGB(img);
        for (int i = 0; i < tArray.length; i++) {
            tArray[i] = tArray[i] << 8;
            tArray[i] = tArray[i] >>> 24;
        }
        return tArray;
    }
    public static int[] getRedArray(int[] tArray) {
        int[] nArray = new int[tArray.length];
        for (int i = 0; i < tArray.length; i++) {
            nArray[i] = tArray[i] << 8;
            nArray[i] = nArray[i] >>> 24;
        }
        return nArray;
    }
    public static int[] getGreenArray(BufferedImage img) {
        int[] tArray = ImageEZ.getRGB(img);
        for (int i = 0; i < tArray.length; i++) {
            tArray[i] = tArray[i] << 16;
            tArray[i] = tArray[i] >>> 24;
        }
        return tArray;
    }
    public static int[] getGreenArray(int[] tArray) {
        int[] nArray = new int[tArray.length];
        for (int i = 0; i < tArray.length; i++) {
            nArray[i] = tArray[i] << 16;
            nArray[i] = nArray[i] >>> 24;
        }
        return nArray;
    }
    public static int[] getBlueArray(BufferedImage img) {
        int[] tArray = ImageEZ.getRGB(img);
        for (int i = 0; i < tArray.length; i++) {
            tArray[i] = tArray[i] << 24;
            tArray[i] = tArray[i] >>> 24;
        }
        return tArray;
    }
    public static int[] getBlueArray(int[] tArray) {
        int[] nArray = new int[tArray.length];
        for (int i = 0; i < tArray.length; i++) {
            nArray[i] = tArray[i] << 24;
            nArray[i] = nArray[i] >>> 24;
        }
        return nArray;
    }

    public static int[] YBRtoRGB(int[] ybr) {
        int[] y = getRedArray(ybr);
        int[] r = getBlueArray(ybr);
        int[] b = getGreenArray(ybr);

        int[] red = new int[y.length];
        int[] green = new int[y.length];
        int[] blue = new int[y.length];

        for (int i = 0; i < red.length; i++) {
            red[i] = (int) (y[i] + 1.402*r[i]);
            green[i] = (int) (y[i] + -.344*b[i] + -.714*r[i]);
            blue[i] = (int) (y[i] + 1.772*b[i]);
        }

        int[] RGB = new int[red.length];
        for (int i = 0; i < red.length; i++) {
            RGB[i] = red[i] << 16 | green[i] << 8 | blue[i] | 255 << 24;
        }
        return RGB;
    }

    public static int[] getLumArray(BufferedImage img) {
        int[] red = getRedArray(img);  //Returns an array of the red values of the pixels
        int[] green = getGreenArray(img);
        int[] blue = getBlueArray(img);

        int[] Y = new int[red.length];

        for (int i = 0; i < red.length; i++) {
            Y[i] = (int) (.299*red[i] + .587*green[i] + .114*blue[i]);
        }

        return Y;
    }

  //    Converts an image to greyscale using the luminance of each pixel
    public static BufferedImage deSaturate(BufferedImage original) {
        BufferedImage deSaturated = new BufferedImage(original.getWidth(),
                                                      original.getHeight(),
                                                      BufferedImage.TYPE_INT_ARGB);

        int[] Y = ImageEZ.getLumArray(original);  //Returns an array of the luminances
        for (int i = 0; i < Y.length; i++) {
            Y[i] = 255 << 24 | Y[i] << 16;
        }

        int[] rgb = ImageEZ.YBRtoRGB(Y);  //Converts the YCbCr colorspace to RGB 
        deSaturated.setRGB(0, 0, original.getWidth(), original.getHeight(),
                           rgb, 0, original.getWidth());
        return deSaturated;
    }

  //    Takes a folder of JPEGs and converts them to Greyscale
    public static void main(String[] args) throws Exception {
        File root = new File(args[0]);
        File[] list = root.listFiles();

        for (int i = 0; i < list.length; i++) {
            BufferedImage a = ImageEZ.deSaturate(ImageIO.read(list[i]));
            displayImage(a);  //Displays the converted images.
            boolean v = ImageIO.write(a, "jpg", new File(list[i].getParent() + "\\" + i + ".jpg"));
        }
  //        Displays the first newly saved image
        displayImage(ImageIO.read(new File(list[0].getParent() + "\\" + 0 + ".png")));
    }
}

我只想强调,这不是关于将图像变成黑白的替代方法的问题。我真正想知道的是为什么它可以作为一个png而不是作为一个jpg。非常感谢所有阅读此内容的人!

1 个答案:

答案 0 :(得分:2)

这是ImageIO的已知问题。

当保存/加载为jpeg时,API不知道如何处理alpha组件(据我所知)。

解决方案是不要将带有alpha分量的图像写入jpg格式,或者使用非基于alpha的图像,例如TYPE_INT_RGB而不是......