- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在抓取屏幕的一部分并扫描特定颜色范围的像素。
我看了MSDN's Capturing an Image示例并知道如何使用这些函数。
我可以将这些位放入数组中,但我不确定如何以可以像处理图像一样遍历数组的方式进行操作。一个伪示例(我敢肯定它离我很远):
for ( x = 1; x <= Image.Width; x += 3 )
{
for ( y = 1; y <= Image.Height; y += 3 )
{
red = lpPixels[x];
green = lpPixels[x + 1];
blue = lpPixels[x + 2];
}
}
这基本上就是我想要做的,所以如果红色、蓝色和绿色是某种颜色,我就会知道它在图像中 (x, y) 的坐标。
我只是不知道如何以这种方式使用 GetDIBits,以及如何适本地设置数组以实现此目的。
最佳答案
除了已经给出的好的答案之外,这里还有一个示例,说明如何获得一个简单的数组结构来行走。 (您可以使用例如 Goz' code 进行迭代。)
您必须选择 DIB_RGB_COLORS
作为 uUsage
的标志,并设置 BITMAPINFO
structure 和它包含的 BITMAPINFOHEADER
structure。当您将 biClrUsed
和 biClrImportant
设置为零时,没有“无”颜色表,因此您可以读取从 GetDIBits
获得的位图的像素> 作为 RGB 值序列。使用32
作为位计数(biBitCount
)根据MSDN设置数据结构:
The bitmap has a maximum of 2^32 colors. If the
biCompression
member of theBITMAPINFOHEADER
isBI_RGB
, thebmiColors
member ofBITMAPINFO
isNULL
. EachDWORD
in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel. The high byte in eachDWORD
is not used.
因为 MS LONG
恰好是 32 位长(DWORD
的大小),您不必注意填充(如 Remarks section 中所述) .
代码:
HDC hdcSource = NULL; // the source device context
HBITMAP hSource = NULL; // the bitmap selected into the device context
BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcSource, hSource, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
{
// error handling
}
// create the pixel buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
// We'll change the received BITMAPINFOHEADER to request the data in a
// 32 bit RGB format (and not upside-down) so that we can iterate over
// the pixels easily.
// requesting a 32 bit image means that no stride/padding will be necessary,
// although it always contains an (possibly unused) alpha channel
MyBMInfo.bmiHeader.biBitCount = 32;
MyBMInfo.bmiHeader.biCompression = BI_RGB; // no compression -> easier to use
// correct the bottom-up ordering of lines (abs is in cstdblib and stdlib.h)
MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);
// Call GetDIBits a second time, this time to (format and) store the actual
// bitmap data (the "pixels") in the buffer lpPixels
if(0 == GetDIBits(hdcSource, hSource, 0, MyBMInfo.bmiHeader.biHeight,
lpPixels, &MyBMInfo, DIB_RGB_COLORS))
{
// error handling
}
// clean up: deselect bitmap from device context, close handles, delete buffer
关于c++ - GetDIBits 并使用 X、Y 遍历像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3688409/
我是一名优秀的程序员,十分优秀!