Java BufferedImage写/读(不同的rgb值)

时间:2018-02-20 22:37:50

标签: java image rgb bufferedimage

我用这段代码写了一张图片:

BufferedImage newImage = new BufferedImage(width, height,
                BufferedImage.TYPE_3BYTE_BGR);
        index = 0;

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                int r = ciphered[index++];
                int g = ciphered[index++];
                int b = ciphered[index++];

                Color newColor = new Color(r, g, b);
                newImage.setRGB(j, i, newColor.getRGB());

            }
        }
        File ouptut = new File("/Users/newbie/Desktop/encrypted.jpg");
        ImageIO.write(newImage, "jpg", ouptut);

当我尝试读取图像(“encrypted.jpg”)时,我得到不同的RGB值。我用以下代码阅读了图像:

File input = new File("/Users/newbie/Desktop/encrypted.jpg");


        BufferedImage image = new BufferedImage(512, 512,
                BufferedImage.TYPE_INT_RGB);
        image = ImageIO.read(input);
int[] t = new int[width * height * 3];


for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                Color c = new Color(image.getRGB(j, i));

                int r = c.getRed();
                int g = c.getGreen();
                int b = c.getBlue();

                t[index++] = r;
                t[index++] = g;
                t[index++] = b;
            }
        }

我不明白我做错了什么。我只是从我插入的那些中得到不同的rgb值。

0 个答案:

没有答案