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

[复制链接]
查看11 | 回复7 | 2021-1-27 05:49:06 | 显示全部楼层 |阅读模式
没什么基础,搞了几天,Getdibits参数不会用,求代码
我是用截屏读像素颜色值再加特征判断来识别数字的,要求速度要快,每秒读2次,GETpixel太慢了
也不要存图片,直接内存读或指针法的。
有的联系我,给红包
分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
BitBlt
TheBitBltfunctionperformsabit-blocktransferofthecolordatacorrespondingtoarectangleofpixelsfromthespecifiedsourcedevicecontextintoadestinationdevicecontext.
BOOLBitBlt(
HDChdcDest,//handletodestinationdevicecontext
intnXDest,//x-coordinateofdestinationrectangle'supper-left
//corner
intnYDest,//y-coordinateofdestinationrectangle'supper-left
//corner
intnWidth,//widthofdestinationrectangle
intnHeight,//heightofdestinationrectangle
HDChdcSrc,//handletosourcedevicecontext
intnXSrc,//x-coordinateofsourcerectangle'supper-left
//corner
intnYSrc,//y-coordinateofsourcerectangle'supper-left
//corner
DWORDdwRop//rasteroperationcode
);
Parameters
hdcDest
Handletothedestinationdevicecontext.
nXDest
Specifiesthelogicalx-coordinateoftheupper-leftcornerofthedestinationrectangle.
nYDest
Specifiesthelogicaly-coordinateoftheupper-leftcornerofthedestinationrectangle.
nWidth
Specifiesthelogicalwidthofthesourceanddestinationrectangles.
nHeight
Specifiesthelogicalheightofthesourceandthedestinationrectangles.
hdcSrc
Handletothesourcedevicecontext.
nXSrc
Specifiesthelogicalx-coordinateoftheupper-leftcornerofthesourcerectangle.
nYSrc
Specifiesthelogicaly-coordinateoftheupper-leftcornerofthesourcerectangle.
dwRop
Specifiesaraster-operationcode.Thesecodesdefinehowthecolordataforthesourcerectangleistobecombinedwiththecolordataforthedestinationrectangletoachievethefinalcolor.
Thefollowinglistshowssomecommonrasteroperationcodes:ValueDescription
BLACKNESSFillsthedestinationrectangleusingthecolorassociatedwithindex0inthephysicalpalette.(Thiscolorisblackforthedefaultphysicalpalette.)
DSTINVERTInvertsthedestinationrectangle.
MERGECOPYMergesthecolorsofthesourcerectanglewiththespecifiedpatternbyusingtheBooleanANDoperator.
MERGEPAINTMergesthecolorsoftheinvertedsourcerectanglewiththecolorsofthedestinationrectanglebyusingtheBooleanORoperator.
NOTSRCCOPYCopiestheinvertedsourcerectangletothedestination.
NOTSRCERASECombinesthecolorsofthesourceanddestinationrectanglesbyusingtheBooleanORoperatorandtheninvertstheresultantcolor.
PATCOPYCopiesthespecifiedpatternintothedestinationbitmap.
PATINVERTCombinesthecolorsofthespecifiedpatternwiththecolorsofthedestinationrectanglebyusingtheBooleanXORoperator.
PATPAINTCombinesthecolorsofthepatternwiththecolorsoftheinvertedsourcerectanglebyusingtheBooleanORoperator.TheresultofthisoperationiscombinedwiththecolorsofthedestinationrectanglebyusingtheBooleanORoperator.
SRCANDCombinesthecolorsofthesourceanddestinationrectanglesbyusingtheBooleanANDoperator.
SRCCOPYCopiesthesourcerectangledirectlytothedestinationrectangle.
SRCERASECombinestheinvertedcolorsofthedestinationrectanglewiththecolorsofthesourcerectanglebyusingtheBooleanANDoperator.
SRCINVERTCombinesthecolorsofthesourceanddestinationrectanglesbyusingtheBooleanXORoperator.
SRCPAINTCombinesthecolorsofthesourceanddestinationrectanglesbyusingtheBooleanORoperator.
WHITENESSFillsthedestinationrectangleusingthecolorassociatedwithindex1inthephysicalpalette.(Thiscoloriswhiteforthedefaultphysicalpalette.)

ReturnValues
Ifthefunctionsucceeds,thereturnvalueisnonzero.
Ifthefunctionfails,thereturnvalueiszero.
WindowsNT:Togetextendederrorinformation,callGetLastError.
Remarks
Ifarotationorsheartransformationisineffectinthesourcedevicecontext,BitBltreturnsanerror.Ifothertransformationsexistinthesourcedevicecontext(andamatchingtransformationisnotineffectinthedestinationdevicecontext),therectangleinthedestinationdevicecontextisstretched,compressed,orrotatedasnecessary.
Ifthecolorformatsofthesourceanddestinationdevicecontextsdonotmatch,theBitBltfunctionconvertsthesourcecolorformattomatchthedestinationformat.
Whenanenhancedmetafileisbeingrecorded,anerroroccursifthesourcedevicecontextidentifiesanenhanced-metafiledevicecontext.
NotalldevicessupporttheBitBltfunction.Formoreinformation,seetheRC_BITBLTrastercapabilityentryintheGetDeviceCapsfunctionaswellasthefollowingfunctions:MaskBlt,PlgBlt,StretchBlt.
BitBltreturnsanerrorifthesourceanddestinationdevicecontextsrepresentdifferentdevices.
ICM:Nocolormanagementisperformedwhenblitsoccur.
WindowsCE:InWindowsCEversion1.0,thedwRopparametercanonlybeassignedthefollowingvalues:
SRCCOPY
SRCAND
SRCPAINT
SRCINVERT
InWindowsCEversion2.0,thedwRopparametercanbeanyROP3.
QuickInfo
WindowsNT:Requiresversion3.1orlater.
Windows:RequiresWindows95orlater.
WindowsCE:Requiresversion1.0orlater.
Header:Declaredinwingdi.h.
ImportLibrary:Usegdi32.lib.
SeeAlso
BitmapsOverview,BitmapFunctions


回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
IDirectDraw2::CreateSurface
TheIDirectDraw2::CreateSurfacemethodcreatesaDirectDrawSurfaceobjectforthisDirectDrawobject.
HRESULTCreateSurface(
LPDDSURFACEDESClpDDSurfaceDesc,
LPDIRECTDRAWSURFACEFAR*lplpDDSurface,
IUnknownFAR*pUnkOuter
);
Parameters
lpDDSurfaceDesc
AddressoftheDDSURFACEDESCstructurethatdescribestherequestedsurface.YoushouldsetanyunusedmembersofDDSURFACEDESCtozerobeforecallingthismethod.ADDSCAPSstructureisamemberofDDSURFACEDESC.
lplpDDSurface
AddressofapointertobeinitializedwithavalidDirectDrawSurfacepointerifthecallsucceeds.
pUnkOuter
AllowsforfuturecompatibilitywithCOMaggregationfeatures.Presently,however,IDirectDraw2::CreateSurfacereturnsanerrorifthisparameterisanythingbutNULL.
ReturnValues
Ifthemethodsucceeds,thereturnvalueisDD_OK.
Ifthemethodfails,thereturnvaluemaybeoneofthefollowingerrorvalues:
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
WindowsNT:Useversion4.0orlater.
Windows:UseWindows95orlater.AvailableasaredistributableforWindows95.
WindowsCE:Unsupported.
Header:Declaredinddraw.h.
ImportLibrary:Useddraw.lib.


回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
网络上七平八凑的,运行后Getdibits这块出错,应该是位图信息头初始化这块没做好
#include
#include
#include
usingnamespacestd;
//使用多字节字符集:项目>最后一项>配置属性>常规里设置
voidgetrgb(LPRECTlpRect)
{
HDChScrDC,hMemDC;//声明屏幕和内在设备描述表
HBITMAPhBitmap,hOldBitmap;//新老位图句柄
intnX,nY,nX2,nY2;//选定区域坐标
intnWidth,nHeight;//位图宽度和高度
intxScrn,yScrn;//屏幕分辨率
intcolorcount;//调色板项数
//if(IsRectEmpty(lpRect))//确保选定区域不为空矩形
//returnNULL;
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(nXxScrn)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);
//得到屏幕位图的句柄
BITMAPINFObitmapInfo={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 实际一行占的字节数
unsignedchar*bitmapBits=newunsignedchar[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保存的就是屏幕的矩阵图像数据
//RGBQUADrgb[3];
//GetDIBColorTable(hMemDC,0,3,rgb);

//for(inti=0;ileft=61;
lpRect->top=776;
lpRect->right=165;
lpRect->bottom=780;
getrgb(lpRect);
system("pause");
return0;
}
回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
这代码真乱(疑似CSDN上某人写的

),对windowsGDI的理解不够,存在大量不必要的操作。建议:
1.如果只读取少数几个像素,还不如直接GetPixel
2.不要每次CreateCompatibleDC,只需要建立一次即可
3.不要用CreateCompatibleBitmap,这个方法是charlespetzold的经典《programmingwindows》系列的流毒,在win3.x时代是非常正确的,对于win9x和NT就已经过时了,只是后来者不求甚解,SDK编程照抄他那一套。要用CreateDIBSection(也只需要建立一次,不要反复建立),后者可以直接取到像素数据的指针,不用再次GetDIBits

回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
好用有帮助

回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
引用5楼早打大打打核战争的回复:这代码真乱(疑似CSDN上某人写的

),对windowsGDI的理解不够,存在大量不必要的操作。建议:
1.如果只读取少数几个像素,还不如直接GetPixel
2.不要每次CreateCompatibleDC,只需要建立一次即可
3.不要用CreateCompatibleBitmap,这个方法是charlespetzold的经典《programmingwindows》系列的流毒,在win3.x时代是非常正确的,对于win9x和NT就已经过时了,只是后来者不求甚解,SDK编程照抄他那一套。要用CreateDIBSection(也只需要建立一次,不要反复建立),后者可以直接取到像素数据的指针,不用再次GetDIBits
你好我两个方法都用了,也得到指针数据了,,但是有个问题就是这个像素的起点都是从左下角开始的,我想要的是左上角开始的,考虑到自己手动移位址的话会影响效率,所以想请问还有其他的方法吗
回复

使用道具 举报

千问 | 2021-1-27 05:49:06 | 显示全部楼层
DIB就是逆序存储(底到顶)每行像素的,这个不影响访问效率,读取一块矩形区域的像素,你总得一行一行读,只是循环方向不同而已,除非矩形区域和整个buffer一样大会有一点影响,正序可以直接memcpy了(还要考虑目标数组是否和DIB一样每行像素做4字节对齐处理),逆序还得一行一行复制
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行