Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageIO not able to write a JPEG file

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.

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 
like image 243
Karan Avatar asked Aug 07 '10 23:08

Karan


People also ask

Can ImageIO read JPG?

imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.

What is ImageIO write?

The ImageIO. write method calls the code that implements PNG writing a “PNG writer plug-in”. The term plug-in is used since Image I/O is extensible and can support a wide range of formats. But the following standard image format plugins : JPEG, PNG, GIF, BMP and WBMP are always be present.

What is ImageIO in Java?

ImageIO. A class containing static convenience methods for locating ImageReader s and ImageWriter s, and performing simple encoding and decoding. ImageReader. An abstract superclass for parsing and decoding of images.

Is ImageIO thread safe?

A problem with this solution is that ImageIO is not thread safe, which is why I recommend using an ExecutorService with a single thread (or switching to JDeli which is thread-safe). In order for your application to end successfully, you will need to wait for any images to finish being output.


2 Answers

OpenJDK does not have a native JPEG encoder, try using Sun's JDK, or using a library (such as 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.

If you import the image back to Java, the transparency is still there, though.

like image 110
Rui Vieira Avatar answered Sep 22 '22 22:09

Rui Vieira


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.

like image 34
Thunder Avatar answered Sep 21 '22 22:09

Thunder