【问题标题】:why read a byte array of bmp and the width and height lost为什么读取bmp的字节数组并且丢失了宽度和高度
【发布时间】:2022-01-09 22:32:23
【问题描述】:

我的服务器通过上传接收到一个 bmp 字节数组,我读取了它的宽度和高度,但得到了-1。我尝试用下面的代码模拟这个上传案例:

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());
    }
}

打印出来:

5
true
171, 125 
5
true
-1, -1 

我找到了

  • jpg 仍然有宽度和高度,但 bmp 没有

有人知道原因吗?

【问题讨论】:

  • 你能使用带有Image类型参数的[ImageIcon]constructor吗?
  • 是的,传递了 bmp 的 BufferedImage 将保持其大小。因为我的目的是模拟上传案例,所以我创建了一个带有字节数组的 ImageIcon。 :)

标签: java bmp


【解决方案1】:

感谢您的提示。我发现如果我用ImageIO 读取这些字节,我可以成功获取图像的宽度和高度。

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;
    }
}

输出

5
true
171, 125 
171, 125 
5
true
-1, -1 
124, 124 

【讨论】:

    【解决方案2】:

    从 Java 文档看,ImageIcon 不支持 BMP 格式:

    你可以使用这个: Get Image Dimension and Image Size from Binary

    【讨论】:

    • 谢谢。我认为该文档仅列出了 3 种支持的格式,而不是所有格式。但似乎只支持这 3 种格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    相关资源
    最近更新 更多