For some reason, opening up some PNG files using ImageBuffer and ImageIO does not work. Here's some code I am using that works fine for resizing/cropping JPGs:
BufferedImage image = ImageIO.read(new File(location));
BufferedImage croppedImage = image.getSubimage(
cropInfo.getX(), cropInfo.getY(), cropInfo.getW(), cropInfo.getH());
BufferedImage resizedImage = new BufferedImage(
TARGET_WIDTH, TARGET_HEIGHT, croppedImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(croppedImage, 0, 0, TARGET_WIDTH, TARGET_HEIGHT, null);
g.dispose();
this.changeContentType("image/png", ".png"); // not really relevant. just a property
ImageIO.write(resizedImage, "png", new File(location));
return resizedImage;
The goal of this function is to take whatever type is given, resize and crop the image, and then save it to PNG with the same filename.
It works on Windows, but if I crop/resize on Linux (lenny), it crashes altogether and complains about the type of the file (it says the type is 0).
java.lang.IllegalArgumentException: Unknown image type 0
java.awt.image.BufferedImage.<init>(BufferedImage.java:490)
trainingdividend.domain.file.ServerImage.resizeImage(ServerImage.java:68)
trainingdividend.domain.file.ServerImage.cropAndResize(ServerImage.java:80)
trainingdividend.service.user.UserAccountManagerImpl.cropAvatar(UserAccountManagerImpl.java:155)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Solutions?
Is there another library I can use altogether?
sun.reflect
in the stack trace, it's Sun's, but just to be sure)