我正在尝试打印位图图像。为了打印图像,我的打印机需要位图的字节数组。图片大小为128x128像素。
以下是我如何读取图像并将其转换为字节数组的代码。
BufferedImage image = ImageIO.read(new File("test.bmp"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "bmp", baos);
byte[] imageInByte = baos.toByteArray();
System.out.println(imageInByte.length);
代码执行后 imageInByte 数组长度为2110.我在这里缺少什么?阵列长度不应该是16384(128 x 128)?
答案 0 :(得分:1)
您假设每像素一个字节且没有标头信息。 " bitsPerPixel"标题将在确定图像数据占用多少空间方面发挥重要作用。请参阅位图文件here的结构。