2 回答
TA贡献1827条经验 获得超4个赞
试试这个代码
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);
}
TA贡献1824条经验 获得超8个赞
前面的答案有效,但可以简化以更具可读性:
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel model = new ComponentColorModel(
colorSpace,
false,
true,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE
);
WritableRaster raster = Raster.createInterleavedRaster(
DataBuffer.TYPE_BYTE,
width,
height,
3,
null
);
BufferedImage image = new BufferedImage(model, raster, true, null);
添加回答
举报