I'm using ImageIO.read to process uploaded image files. The code is similar to
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image.getContents()));
I've managed to solve most of the issues, but this one has left me clueless. The uploaded image has a JCS_YCCK profile, as defined in com.sun.imageio.plugins.jpeg.JPEG, which is not supported by com.sun.imageio.plugins.jpeg.JPEGImageReader. This leads to a nice stack trace similar to:
Caused by: javax.imageio.IIOException: Unsupported Image Type
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:910)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:885)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.example.ImageWriter.resizeEmbeddableImageInPlace(ImageWriter.java:231)
How can I process this type of JPEG using Java's ImageIO?
Update: I've tried Commons-Sanselan, indicated by an answer, but unfortunately it does not support JPEG files:
org.apache.sanselan.ImageReadException: Sanselan cannot read or write JPEG images.
at org.apache.sanselan.formats.jpeg.JpegImageParser.getBufferedImage(JpegImageParser.java:90)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1264)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1231)
One possible solution is to use the Java Advanced Imaging Image IO extensions. When properly installed, the conversion works out of the box.
The problem is that it does not play well with Maven, so I've asked Using Java Advanced Imaging with Maven. If that works out, this answer will be accepted.
I don't know for ImageIO, but you could use the Commons Sanselan library, which offers easy ways to access all sorts of images.
Related
I have created a GUI using java Swing which displays some images. I have been testing it and have managed to create some labels which I have filled with JPEG images as a test.
Now, I face the problem that I cannot display the actual files i need to display because they are .nd2 files (from a Nikon microscope). I have been looking at how to use the Bio-formats and/or IJ packages to do so...but I don't know where to start.
Can anyone help? I am using the Eclipse IDE for Java
About the format
From https://www.file-extensions.org/
... The ND2 format uses JPEG-2000 compression, and also can be
uncompressed or Zip-compressed ...
As mentioned in read jpeg2000 files in java
JPEG 2000 seems to be not included inside standard Java SDK.
Potential solutions
1. Use Open JPEG + existing JNI wrapper
I would try out https://github.com/uclouvain/openjpeg and search for some java wrappers to use openjpeg (e.g. look at https://github.com/barmintor/openjpeg for an JNI approach for maven).
2. Use Open JPEG + Write own JNI wrapper
Another approach would be to look at
https://github.com/ThalesGroup/JP2ForAndroid/blob/master/library/src/main/java/com/gemalto/jp2/JP2Decoder.java , inspect involved classes etc. and write an own JNI wrapper
The mentioned github reposoitory code writes to android bitmap, so not directly usable for your Swing project, but it shows you the way to decode JPEG2000 format by native calls to OpenJPEG library
How to convert a byte[] to a BufferedImage in Java? describes conversion from byte array to a buffered image - so these information should help you to read the image data into a buffered image (so usable in Swing).
I need to convert a DICOM file (.dcm) in a jpeg2000 format, someone has a code snippet that perform this operation?
I found in the web only a way for the dcm to jpeg conversion (the first answer to that question: How to convert dicom file to jpg conversion), but that doesn't work for me.
Thanks in advice for te help and sorry for my not perfect english.
The best tool I encountered when doing image conversion is ImageMagick (http://www.imagemagick.org/), which supports Dicom as input and JPEG-2000 as output.
I also have used dcm4che for this purpose (it has the advantage to be a purely Java solution), but the JPEG-2000 encoder had a memory leak - so if you have to do it for lots of images, it might be a problem (recent version might be fixed though).
I try to read a JPEG file with ImageIO.read() but for this image it give me a CMMException. after read this and this I understand ImageIO can't read some kind of jpeg file.
So I need a solution to read all kind of images. JAI library look to be a dead library. And I don't undestrand how TwelveMonkeys works. So if someone have explainations about it or another alternative, I'll take it. Thank's
For reading most JPEGs (even those that that cause CMMExceptions), you can use ImageIO and TwelveMonkeys ImageIO plug-ins. To do so, add the following dependency to your Maven project:
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.0</version>
If you already use ImageIO to read images, there's no need to change your code. :-)
To verify that the plugin is installed and used at run-time, you could use the following code:
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
while (readers.hasNext()) {
System.out.println("reader: " + readers.next());
}
The first line should print:
reader: com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader#somehash
I'm attempting to get a JPEG into a BufferedImage to display on a JPanel . However, javax.imageio.ImageIO.read() seems to be very fussy about the JPEGs it processes, often throwing an IIOException. The same JPEGs open fine in pretty much any image reader you'd care to name.
I've looked at Apache's Sanselan and JAI. But Sanselan can't process JPEGs and JAI isn't available for 64-bit Windows platforms and doesn't seem to be maintained (the last update was in 2006). A previous answer on StackOverflow suggested com.sun.image.codec.jpeg.JPEGCodec, but this was deprecated in Java 5 and has disappeared in 7.
Are these my only options? Are there really no Java libraries capable of robustly reading JPEGs into a BufferedImage?
Legacy Toolkit methods such as createImage and getImage are known to be more lenient than ImageIO.
Werner Randelshofer also wrote a Service Provider to read CMYK JPEGs with ImageIO.
By combining both approaches (try every possible ImageReader and then fallback to Toolkit) you will be able to handle a reasonable number of JPEG Images.
Reading JPEGs with CMYK profile may be a interesting read.
I am working on google appengine to create a tool for comparing image similarity.
I need to extract the pixel values of each image to perform this.
Unfortunately appengine does not support the java image libs.So I am unable to proceed.
Is there any appengine safe image library in java capable of extracting image data?
I saw some techniques in python but dont want to switch to python if I can do it in java somehow...
GAEJ has its own graphic library with fairly limited features and java.awt.image.BufferedImage is a restricted class (ie, java.awt.Image is not supported and still not present in the Jre Class White List ).
There's an open issue here, that you might want to star.
EDIT:
Somebody has patched pngj to work with InputStream.(You could use it to read a PNG pixel by pixel)
The new version of pngj now an alternative pngj-sandbox.jar that only references whitelisted classes, it should run in google-app-engine.
can https://github.com/witwall/appengine-awt help to you? i believe it willbe enough to add theis lib as dependency to the project to make BufferedImage working (but havent' tried this yet)