I just want to ask how to get the width and height of an image, because this returns -1 for width and height:
private void resizeImage(Image image){
JLabel imageLabel = new JLabel();
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
System.out.println("Width:" + imageWidth);
System.out.println("Height:" + imageHeight);
}
You should do something like this :
BufferedImage bimg = ImageIO.read(new File(filename));
int width = bimg.getWidth();
int height = bimg.getHeight();
as this post says
With Apache Commons Imaging, you can get image width and height with better performance, without reading entire image to memory.
Sample code below is using Sanselan 0.97-incubator (Commons Imaging is still SNAPSHOT as I write this):
final ImageInfo imageInfo = Sanselan.getImageInfo(imageData);
int imgWidth = imageInfo.getWidth();
int imgHeight = imageInfo.getHeight();
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