簡體   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