my server receives a byte array of bmp by uploading, and I read its width and height but get -1
. I try to simulate this uploading case with the code below:
public class ImageReader {
private static URL imgUrl;
public static void main(String[] args) throws IOException {
imgUrl = ImageReader.class.getResource("myimage.jpg");
printWidthHeight(imgUrl, "jpg");
imgUrl = ImageReader.class.getResource("flag.bmp");
printWidthHeight(imgUrl, "bmp");
}
private static void printWidthHeight(URL imgUrl, String format) throws IOException {
BufferedImage image = ImageIO.read(imgUrl);
System.out.println(image.getType());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean isAppropriate = ImageIO.write(image, format, baos);
System.out.println(isAppropriate);
byte[] bytes = baos.toByteArray();
ImageIcon imageIcon = new ImageIcon(bytes);
System.out.format( "%s, %s \n", imageIcon.getIconWidth(), imageIcon.getIconHeight());
}
}
It prints:
5
true
171, 125
5
true
-1, -1
I found
- jpg still has width and height, but bmp doesn't
Does anyone know the reason?