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.
我有一个BufferedImage我正在尝试写入一个jpeg文件,但我的Java程序抛出一个异常。我能够成功地将相同的缓冲区保存到gif和png。我曾尝试在Google上寻找解决方案,但无济于事。
Code:
码:
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:
例外:
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
3 个解决方案
#1
33
OpenJDK does not have a native JPEG encoder, try using Sun's JDK, or using a library (such as JAI
OpenJDK没有原生JPEG编码器,尝试使用Sun的JDK,或使用库(如JAI)
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.
AFAIK,关于“粉红色调”,Java将JPEG保存为ARGB(仍然具有透明度信息)。大多数观众在打开时假设四个频道必须对应于CMYK(不是ARGB),因此必须对应红色。
If you import the image back to Java, the transparency is still there, though.
如果将图像导回到Java,透明度仍然存在。
#2
24
I had the same issue in OpenJDK 7 and I managed to get around this exception by using an imageType
of TYPE_3BYTE_BGR
instead of TYPE_4BYTE_ABGR
using the same OpenJDK.
我在OpenJDK 7中遇到了同样的问题,我设法使用相同的OpenJDK使用TYPE_3BYTE_BGR的imageType而不是TYPE_4BYTE_ABGR来解决此异常。
#3
0
You get the same error
你得到同样的错误
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)
if you are using a not supported Color Space (in my case CYMK). See How to convert from CMYK to RGB in Java correctly? how to solve this.
如果您使用的是不受支持的色彩空间(在我的情况下是CYMK)。请参阅如何正确地在Java中将CMYK转换为RGB?如何解决这个问题。
#1
33
OpenJDK does not have a native JPEG encoder, try using Sun's JDK, or using a library (such as JAI
OpenJDK没有原生JPEG编码器,尝试使用Sun的JDK,或使用库(如JAI)
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.
AFAIK,关于“粉红色调”,Java将JPEG保存为ARGB(仍然具有透明度信息)。大多数观众在打开时假设四个频道必须对应于CMYK(不是ARGB),因此必须对应红色。
If you import the image back to Java, the transparency is still there, though.
如果将图像导回到Java,透明度仍然存在。
#2
24
I had the same issue in OpenJDK 7 and I managed to get around this exception by using an imageType
of TYPE_3BYTE_BGR
instead of TYPE_4BYTE_ABGR
using the same OpenJDK.
我在OpenJDK 7中遇到了同样的问题,我设法使用相同的OpenJDK使用TYPE_3BYTE_BGR的imageType而不是TYPE_4BYTE_ABGR来解决此异常。
#3
0
You get the same error
你得到同样的错误
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)
if you are using a not supported Color Space (in my case CYMK). See How to convert from CMYK to RGB in Java correctly? how to solve this.
如果您使用的是不受支持的色彩空间(在我的情况下是CYMK)。请参阅如何正确地在Java中将CMYK转换为RGB?如何解决这个问题。