Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BuffereImage到字节数组大小_Java_Image_Bytearray - Fatal编程技术网

Java BuffereImage到字节数组大小

Java BuffereImage到字节数组大小,java,image,bytearray,Java,Image,Bytearray,我有一个BuffereImage,里面有.png或.svg。假设它是128*128(image.GetHeight()image.GetWidth()),我将其转换为字节数组,如下所示: byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 我的字节数组包含2048个字节(像素长度) 我想它的尺寸必须是128*128=16384(所有像素) 我想了解SVG和PNG是如何存储到字节数组的。我

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

byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
我的字节数组包含2048个字节(像素长度)

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


我想了解SVG和PNG是如何存储到字节数组的。我尝试过使用1像素图片(黑色像素)-如果需要字节数组表示,则可以使用字节数组大小为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();

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

如果您想要图像的像素表示,请尝试阅读以下文章:您能提供这样的图像吗?(可能是PNG,因为SVG是矢量图形,与BuffereImage无关……)好吧,但为什么我要用字节数组大小1表示一个像素?