Ok I have an Image that I'm trying to read. Problem is that the Image.read(file) returns NULL.
File file = new File("C:\\images\\image1.jpg");
if(file.exists()){
System.out.println("Image file exists.");
BufferedImage originalImage = ImageIO.read(file);
}
So image exists but ImageIO.read(file) returns NULL. No thrown errors nothing!!! Whats going on?
This is what I have tried so far:
Can someone help with this? I'm new to this, not sure how to fix this.
Ok I just figured out that ImageIO.getImageReaders(stream) returns an empty Iterator. This means that it couldn't find a suitable reader? How am I supposed to read this image?
Image I/O recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.
ImageIO provides ImageReader and ImageWriter plug-ins for the Graphics Interchange Format (GIF) image format.
Java implements a particular type of object called a BufferedImage for images in Java. A BufferedImage can be read from several distinct image types (i.e., BMP, HEIC, etc.). Not all of these are backed by ImageIO itself, but there are plugins to extend ImageIO and other libraries such as Apache Imaging and JDeli.
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.
Ok since I switched laptops, I looked at my old laptop and found this JAR jai-imageio.jar in the jre/ext/lib (I know bad idea). I moved it to my project/lib and it worked! I guess this jai-imageio.jar contains additional image readers.
From the Javadocs for ImageIO.read()
Returns a
BufferedImage
as the result of decoding a supplied File with anImageReader
chosen automatically from among those currently registered. TheFile
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.
Try creating an ImageInputStream
, then pass that onto the ImageIO.read()
method, instead of sending the file itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With