2

我正在创建一个简单的程序,它接受灰度图像作为输入,我只想检索每个像素的颜色信息,将其存储在我称为 PixelClass 的对象数组中。最终目标只是使用所述获取的颜色信息将图像重新绘制为新的 BufferedImage。

用于从给定图像创建像素数组的代码。

    public static PixelClass[][] getPixelArray(BufferedImage bi){
    int height = bi.getHeight();
    int width = bi.getWidth();
    PixelClass[][] pixelArray = new PixelClass[height][width];
    for(int i = 0 ; i < height ; i++){
        for(int j = 0 ; j < width ; j++){
            pixelArray [i] [j] = new PixelClass(bi.getRGB(j, i));
        }
    }
    return pixelArray;
}

用于尝试使用 PixelClass 对象数组重新绘制所述图像的代码

    public void paintToPanel(PixelClass [][] pc, int height, int width){
    BufferedImage nbi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
    for ( int i = 0 ; i < height ; i++){
        for ( int j = 0 ; j < width ; j++){
            nbi.setRGB(j, i, pc[i][j].getRGBValue());
        }
    }
    JLabel containerLabel = new JLabel(new ImageIcon(nbi));
    containerLabel.setBounds(10,10,nbi.getHeight(), nbi.getWidth());
    this.add(containerLabel);
}

链接到原始图像

http://sphotos.ak.fbcdn.net/hphotos-ak-snc4/hs1364.snc4/163667_172099429501181_100001033756527_413302_3062182_n.jpg

如您所见,图像质量显着下降。生成的图像似乎变淡了。

4

1 回答 1

1

我建议你使用 MemoryImageSource 类。就像是 :

byte[] pixels = // your pixels
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); 
int bits[] = new int[] {8};

ColorModel cm = new ComponentColorModel(cs, bits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

MemoryImageSource mis = new MemoryImageSource(width, height, cm, pixels, 0, width);

Image im = Toolkit.getDefaultToolkit().createImage(mis);
于 2011-03-07T12:03:57.680 回答