【问题标题】:How to Read JPEG image into BufferedImage object using Java如何使用 Java 将 JPEG 图像读入 BufferedImage 对象
【发布时间】:2016-05-19 17:24:05
【问题描述】:

这里不是重复问题,因为我在谷歌和StackOverflow上找了很久,还是找不到解决办法。

我有这两张图片:

这是来自同一网站的两张图片,具有相同的前缀和相同的格式。唯一的区别是大小:第一个较大,而第二个较小。

我将这两个图像下载到本地文件夹并使用 Java 将它们读入 BufferedImage 对象。但是,当我将 BufferedImages 输出到本地文件时,我发现第一个图像几乎是红色的,而第二个是正常的(与原始图像相同)。我的代码有什么问题?

byte[] rawData = getRawBytesFromFile(imageFilePath); // some code to read raw bytes from image file
ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
BufferedImage img = ImageIO.read(iis);
FileOutputStream fos = new FileOutputStream(outputImagePath, false);
ImageIO.write(img, "JPEG", fos);
fos.flush();
fos.close();

PS:我用 GIMP 打开第一张图片,发现颜色模式是 'sRGB',没有 alpha 或其他东西。

【问题讨论】:

  • emm,你选了一张漂亮的照片,我喜欢你的味道:)我不知道颜色有什么问题,但试试这个BufferedImage img = ImageIO.read(new File("path/to/image"));
  • 其实我试过了,但是失败了……

标签: java image jpeg


【解决方案1】:

这显然是一个已知的错误,我看到了几个建议(this 是其中之一)建议改用Toolkit#createImage,这显然忽略了颜色模型。

我对此进行了测试,它似乎工作正常。

public class TestImageIO01 {

    public static void main(String[] args) {
        try {
            Image in = Toolkit.getDefaultToolkit().createImage("C:\\hold\\test\\13652375852388.jpg");

            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(in)), "Yeah", JOptionPane.INFORMATION_MESSAGE);

            BufferedImage out = new BufferedImage(in.getWidth(null), in.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = out.createGraphics();
            g2d.drawImage(in, 0, 0, null);
            g2d.dispose();

            ImageIO.write(out, "jpg", new File("C:\\hold\\test\\Test01.jpg"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

nb- 我使用JOptionPane 来验证传入的图像。使用ImageIO 时,它带有红色调,使用Toolkit 时,它看起来很好。

更新

还有一个explantation

【讨论】:

    【解决方案2】:

    我在netbeans中检查了您的代码并遇到了您的问题,然后我将代码更改为以下没有问题:

        public class Test {
    
        public static void main(String args[]) throws IOException {
    
    
            byte[] rawData = getRawBytesFromFile(imageFilePath); // some code to read raw bytes from image file
    //        ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
    //        BufferedImage img = ImageIO.read(iis);
            FileOutputStream fos = new FileOutputStream(outputImagePath, false);
            fos.write(rawData);
    //        ImageIO.write(img, "JPEG", fos);
            fos.flush();
            fos.close();
        }
    
        private static byte[] getRawBytesFromFile(String path) throws FileNotFoundException, IOException {
    
            byte[] image;
            File file = new File(path);
            image = new byte[(int)file.length()];
    
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(image);
    
            return image;
        }
    }
    

    请检查并告诉我结果;)

    祝你好运

    【讨论】:

    • 感谢您的回答,在复制图像文件时,它确实有效。但是,您知道,我需要将图像文件读入 BufferedImage 对象以进行进一步处理(如旋转或缩放),所以这不是这种情况的解决方案。
    • 我使用 photoshop 将图像格式更改为 PNG,然后将 png 文件更改为 JPEG。使用新的 jpg 文件一切正常。 This is 新图片。
    • 是的,就像@MadProgrammer 所说的,它是ImageIO 的一个错误,它无法读取某种类型的JPEG。当您使用 PS 将其重新保存为标准 JPEG 时,问题就解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多