繁体   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