【问题标题】:BufferedImage to byte array size [duplicate]BufferedImage 到字节数组大小[重复]
【发布时间】:2014-03-31 12:25:43
【问题描述】:

我有一个带有 .png 或 .svg 的 BufferedImage。假设它是 128*128 (image.GetHeight() image.GetWidth()),我将其转换为字节数组,如下所示:

byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

我的字节数组包含 2048 个字节(pixel.length)??

我想它的尺寸一定是128*128 = 16384(所有像素)

我想了解 SVG 和 PNG 如何存储到字节数组。我试过 1 像素图片(黑色像素) - 完美的字节数组大小是 1

【问题讨论】:

标签: java image bytearray


【解决方案1】:

如果您想要字节数组表示,请尝试使用ByteArrayOutputStream

BufferedImage bi = ImageIO.read(new File("C:\\path\\to\\image.png"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos);
baos.flush();
byte[] byteImage = baos.toByteArray();
baos.close();

另外值得注意的是,每个像素不是由byte 表示的,颜色存储为integer。您有一个 Alpha 通道,然后是三个用于 RGB 的通道。这意味着 4*Byte,导致 4*8 位 --> 32 位 --> 整数。

【讨论】:

  • 好的,但是为什么我的字节数组 sizeof 1 代表一个像素?
猜你喜欢
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 2011-12-14
相关资源
最近更新 更多