【问题标题】:Not able to generate same output image from source input无法从源输入生成相同的输出图像
【发布时间】:2016-06-09 16:52:43
【问题描述】:

我正在将 .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);

【问题讨论】:

    标签: java image awt


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多