java - 从字节数组获取像素值

标签 java arrays image-processing bufferedimage

我在获取像素数据时遇到问题。

我的程序会截屏,每次循环都会存储上一个屏幕截图。

我的目标是在当前屏幕截图和旧屏幕截图之间进行像素级别的比较。

我运行了这段代码,它告诉我屏幕截图的格式: 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...
        }
    }

关于java - 从字节数组获取像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620632/

相关文章:

java - GWT Place Class 增强可行性

java - ScrollView 内的 LinearLayout 扩展得不够远

python - 如何从一组相似图像中删除点状水印?

java.io.FileNotFoundException : in. txt,(系统找不到指定的文件)

java - 我应该选择哪个 Java OTR 库?

java - 如何将字符串参数拼凑在一起形成现有符号?

java - 如何从java数组中获取非随机样本

javascript - 使用 JavaScript 按公共(public)值对对象属性进行分组

image-processing - 具有反卷积或其他功能的高档层

algorithm - Matlab 图像分析,试图检测运动方向