使用缓冲图像写入RGB图像

时间:2011-03-02 02:10:38

标签: java image-processing image-manipulation bufferedimage

我有一个二维字节数组I [] [],每个条目都是0或-1(即255)

现在我使用以下代码以RGB格式编写图像。

    public void writeImage(String outputFileName){
    BufferedImage BI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for(int y = 0;y < height;y++){
        for(int x = 0;x < width;x++){
            int gray = (int) I[y][x] & 0xFF;
            int newRGB = (gray << 16) + (gray << 8) + gray;
            BI.setRGB(x, y, newRGB);
        }
    }
    try{
        ImageIO.write(BI, "JPG", new File(outputFileName));
    } catch(IOException e){
        System.err.println("Unable to output results");
    }
}

图像成功写入。由于原始数据数组仅包含两个不同的值(0和255),因此我预计写入的图像也将具有两个不同的级别。然而,书面图像有92个不同的级别。我做错了什么?

1 个答案:

答案 0 :(得分:4)

你没有做错任何事,但JPEG压缩意味着每个像素中的水平没有被精确记录 - 当图像被解压缩时,JPEG压缩伪像会将锐边稍微扩展到其他像素。