Java 使用 ImageIO.write jpg 文件的问题:粉红色背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4386446/
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
Issue using ImageIO.write jpg file: pink background
提问by Koerr
I'm using the following code to write a jpg file:
我正在使用以下代码编写一个 jpg 文件:
String url="http://img01.taobaocdn.com/imgextra/i1/449400070/T2hbVwXj0XXXXXXXXX_!!449400070.jpg";
String to="D:/temp/result.jpg";
ImageIO.write(ImageIO.read(new URL(url)),"jpg", new File(to));
But I get the result.jpg
is a pink background image:
但我得到的result.jpg
是一个粉红色的背景图片:
采纳答案by finnw
You can work around this by using Toolkit.createImage(url)
instead of ImageIO.read(url)
which uses a different implementation of the decoding algorithm.
您可以通过使用Toolkit.createImage(url)
而不是ImageIO.read(url)
which 使用解码算法的不同实现来解决此问题。
If you are using the JPEG encoder included with the Sun JDK then you must also ensure that you pass it an image with no alpha channel.
如果您使用的是 Sun JDK 中包含的 JPEG 编码器,那么您还必须确保向它传递一个没有 alpha 通道的图像。
Example:
例子:
private static final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF};
private static final ColorModel RGB_OPAQUE =
new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
// ...
String sUrl="http://img01.taobaocdn.com/imgextra/i1/449400070/T2hbVwXj0XXXXXXXXX_!!449400070.jpg";
URL url = new URL(sUrl);
Image img = Toolkit.getDefaultToolkit().createImage(url);
PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
pg.grabPixels();
int width = pg.getWidth(), height = pg.getHeight();
DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null);
String to = "D:/temp/result.jpg";
ImageIO.write(bi, "jpg", new File(to));
Note: My guess is that the color profile is corrupted, and Toolkit.createImage()
ignores all color profiles. If so then this will reduce the quality of JPEGs that have a correct color profile.
注意:我的猜测是颜色配置文件已损坏,并Toolkit.createImage()
忽略了所有颜色配置文件。如果是这样,那么这将降低具有正确颜色配置文件的 JPEG 的质量。
回答by Ali Irawan
I had similar problems. But then I solved it by using this one
我有类似的问题。但后来我用这个解决了
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//do something to populate the image
//such as
image.setRGB( x, y, pixelValue); //set your own pixels color
ImageIO.write(image, "jpg", new File("D:\test.jpg"));
Note that I'm using Java version 1.6.0_25-b06 and It's just works fine.
请注意,我使用的是 Java 版本 1.6.0_25-b06,它运行良好。
Maybe you can check the Java version.
也许你可以检查Java版本。
回答by hans moleman
BufferedImage originalImage = ImageIO.read(getContent());
BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
for (int x = 0; x < originalImage.getWidth(); x++) {
for (int y = 0; y < originalImage.getHeight(); y++) {
newImage.setRGB(x, y, originalImage.getRGB(x, y));
}
}
this did the trick for me
这对我有用
回答by Claudio Rossetto
This works for me:
这对我有用:
int w = originalImage.getWidth();
int h = originalImage.getHeight();
BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int[] rgb = originalImage.getRGB(0, 0, w, h, null, 0, w);
newImage.setRGB(0, 0, w, h, rgb, 0, w);
回答by Bazi
I faced the same issue when reading and writing images using ImageIO
. After reading answers here I changed my JRE - from openjdk version "1.8.0_91"
to HotSpot java version "1.8.0_102"
. The hint was not obvious in finnw
's answer but it worth a try ;-)
我在使用ImageIO
. 这里读书的答案后,我改变了我的JRE -从openjdk version "1.8.0_91"
到HotSpot java version "1.8.0_102"
。finnw
答案中的提示并不明显,但值得一试;-)
And thus the issue resolved! So if you are using OpenJDK JRE and free to change your JRE, changing it HotSpot and you won't need to change your code.
从而问题解决了!因此,如果您正在使用 OpenJDK JRE 并且可以自由更改 JRE,则更改它的 HotSpot 并且您不需要更改您的代码。
回答by Reto H?hener
- for me the problem was not the reading but the writing
- ImageIO will happily write JPG files from ARGB BufferedImages
- Browsers/other programs then interpret this 4-channel file as CMYK color or something, resulting in the effect described in this question
- solutionfor me: make sure that that BufferedImage that is passed to ImageIO.write is of type RGB (not ARGB)
- this also explains why the problem only appears when saving to jpeg, but not when saving to png
- this took a long time for me to figure out because my own image tools always convert to ARGB on the fly, so I was always passing an ARGB image to ImageIO.write, without realizing it
- 对我来说,问题不在于阅读,而在于写作
- ImageIO 会很乐意从 ARGB BufferedImages 写入 JPG 文件
- 浏览器/其他程序然后将此 4 通道文件解释为 CMYK 颜色或其他颜色,从而产生此问题中描述的效果
- 我的解决方案:确保传递给 ImageIO.write 的 BufferedImage 是 RGB 类型(不是 ARGB)
- 这也解释了为什么问题只在保存为 jpeg 时出现,而在保存为 png 时不出现
- 我花了很长时间才弄明白,因为我自己的图像工具总是即时转换为 ARGB,所以我总是将 ARGB 图像传递给 ImageIO.write,却没有意识到