繁体   English   中英

Java BufferedImage灰度降级

[英]Java BufferedImage Grayscale Degradation

我正在创建一个简单的程序,该程序接受灰度图像作为输入,我只想做的就是检索每个像素的颜色信息,并将其存储在我称为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

如您所见,图像质量明显下降。 产生的图像似乎褪色。

我建议您使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM