0

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?

Hawk
  • 897
  • 7
  • 14
  • Can you use the [`ImageIcon`] [constructor](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/ImageIcon.html#%3Cinit%3E(java.awt.Image)) that takes a parameter of type `Image`? – Abra Dec 03 '21 at 15:31
  • yse, passed a BufferedImage of bmp will keep its size. because my intention is to simulate the uploading case, that's why I create an ImageIcon with byte array. :) – Hawk Dec 04 '21 at 00:57

2 Answers2

2

From Java documentation, ImageIcon not support BMP format: enter image description here

You can use this : Get Image Dimension and Image Size from Binary

Sofyane MAKERRI
  • 395
  • 3
  • 5
  • Thanks. I thought the document just lists 3 of supported formats, not all formats. But it looks like only those 3 formats are supported. – Hawk Dec 04 '21 at 05:45
0

Thanks for your hints. I found if I read those bytes with ImageIO, I can successfully get an image's width and height.

public class ImageReader {

    private static URL imgUrl;
    private static byte[] bytes;

    public static void main(String[] args) throws IOException {
        imgUrl = ImageReader.class.getResource("myimage.jpg");
        bytes = getBytes(imgUrl, "jpg");
        printWidthHeight(bytes);
        imgUrl = ImageReader.class.getResource("flag.bmp");
        bytes = getBytes(imgUrl, "bmp");
        printWidthHeight(bytes);
    }

    private static void printWidthHeight(byte[] bytes) throws IOException {
        ImageIcon imageIcon = new ImageIcon(bytes);
        System.out.format( "%s, %s \n", imageIcon.getIconWidth(), imageIcon.getIconHeight());


        BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
        ImageIcon imageIcon2 = new ImageIcon(image);
        System.out.format( "%s, %s \n", imageIcon2.getIconWidth(), imageIcon2.getIconHeight());
    }

    private static byte[] getBytes(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();
        return bytes;
    }
}

output

5
true
171, 125 
171, 125 
5
true
-1, -1 
124, 124 
Hawk
  • 897
  • 7
  • 14