1

我有一个问题让我发疯了好几天。希望这里有人可以帮助我了解正在发生的事情。我正在尝试编写一个简单的 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。非常感谢所有阅读本文的人!

4

1 回答 1

2

这是ImageIO.

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

解决方案是不要编写带有 alpha 组件的图像来jpg格式化,或者使用非基于 alpha 的图像,例如TYPE_INT_RGB...

于 2013-09-16T04:33:38.750 回答