Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将图像的光栅字节数组转换为原始图像?_Java_Image_Image Processing_Raster - Fatal编程技术网

如何在java中将图像的光栅字节数组转换为原始图像?

如何在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)

我需要将图像的字节转换为原始图像。我的字节数组是由WritableRaster创建的。我试图转换成原始形式,但它生成的图像没有原始颜色。我的尝试如下

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知道如何解释图像数据