public static ByteArrayOutputStream getImgByteArrayOutputStream(byte[] rgba,int width, int height, String imageType) {
int capacity = height * width * 3;
ByteArrayOutputStream os = new ByteArrayOutputStream(capacity);
try{
int bytePerPixel = 4;
if(null != rgba && rgba.length != width*height*bytePerPixel){
throw new RuntimeException("invalid image description");
}
DataBufferByte dataBuffer = null == rgba ? null : new DataBufferByte(rgba, rgba.length);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] bOffs = {0,1,2};
ComponentColorModel colorModel = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
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);
ImageIO.setUseCache(false);
ImageIO.write(img, imageType.replace(".",""), os);
return os;
} catch (Exception e){
log.info("picture make failed ", e);
}
return null;
}