使用Java将PNG转换为JPG时出现问题(ImageIO.write()) [英] Problem converting PNG to JPG using Java (ImageIO.write())

查看:347
本文介绍了使用Java将PNG转换为JPG时出现问题(ImageIO.write())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

推荐答案

我不确定给定缓冲区创建后未使用其他代码段的工作方式.我发现这个粉红色的问题是特定于jvm版本的.

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

这篇关于使用Java将PNG转换为JPG时出现问题(ImageIO.write())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆