使用 Java (ImageIO.write()) 将 PNG 转换为 JPG 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:09:24  来源:igfitidea点击:

Problem converting PNG to JPG using Java (ImageIO.write())

javajavax.imageio

提问by Adam Stokar

I am using ImageIO.write() to convert PNG files to JPG. For some reason, my result image has a pink layer over it. I have searched far and wide for a solution but haven't found any. The code works for all other types of images except PNG.

我正在使用 ImageIO.write() 将 PNG 文件转换为 JPG。出于某种原因,我的结果图像上有一个粉红色层。我已经广泛搜索了解决方案,但没有找到任何解决方案。该代码适用于除 PNG 之外的所有其他类型的图像。

回答by Adam Stokar

Quick reading of other SO answers tagged ImageIO led to this.

快速阅读标记为 ImageIO 的其他 SO 答案导致了这一点

The root cause can be a buggy reader. The proposed workaround is using different reader package.

根本原因可能是错误的阅读器。建议的解决方法是使用不同的阅读器包。

EditAbove link is broken, but thisappears to be it.

编辑上面的链接被打破,但是似乎是它。

EditThe above links are broken, here it is on archive.org.

编辑上面的链接已损坏,这里是 archive.org

回答by Brad Willard

I'm not sure how the other code snippet works given buffer is not used after it's created. I've found this pink problem to be jvm version specific.

我不确定其他代码片段是如何工作的,因为缓冲区在创建后没有被使用。我发现这个粉红色问题是特定于 jvm 版本的。

The easiest solution I've found is to do this.

我找到的最简单的解决方案是这样做。

BufferedImage image = null;
BufferedImage imageRGB = null;

// imageBytes is some png file you read
image = ImageIO.read(new ByteArrayInputStream(imageBytes));

// Attempt at PNG read fix
imageRGB = new BufferedImage(image.getWidth(),
    image.getHeight(), BufferedImage.TYPE_INT_RGB);

// write data into an RGB buffered image, no transparency
imageRGB.setData(image.getData());

// return the RGB buffered image, or do ImageIO.write( ... );
return imageRGB; // fixed for jpeg

回答by Lakshmi Priya.K

I too had the same problem, but if i write it in png format then it gets solved.

我也有同样的问题,但如果我以 png 格式编写它,那么它就解决了。

Something like this,

像这样的东西,

ImageIO.write(resizedImageBuffer, "png", baos);

回答by karakuricoder

I found this linkwhich has some code that might be of use. I tried your code with a few of my images but I couldn't reproduce the issue. I tried the last answer by devyn_a and it didn't break anything. Here's your code modified with devyn_a's solution.

我发现这个链接有一些可能有用的代码。我用我的一些图像尝试了你的代码,但我无法重现这个问题。我尝试了 devyn_a 的最后一个答案,但没有破坏任何东西。这是使用 devyn_a 的解决方案修改的代码。

String url = "file:///d:/teststuff/IMG_0393.JPG";
String to = "d:/teststuff/out.jpg";
BufferedImage oldImage = ImageIO.read(new URL(url));
BufferedImage buffer = new BufferedImage (oldImage.getWidth(),
                 oldImage.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageIO.write(ImageIO.read(new URL(url)), "jpg", new File(to));

It would be interesting to know if this resolves the issue.

知道这是否能解决问题会很有趣。