如何读取矩形区屏幕像素值到二维数组

muststudyhard 2018-01-23 11:48:46
没什么基础,搞了几天,Getdibits 参数不会用,求代码

我是用截屏读像素颜色值再加特征判断来识别数字的,要求速度要快,每秒读2次,GETpixel太慢了

也不要存图片,直接内存读或指针法的。

有的联系我,给红包
...全文
449 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
DIB就是逆序存储(底到顶)每行像素的,这个不影响访问效率,读取一块矩形区域的像素,你总得一行一行读,只是循环方向不同而已,除非矩形区域和整个buffer一样大会有一点影响,正序可以直接memcpy了(还要考虑目标数组是否和DIB一样每行像素做4字节对齐处理),逆序还得一行一行复制
li3307246695 2019-11-09
  • 打赏
  • 举报
回复
引用 5 楼 早打大打打核战争 的回复:
这代码真乱(疑似CSDN上某人写的 ),对windows GDI的理解不够,存在大量不必要的操作。建议:
1. 如果只读取少数几个像素,还不如直接GetPixel
2. 不要每次CreateCompatibleDC,只需要建立一次即可
3. 不要用CreateCompatibleBitmap,这个方法是charles petzold的经典《programming windows》系列的流毒,在win 3.x时代是非常正确的,对于win 9x和NT就已经过时了,只是后来者不求甚解,SDK编程照抄他那一套。要用CreateDIBSection(也只需要建立一次,不要反复建立),后者可以直接取到像素数据的指针,不用再次GetDIBits
你好我两个方法都用了,也得到指针数据了,,但是有个问题就是这个像素的起点都是从左下角开始的,我想要的是左上角开始的,考虑到自己手动移位址的话会影响效率,所以想请问还有其他的方法吗
不懂 就问 2018-09-25
  • 打赏
  • 举报
回复
好用 有帮助
  • 打赏
  • 举报
回复
这代码真乱(疑似CSDN上某人写的 ),对windows GDI的理解不够,存在大量不必要的操作。建议: 1. 如果只读取少数几个像素,还不如直接GetPixel 2. 不要每次CreateCompatibleDC,只需要建立一次即可 3. 不要用CreateCompatibleBitmap,这个方法是charles petzold的经典《programming windows》系列的流毒,在win 3.x时代是非常正确的,对于win 9x和NT就已经过时了,只是后来者不求甚解,SDK编程照抄他那一套。要用CreateDIBSection(也只需要建立一次,不要反复建立),后者可以直接取到像素数据的指针,不用再次GetDIBits
赵4老师 2018-01-23
  • 打赏
  • 举报
回复
IDirectDraw2::CreateSurface The IDirectDraw2::CreateSurface method creates a DirectDrawSurface object for this DirectDraw object. HRESULT CreateSurface( LPDDSURFACEDESC lpDDSurfaceDesc, LPDIRECTDRAWSURFACE FAR *lplpDDSurface, IUnknown FAR *pUnkOuter ); Parameters lpDDSurfaceDesc Address of the DDSURFACEDESC structure that describes the requested surface. You should set any unused members of DDSURFACEDESC to zero before calling this method. A DDSCAPS structure is a member of DDSURFACEDESC. lplpDDSurface Address of a pointer to be initialized with a valid DirectDrawSurface pointer if the call succeeds. pUnkOuter Allows for future compatibility with COM aggregation features. Presently, however, IDirectDraw2::CreateSurface returns an error if this parameter is anything but NULL. Return Values If the method succeeds, the return value is DD_OK. If the method fails, the return value may be one of the following error values: DDERR_INCOMPATIBLEPRIMARY DDERR_INVALIDCAPS DDERR_INVALIDOBJECT DDERR_INVALIDPARAMS DDERR_INVALIDPIXELFORMAT DDERR_NOALPHAHW DDERR_NOCOOPERATIVELEVELSET DDERR_NODIRECTDRAWHW DDERR_NOEMULATION DDERR_NOEXCLUSIVEMODE DDERR_NOFLIPHW DDERR_NOMIPMAPHW DDERR_NOOVERLAYHW DDERR_NOZBUFFERHW DDERR_OUTOFMEMORY DDERR_OUTOFVIDEOMEMORY DDERR_PRIMARYSURFACEALREADYEXISTS DDERR_UNSUPPORTEDMODE QuickInfo Windows NT: Use version 4.0 or later. Windows: Use Windows 95 or later. Available as a redistributable for Windows 95. Windows CE: Unsupported. Header: Declared in ddraw.h. Import Library: Use ddraw.lib.
赵4老师 2018-01-23
  • 打赏
  • 举报
回复
BitBlt The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context. BOOL BitBlt( HDC hdcDest, // handle to destination device context int nXDest, // x-coordinate of destination rectangle's upper-left // corner int nYDest, // y-coordinate of destination rectangle's upper-left // corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle HDC hdcSrc, // handle to source device context int nXSrc, // x-coordinate of source rectangle's upper-left // corner int nYSrc, // y-coordinate of source rectangle's upper-left // corner DWORD dwRop // raster operation code ); Parameters hdcDest Handle to the destination device context. nXDest Specifies the logical x-coordinate of the upper-left corner of the destination rectangle. nYDest Specifies the logical y-coordinate of the upper-left corner of the destination rectangle. nWidth Specifies the logical width of the source and destination rectangles. nHeight Specifies the logical height of the source and the destination rectangles. hdcSrc Handle to the source device context. nXSrc Specifies the logical x-coordinate of the upper-left corner of the source rectangle. nYSrc Specifies the logical y-coordinate of the upper-left corner of the source rectangle. dwRop Specifies a raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color. The following list shows some common raster operation codes: Value Description BLACKNESS Fills the destination rectangle using the color associated with index 0 in the physical palette. (This color is black for the default physical palette.) DSTINVERT Inverts the destination rectangle. MERGECOPY Merges the colors of the source rectangle with the specified pattern by using the Boolean AND operator. MERGEPAINT Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator. NOTSRCCOPY Copies the inverted source rectangle to the destination. NOTSRCERASE Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color. PATCOPY Copies the specified pattern into the destination bitmap. PATINVERT Combines the colors of the specified pattern with the colors of the destination rectangle by using the Boolean XOR operator. PATPAINT Combines the colors of the pattern with the colors of the inverted source rectangle by using the Boolean OR operator. The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator. SRCAND Combines the colors of the source and destination rectangles by using the Boolean AND operator. SRCCOPY Copies the source rectangle directly to the destination rectangle. SRCERASE Combines the inverted colors of the destination rectangle with the colors of the source rectangle by using the Boolean AND operator. SRCINVERT Combines the colors of the source and destination rectangles by using the Boolean XOR operator. SRCPAINT Combines the colors of the source and destination rectangles by using the Boolean OR operator. WHITENESS Fills the destination rectangle using the color associated with index 1 in the physical palette. (This color is white for the default physical palette.) Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. Windows NT: To get extended error information, callGetLastError. Remarks If a rotation or shear transformation is in effect in the source device context, BitBlt returns an error. If other transformations exist in the source device context (and a matching transformation is not in effect in the destination device context), the rectangle in the destination device context is stretched, compressed, or rotated as necessary. If the color formats of the source and destination device contexts do not match, the BitBlt function converts the source color format to match the destination format. When an enhanced metafile is being recorded, an error occurs if the source device context identifies an enhanced-metafile device context. Not all devices support the BitBlt function. For more information, see the RC_BITBLT raster capability entry in the GetDeviceCaps function as well as the following functions: MaskBlt, PlgBlt, StretchBlt. BitBlt returns an error if the source and destination device contexts represent different devices. ICM: No color management is performed when blits occur. Windows CE: In Windows CE version 1.0, the dwRop parameter can only be assigned the following values: SRCCOPY SRCAND SRCPAINT SRCINVERT In Windows CE version 2.0, the dwRop parameter can be any ROP3. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Bitmaps Overview, Bitmap Functions
muststudyhard 2018-01-23
  • 打赏
  • 举报
回复
网络上七平八凑的,运行后Getdibits这块出错,应该是位图信息头初始化这块没做好 #include<Windows.h> #include<iostream> #include<wingdi.h> using namespace std; //使用多字节字符集:项目>最后一项>配置属性>常规 里设置 void getrgb(LPRECT lpRect) { HDC hScrDC, hMemDC; //声明屏幕和内在设备描述表 HBITMAP hBitmap, hOldBitmap; //新老位图句柄 int nX, nY, nX2, nY2; //选定区域坐标 int nWidth, nHeight; //位图宽度和高度 int xScrn, yScrn; //屏幕分辨率 int colorcount; // 调色板项数 //if(IsRectEmpty(lpRect)) //确保选定区域不为空矩形 // return NULL; hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL); //为屏幕创建设备描述表 hMemDC = CreateCompatibleDC(hScrDC); //为屏幕设备描述表创建兼容的内存设备描述表 //GetDIBits nX = lpRect->left; //获得选定区域坐标 nY = lpRect->top; nX2 = lpRect->right; nY2 = lpRect->bottom; xScrn = GetDeviceCaps(hScrDC, HORZRES); //获得屏幕分辨率 yScrn = GetDeviceCaps(hScrDC, VERTRES); colorcount = GetDeviceCaps(hScrDC, NUMCOLORS); // 当前设备颜色项目数,超过8位返回-1,没有调色板。 if (nX<0) nX = 0; //确保选定区域是可见的 if (nY<0) nY = 0; if (nX2>xScrn) nX2 = xScrn; if (nY2>yScrn) nY2 = yScrn; nWidth = nX2 - nX; //获得位图的宽度和高度 nHeight = nY2 - nY; hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight); //创建一个与屏幕设备描述表兼容的位图 hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); // 把新位图选到内存设备描述表中 BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY); // 把屏幕设备描述表拷贝到内存设备描述表中 hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap); //得到屏幕位图的句柄 BITMAPINFO bitmapInfo = { 0 }; //BITMAPINFO结构有两个成员变量,分别是BITMAPINFOHEADER结构和RGBQUAD结构,   //BITMPINFOHEADER结构中包含了图像的一些基本信息,包括宽与高   //我们要先将biSize属性初始为BITMAPINFOHEADER结构的大小,以便GetDIBits函数在调用的时候填充其他的属性变量   bitmapInfo.bmiHeader.biSize= sizeof(BITMAPINFOHEADER); //将hBitmap选入到hMemDC中,通俗点说就是关联起来,对hMemDC的操作也就是对hBitmap操作,   //比如在hMemDC中写入文字在hBitmap中也能体现,常见给图片打水印可以用这个方法实现 GetDIBits(hMemDC, hBitmap, 0, nHeight, NULL, &bitmapInfo, DIB_RGB_COLORS); //根据第一次调用 GetDIBits函数完成后,填充到bitmapInfo中的图像占字节数大小来动态分配内存   //你也可以用图像的宽*高来得到图像所占字节数,但此方法有一点要说明 ,第一行的字节数必须是4的倍数,不够的用0补足。   //例:宽为923像素的图像,923/4 = 230(取整数部分),多出三个字节,这个时候我们就要补足四位,变成(230+1)*4 = 924 实际一行占的字节数   unsigned char *bitmapBits = new unsigned char[bitmapInfo.bmiHeader.biSizeImage]; memset(bitmapBits, 0, bitmapInfo.bmiHeader.biSizeImage);//初始为0   //第二次调用函数,并且第五个参数为有效的指针且可写入,   //函数调用后,将会把图像的每个像素点的RGB值 以16进制值写入到bitmapBits中,   //常见的一个像素是占4个字节,第一个字节是蓝色--B,第二个字节是绿色--G,第三个字节是红色--R,第四个不知道   GetDIBits(hMemDC, hBitmap, 0, nHeight, bitmapBits, &bitmapInfo, DIB_RGB_COLORS); SelectObject(hMemDC, hOldBitmap); //bitmapBits保存的就是屏幕的矩阵图像数据 //RGBQUAD rgb[3]; //GetDIBColorTable(hMemDC, 0, 3, rgb); //for (int i = 0; i<3; i++) { // printf("--%d", rgb[i].rgbGreen); //cout << rgb[i].rgbRed << "," << rgb[i].rgbGreen << "," << rgb[i].rgbBlue << endl; } //printf("%d %d" ,rgb[2].rgbGreen, rgb[1].rgbGreen); //int xx = rgb[2].rgbGreen; DeleteDC(hScrDC); //清除 DeleteDC(hMemDC); } int main() { LPRECT lpRect = new RECT(); //注意实体类型变量初始化 lpRect->left = 61; lpRect->top =776; lpRect->right = 165; lpRect->bottom = 780; getrgb(lpRect); system("pause"); return 0; }

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧