从字节数组中获取像素值
Getting Pixel Values from Byte Array
我无法获取像素数据。
我的程序截屏,每次循环它都会存储之前的截图。
我的目标是在像素级别对当前屏幕截图和旧屏幕截图进行比较。
我 运行 这个代码告诉我屏幕截图的格式:
System.out.println(image.getType());
这个(对于我的程序)的输出是 1,表示它是 BufferedImage.TYPE_INT_RGB
根据我的阅读,类型决定了像素值在字节数组中的顺序。
我正在使用此代码将我的缓冲图像转换为字节数组(缓冲图像是使用 awt.Robot class 创建的):
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 中的像素值的最简单方法是使用光栅:
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...
}
}
我无法获取像素数据。
我的程序截屏,每次循环它都会存储之前的截图。
我的目标是在像素级别对当前屏幕截图和旧屏幕截图进行比较。
我 运行 这个代码告诉我屏幕截图的格式:
System.out.println(image.getType());
这个(对于我的程序)的输出是 1,表示它是 BufferedImage.TYPE_INT_RGB
根据我的阅读,类型决定了像素值在字节数组中的顺序。
我正在使用此代码将我的缓冲图像转换为字节数组(缓冲图像是使用 awt.Robot class 创建的):
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 中的像素值的最简单方法是使用光栅:
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...
}
}