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

发布于 2024-08-12 15:54:22 字数 118 浏览 7 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦回旧景 2024-08-19 15:54:22

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

我发现的最简单的解决方案就是这样做。

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

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.

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
ぺ禁宫浮华殁 2024-08-19 15:54:22

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

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

编辑上面的链接已损坏,但似乎就是这样。

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

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

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

Edit Above link is broken, but this appears to be it.

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

雨巷深深 2024-08-19 15:54:22

我也遇到了同样的问题,但是如果我以 png 格式编写它就可以解决。

像这样的事情,

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

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

Something like this,

ImageIO.write(resizedImageBuffer, "png", baos);
漆黑的白昼 2024-08-19 15:54:22

我发现这个 链接 其中有一些可能有用的代码。我用我的一些图像尝试了您的代码,但无法重现该问题。我尝试了 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));

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

I found this link which 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文