繁体   English   中英

如何从字节数组(2字节/像素)获取PNG?

[英]How can I get a PNG from a byte Array (2 bytes/pixel)?

我有一个字节数组,我的数据每个像素存储两个字节。 我现在想从此数据中获取PNG。 我在这里找到了很棒的代码:

byte[] aByteArray = {0xa,0x2,0xf,(byte)0xff,(byte)0xff,(byte)0xff};
int width = 1;
int height = 2;

DataBuffer buffer = new DataBufferByte(aByteArray, aByteArray.length);

//3 bytes per pixel: red, green, blue
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, 3 * width, 3, new int[] {0, 1, 2}, (Point)null);
ColorModel cm = new ComponentColorModel(ColorModel.getRGBdefault().getColorSpace(), false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); 
BufferedImage image = new BufferedImage(cm, raster, true, null);

ImageIO.write(image, "png", new File("image.png"));

在这里,作者使用每个像素3个字节。 所以我的问题是:如果我的字节数组每个像素有2个字节,如何使此代码起作用?

更新:数据是灰度的,对不起,我以前不知道。。。因此无需获取彩色图像。 因此,您知道谁从该字节数组中获得灰度图像吗?

在我看来,您必须将此代码赋予每个像素3个字节,因此它获得了RGB的三种颜色。 如果您只想使用绿色和蓝色或其他组合来做到这一点,则应该为第三个设置默认值...但是我不确定100%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM