无法从源输入生成相同的输出图像

Not able to generate same output image from source input

我正在读取一个 .jpg 文件作为整数数组(源)并尝试从相同的数据生成一个新图像,但是代码生成了一个黑色图像。但它应该生成重复图像作为源。

        String srcName = "input.jpg";
        File srcFile = new File(srcName);
        BufferedImage image = ImageIO.read(srcFile);
        System.out.println("Source image: " + srcName);


        int w = image.getWidth();
        int h = image.getHeight();
        int[] src = image.getRGB(0, 0, w, h, null, 0, w);

        System.out.println("Array size is " + src.length);


        BufferedImage dstImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        // generating destination image with same source array
        dstImage.setRGB(0, 0, w, h, src, 0, w);

        String dstName = "output.jpg";
        File dstFile = new File(dstName);
        ImageIO.write(dstImage, "jpg", dstFile);
        System.out.println("Output image: " + dstName);

您需要为两张图片使用相同的颜色编码类型。 您的输入图像可能未编码为 BufferedImage.TYPE_INT_ARGB.

这为我的测试图像修复了它,其类型为 BufferedImage.TYPE_3BYTE_BGR:

BufferedImage dstImage = new BufferedImage(w, h, image.getType());

但是,我不希望新写入的图像与输入的图像完全相同。我宁愿 ImageIO 在将图像数据编码为 jpg 时引入一些伪像。