2011-03-02 69 views
0

如何找到BufferedImage中的顏色通道順序(不同類型,包括Alpha通道)?BufferedImage中的通道順序?

我需要知道像LookupOp(ByteLookupTable(int,byte [] []))或RescaleOp(float [],float)中Byup [] []的順序的R,G和B參數的順序[],提示)。

是否有一種通用的方法來查找給定BufferedImage的順序?我認爲它應該在ColorModel中,但我找不到它。

我已經使用像if (t == BufferedImage.TYPE_INT_ARGB)這樣的代碼,但必須有更好的方法,對吧?

+0

澄清:如果我需要將RGB,BRG,RGBA甚至另一個訂單中的LookupOp中的R,G和B的查找表放在我所尋找的信息中。 – robcast 2011-03-03 11:05:58

回答

1

我認爲你要找的是 SampleModelColorModel

SampleModel描述了數據是如何組織的,它允許您獲取一個或多個像素的數據。 (通過調用bi.getData().getSampleModel(),獲得SampleModel,其中bi是BufferedImage)。

ColorModel然後提供用於從像素獲取ARGB組分的方法(getAlphagetRedgetGreenGetBlue)。

附錄:

我覺得你用這個方式是:

BufferedImage bi = ...; 
    Raster r = bi.getData(); 
    // Use the sample model to get the pixel 
    SampleModel sm = r.getSampleModel(); 
    Object pixel = sm.getPixel(0, 0, (int[])null, r.getDataBuffer()); 
    // Use the color model to get the red value from the pixel 
    ColorModel cm = bi.getColorModel(); 
    int red = cm.getRed(pixel[0]); 

這看起來像這將是用於處理可能遇到的任何顏色/採樣模式非常靈活,但我可以沒想到表演會很壯觀。我可能會使用這種模型不可知的方法將圖像轉換爲TYPE_INT_ARGB,其中佈局是有據可查的,然後直接操作數據。然後,如有必要,將其轉換回原始格式。

+0

SampleModel的鏈接對我來說是新的,但我仍然沒有找到有關我必須提供的渠道順序的信息,例如, LookupOp。我知道我可以從ColorModel獲得一個像素的Red(),但我怎麼知道紅色通道的順序在哪裏? – robcast 2011-03-03 11:09:40