簡體   English   中英

從字節數組獲取像素值

[英]Getting Pixel Values from Byte Array

我在獲取像素數據時遇到問題。

我的程序獲取屏幕截圖,每個循環將存儲先前的屏幕截圖。

我的目標是在當前屏幕截圖和舊屏幕截圖之間的像素級別進行比較。

我運行了這段代碼,告訴我屏幕截圖的格式是: System.out.println(image.getType());

此輸出(對於我的程序)為1,表示其為BufferedImage.TYPE_INT_RGB

根據讀取的內容,這些類型確定像素值在字節數組中的順序。

我正在使用此代碼將我的緩沖圖像轉換為字節數組(該緩沖圖像是使用awt.Robot類創建的):

public byte[] convertToByteArray(BufferedImage img){
        byte[] imageInByte = null;
        try {



            // convert BufferedImage to byte array
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, "png", baos);
            baos.flush();
            imageInByte = baos.toByteArray();
            baos.close();

        } catch (IOException ex) {
            Logger.getLogger(OverlayWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        return imageInByte;
}

最后,我使用比較方法來檢查字節數組。 現在,這僅顯示數組的顏色值:

  final byte[] pixels = convertToByteArray(image);
  final int pixelLength = 3;
        for (int pixel = 0, row = 0, col = 0; pixel < 1; pixel += pixelLength) {
        int argb = 0;
        argb += -16777216; // 255 alpha
        argb += ((int) pixels[pixel] & 0xff); // blue
        argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
        argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red


        int r = (argb >> 16) & 0xff, g = (argb >> 8)& 0xff, b = argb & 0xff;
        System.out.println("R = " + r);
        System.out.println("G = " + g);
        System.out.println("B = " +  b);

        col++;
        if (col == width) {
           col = 0;
           row++;
        }




     }

我的這段代碼的問題是,即使我截取了純色的屏幕截圖,像素值也無處不在。 我期望每個像素具有相同的顏色值。

-編輯-

由於性能原因,我避免使用getRGB。 在我的應用程序中,每次迭代兩次調用getRGB的大圖像非常昂貴。

訪問BufferedImage中的像素值的最簡單方法是使用Raster:

BufferedImage image = ...
for (int y=0 ; y < image.getHeight() ; y++)
    for (int x=0 ; x < image.getWidth() ; x++)
        for (int c=0 ; c < image.getRaster().getNumBands() ; c++)
            final int value = image.getRaster().getSample(x, y, c) ; // Returns the value of the channel C of the pixel (x,y)

柵格將為您處理編碼,使其成為訪問像素值的最簡單方法。 但是,最快的方法是使用DataBuffer,但隨后必須管理所有編碼。

/* This method takes a BufferedImage encoded with TYPE_INT_ARGB and copies the pixel values into an image encoded with TYPE_4BYTE_ABGR.*/
public static void IntToByte(BufferedImage source, BufferedImage result)
    {
    final byte[] bb = ((DataBufferByte)result.getRaster().getDataBuffer()).getData() ;
    final int[] ib  = ((DataBufferInt)source.getRaster().getDataBuffer()).getData() ;

    switch ( source.getType() )
        {
        case BufferedImage.TYPE_INT_ARGB :
            for (int i=0, b=0 ; i < ib.length ; i++, b+=4)
                {
                int p   = ib[i] ;
                bb[b]   = (byte)((p & 0xFF000000) >> 24) ;
                bb[b+3] = (byte)((p & 0xFF0000) >> 16) ;
                bb[b+2] = (byte)((p & 0xFF00) >> 8) ;
                bb[b+1] = (byte)( p & 0xFF) ;
                }
            break ;
        // Many other case to manage...
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM