版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net/10km/article/details/88680596
从RGBA格式转BufferedImage的实现如下,注意,这个实现实际只保留了,Red,Green,Blue三个颜色通道数据,删除了alpha通道。
/** * 从RGBA格式图像矩阵数据创建一个BufferedImage * @param matrixRGBA RGBA格式图像矩阵数据,为null则创建一个指定尺寸的空图像 * @param width * @param height * @return */ public static BufferedImage createRGBAImage(byte[] matrixRGBA,int width,int height){ // 定义每像素字节数 int bytePerPixel = 4; if(null != matrixRGBA&&matrixRGBA.length == width*height*bytePerPixel){ throw new IllegalArugmentException("invalid image description"); } // 将图像数据byte[]封装为DataBuffer DataBufferByte dataBuffer = null == matrixRGBA ? null : new DataBufferByte(matrixRGBA, matrixRGBA.length); // 定义色彩空间 sRGB ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] bOffs = {0,1,2}; // 根据色彩空间创建色彩模型(ColorModel实例),bOffs用于定义R,G,B三个分量在每个像素数据中的位置 ComponentColorModel colorModel = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); // 从DataBuffer创建光栅对象Raster WritableRaster raster = null != dataBuffer ? Raster.createInterleavedRaster(dataBuffer, width, height, width*bytePerPixel, bytePerPixel, bOffs, null) : Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,width*bytePerPixel, bytePerPixel, bOffs, null); BufferedImage img = new BufferedImage(colorModel,raster,colorModel.isAlphaPremultiplied(),null); return img; }
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句