我有一个带有RGB值的byte[]数组。我想创建BufferedImage而不逐个设置像素,因为图像可能很大。我找到了以下片段:
byte[] frame = ...;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(frame, frame.length), new Point() ) );这确实很好,但是有一个小问题;-) TYPE_3BYTE_BGR期望字节的顺序是相反的。
所以问题是:
的更好的方法?
发布于 2019-10-30 00:48:51
试试这段代码
BufferedImage img = create3ByteRGBImage(int width, int height, new int[] {8, 8, 8},
new int[] {0, 1, 2});
private BufferedImage create3ByteRGBImage(width, height, int[] nBits, int[] bOffs) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colorModel =
new ComponentColorModel(cs, nBits,
false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height,
width*3, 3,
bOffs, null);
return new BufferedImage(colorModel, raster, false, null);
}https://stackoverflow.com/questions/58616909
复制相似问题