如何在java中将图像的光栅字节数组转换为原始图像?
我需要将图像的字节转换为原始图像。我的字节数组是由WritableRaster创建的。我试图转换成原始形式,但它生成的图像没有原始颜色。我的尝试如下如何在java中将图像的光栅字节数组转换为原始图像?,java,image,image-processing,raster,Java,Image,Image Processing,Raster,我需要将图像的字节转换为原始图像。我的字节数组是由WritableRaster创建的。我试图转换成原始形式,但它生成的图像没有原始颜色。我的尝试如下 try { BufferedImage readImage = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")); byte imageData[] = get_byte_data(readImage)
try {
BufferedImage readImage = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"));
byte imageData[] = get_byte_data(readImage); // Then I need to convert this to original image
DataBuffer buffer = new DataBufferByte(imageData, imageData.length);
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"));
} catch (IOException ex) {
Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static byte[] get_byte_data(BufferedImage image) {
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
return buffer.getData();
}
我发现你的代码出了什么问题: 更改此行:
WritableRaster raster=raster.createInterleavedRaster(缓冲区、宽度、高度、3*width、3、新int[]{0、1、2},(点)null)代码>
到
WritableRaster raster=raster.createInterleavedRaster(缓冲区、宽度、高度、3*width、3、新int[]{2,1,0},(点)null)代码>
将正常工作,因为原始图像数据是BGR格式而不是RGB。如何将光栅转换回“原始”图像?BuffereImage由光栅和颜色模型组成。ColorModel知道如何解释图像数据