问题描述
我有一个BufferedImage我正在尝试写一个jpeg文件,但是我的Java程序抛出异常。我能够成功地将相同的缓冲区保存到gif和png。我曾尝试在Google上寻找解决方案,但无济于事。
I have a BufferedImage I'm trying to write to a jpeg file, but my Java program throws an exception. I'm able to successfully save the same buffer to a gif and png. I've tried looking around on Google for solutions, but to no avail.
代码:
File outputfile = new File("tiles/" + row + ":" + col + ".jpg");
try {
ImageIO.write(mapBufferTiles[row][col], "jpg", outputfile);
} catch (IOException e) {
outputfile.delete();
throw new RuntimeException(e);
}
例外:
Exception in thread "main" java.lang.RuntimeException: javax.imageio.IIOException: Invalid argument to native writeImage
at MapServer.initMapBuffer(MapServer.java:90)
at MapServer.<init>(MapServer.java:24)
at MapServer.main(MapServer.java:118)
Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:357)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at javax.imageio.ImageIO.doWrite(ImageIO.java:1602)
at javax.imageio.ImageIO.write(ImageIO.java:1526)
at MapServer.initMapBuffer(MapServer.java:87)
... 2 more
推荐答案
OpenJDK没有原生JPEG编码器,尝试使用Sun的JDK,或使用库(例如
OpenJDK does not have a native JPEG encoder, try using Sun's JDK, or using a library (such as JAI
AFAIK,关于粉红色调,Java将JPEG保存为ARGB(仍具有透明度)信息)。大多数观众在打开时假设四个频道必须对应一个CMYK(不是ARGB),因此必须是红色。
AFAIK, regarding the "pinkish tint", Java saves the JPEG as ARGB (still with transparency information). Most viewers, when opening, assume the four channels must correspond to a CMYK (not ARGB) and thus the red tint.
如果你将图像导回到Java,透明度仍然存在。
If you import the image back to Java, the transparency is still there, though.
这篇关于ImageIO无法写入JPEG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!