I am trying to write a method that takes an image, and saves a 100 by 100 thumbnail of that image. However, when I save the file, it comes out as an unreadable 0 byte image (with the error "Error interpreting JPEG image file (Improper call to JPEG library in state 200)") in Ubuntu's ImageViewer. My code is as follows:
public boolean scale(){
String file = filename.substring(filename.lastIndexOf(File.separator)+1);
File out = new File("data"+File.separator+"thumbnails"+File.separator+file);
if( out.exists() ) return false;
BufferedImage bi;
try{
bi = ImageIO.read(new File(filename));
}
catch(IOException e){
return false;
}
Dimension imgSize = new Dimension(bi.getWidth(), bi.getHeight());
Dimension bounds = new Dimension(100, 100);
int newHeight = imgSize.height;
int newWidth = imgSize.width;
if( imgSize.width > bounds.width ){
newWidth = bounds.width;
newHeight = (newWidth*imgSize.height)/imgSize.width;
}
if( imgSize.height > bounds.width ){
newHeight = bounds.height;
newWidth = (newHeight*imgSize.width)/imgSize.height;
}
Image img = bi.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
BufferedImage thumb = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = thumb.createGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
try{
ImageIO.write(thumb, "jpg", out);
}
catch(IOException e){
return false;
}
return true;
}
Where "filename" is a global variable for the class housing this method, representing the path to the original image. My main issue is that I do not see why I'm creating a 0 byte image.
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.
Just use the read method of the Java ImageIO class, and you can open/read images in a variety of formats (GIF, JPG, PNG) in basically one line of Java code.
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.
So, the issue was this. I'm working in OpenJDK. OpenJDK doesn't have a JPEG encoder, apparently, so while the file was being created by
ImageIO.write(thumb, "jpg", out);
it wasn't actually creating anything for the file to save; hence the empty 0 byte unreadable file. Changing the ImageIO argument to "png" (and adjusting the new File() extension, appropriately) successfully created the desired image with the above code.
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