0

I have a BufferedImage with .png or .svg in it. let's say it is 128*128 (image.GetHeight() image.GetWidth()) and I convert it to byte array like this:

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

and my byte array contains 2048 bytes (pixel.length) ??

I suppose that its dimension must be 128*128 = 16384 (all pixels)

I ma trying to understand how the SVG and PNG stores to byte array. I have tried with 1 pixel picture(black pixel) - that works perfect byte array size is 1

curiousity
  • 4,703
  • 8
  • 39
  • 59
  • If you want the pixel representation of an image, try reading this post: http://sanjaal.com/java/tag/java-image-pixel-representation/ – Kuba Spatny Mar 31 '14 at 12:39
  • Can you provide such an image? (Probably a PNG, since SVG is a vector graphics and not related to BufferedImage...) – Marco13 Mar 31 '14 at 13:23

1 Answers1

1

Try using the ByteArrayOutputStream if you want the byte array representation

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

Also it's worth noting that each pixel isn't represented by a byte, the color is stored as an integer. You have an alpha channel and then three channels for RGB. That means 4*Byte, which leads to 4*8 bits --> 32 bits --> integer.

Dropout
  • 13,653
  • 10
  • 56
  • 109