天马阁

 找回密码
 立即注册
                                        →→→→→→→→→→→→ 1点击查看所有VIP教程目录长列表(总教程数269个) 2办理VIP详情进入 ←←←←←←←←←←←←
1 x64CE与x64dbg入门基础教程 7课 已完结 2 x64汇编语言基础教程 16课 已完结 3 x64辅助入门基础教程 9课 已完结 4 C++x64内存辅助实战技术教程 149课 已完结
5 C++x64内存检测与过检测技术教程 10课 已完结 6 C+x64二叉树分析遍历与LUA自动登陆教程 19课已完结 7 C++BT功能原理与x64实战教程 29课 已完结 8 C+FPS框透视与自瞄x64实现原理及防护思路 30课完结
64驱?封? 9 64反驱? 10 64位V? 11 绝? 12 ???课?
13 64透 ? 14 64U ? 15 64Q ? 16 64功 ?
17 64U ? 18 64模 ? 19 64多 ? 20 64网 ?
21 64注 ? 22 64火 ? 23 64棋 ? 24 64自二链L?
25 64破 ? VIP会员办理QQ: 89986068   
【请先加好友,然后到好友列表双击联系客服办理,不然可能无法接受到信息。】
27 加入2000人交流群637034024 3 28 免责声明?
查看: 1138|回复: 0

VC++找图找色模块源码,已封装可以直接调用。

[复制链接]

8

主题

0

回帖

12

积分

编程入门

Rank: 1

天马币
36
发表于 2024-3-5 09:30:22 | 显示全部楼层 |阅读模式
VC找图找色模块源码,已封装可以直接调用。
#include "iostream.h"
char   *lpBufscr,*lpBufbmp;
BITMAP   bb,bm;//bb=屏幕  bm=图片
DWORD   dwX,dwY;
struct Points
{
POINT pts;
struct Points *next;
};
void GetScreenData(int dx,int dy)
{
HBITMAP   hBitmap,hOld;
HDC   hDC,hcDC;
BITMAPINFO   b;
HANDLE   hp;
// DWORD   dwX,dwY;
dwX=GetSystemMetrics(SM_CXSCREEN);
dwY=GetSystemMetrics(SM_CYSCREEN);
dwX=min(dwX,(unsigned)dx);
dwY=min(dwY,(unsigned)dy);
hDC=::GetWindowDC(NULL);
hcDC=CreateCompatibleDC(hDC);
hBitmap=CreateCompatibleBitmap(hDC,dwX,dwY);
hOld=(HBITMAP)SelectObject(hcDC,hBitmap);
BitBlt(hcDC,0,0,dwX,dwY,hDC,0,0,SRCCOPY);
bb.bmWidth=dwX;
bb.bmHeight=dwY;
bb.bmPlanes=1;
bb.bmWidthBytes=bb.bmWidth*3;
bb.bmBitsPixel=3;
bb.bmType=0;
b.bmiHeader.biSize   =   sizeof(BITMAPINFOHEADER);
b.bmiHeader.biWidth=dwX;
b.bmiHeader.biHeight=dwY;
b.bmiHeader.biPlanes   =   1;
b.bmiHeader.biBitCount   =24;
b.bmiHeader.biCompression   =   BI_RGB;
b.bmiHeader.biSizeImage   =   0;
b.bmiHeader.biXPelsPerMeter   =   0;
b.bmiHeader.biYPelsPerMeter   =   0;
b.bmiHeader.biClrUsed   =   0;
b.bmiHeader.biClrImportant   =   0;
b.bmiColors[0].rgbBlue=8;
b.bmiColors[0].rgbGreen=8;
b.bmiColors[0].rgbRed=8;
b.bmiColors[0].rgbReserved=0;
hp=GetProcessHeap();
lpBufscr=(char *)HeapAlloc(hp,HEAP_ZERO_MEMORY,bb.bmHeight*bb.bmWidth*4);
GetDIBits(hcDC,hBitmap,0,dwY,lpBufscr,&b,DIB_RGB_COLORS);
ReleaseDC(NULL,hDC);   
DeleteDC(hcDC);   
DeleteObject(hBitmap);   
DeleteObject(hOld);   
// HeapFree(hp,0,lpBufscr);
}
void GetBmpData(LPCTSTR lpszName)
{
HANDLE hp;
HDC hdc,hdcmem;
HBITMAP hBm;
BITMAPINFO   b;
hBm=(HBITMAP)LoadImage(NULL,/*"c:\\1.bmp"*/lpszName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm);
hdc=::GetWindowDC(NULL);
hdcmem=CreateCompatibleDC(hdc);
SelectObject(hdcmem,hBm);
BitBlt(hdcmem,0,0,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
b.bmiHeader.biSize   =   sizeof(BITMAPINFOHEADER);
b.bmiHeader.biWidth=bm.bmWidth;
b.bmiHeader.biHeight=bm.bmHeight;
b.bmiHeader.biPlanes   =   1;
b.bmiHeader.biBitCount   =24;
b.bmiHeader.biCompression   =   BI_RGB;
b.bmiHeader.biSizeImage   =   0;
b.bmiHeader.biXPelsPerMeter   =   0;
b.bmiHeader.biYPelsPerMeter   =   0;
b.bmiHeader.biClrUsed   =   0;
b.bmiHeader.biClrImportant   =   0;
b.bmiColors[0].rgbBlue=8;
b.bmiColors[0].rgbGreen=8;
b.bmiColors[0].rgbRed=8;
b.bmiColors[0].rgbReserved=0;
hp=GetProcessHeap();
lpBufbmp=(char *)HeapAlloc(hp,HEAP_ZERO_MEMORY,bm.bmHeight*bm.bmWidth*4);
GetDIBits(hdc,hBm,0,bm.bmHeight,lpBufbmp,&b,DIB_RGB_COLORS);
DeleteObject(&hBm);
DeleteDC(hdc);
DeleteDC(hdcmem);
}
//释放lpBuf
int ReleaseLP()
{
HANDLE hp=GetProcessHeap();
HeapFree(hp,0,lpBufscr);
HeapFree(hp,0,lpBufbmp);
return 0;
}

int GetScrPixel(int x,int y)//获取屏幕颜色
{
if(x<0 || x>(int)dwX || x<0 || x>(int)dwY)return-1;
BYTE   bBlue=lpBufscr[bb.bmWidthBytes*(dwY-y-1) + bb.bmBitsPixel*x + 0];
BYTE   bGreen=lpBufscr[bb.bmWidthBytes*(dwY-y-1) + bb.bmBitsPixel*x + 1];
BYTE   bRed=lpBufscr[bb.bmWidthBytes*(dwY-y-1) + bb.bmBitsPixel*x + 2];
COLORREF   color=RGB(bBlue,bGreen,bRed);
return int(color);
}
int GetBmpPixel(int x,int y)//获取位图颜色
{
if(x<0 || x>(int)dwX || x<0 || x>(int)dwY)return-1;
BYTE   bBlue=lpBufbmp[bm.bmWidthBytes*(bm.bmHeight-y-1) + bm.bmBitsPixel*x/8 + 0];
BYTE   bGreen=lpBufbmp[bm.bmWidthBytes*(bm.bmHeight-y-1) + bm.bmBitsPixel*x/8 + 1];
BYTE   bRed=lpBufbmp[bm.bmWidthBytes*(bm.bmHeight-y-1) + bm.bmBitsPixel*x/8 + 2];
COLORREF   color=RGB(bBlue,bGreen,bRed);
return int(color);
//return lpBufbmp[1];
}

//查找矩形区域内颜色值为 color的点
//从左往右逐行往下找
//找到返回1,并将坐标赋值给参数(fx,fy),找不到则返回0
int FindColor(int x1,int y1,int x2,int y2,int color,int &fx,int &fy)
{
GetScreenData(x2,y2);
fx=fy=-1;
int xtmp=x1;
for(;y1<y2;y1++)
{
  for(x1=xtmp;x1<=x2;x1++)
   {
    if(GetScrPixel(x1,y1)==color)
    {
     fx=x1;
     fy=y1;
     ReleaseLP();
     return 1;
    }
   }
}
ReleaseLP();
return 0;
}
POINT FindFirstPoint(int x1,int y1,int x2,int y2)
{
int color=GetBmpPixel(0,0);
POINT pt;
pt.x=-1;
pt.y=-1;
int fx,fy,xtmp=x1;;
fx=fy=-1;
for(;y1<y2;y1++)
  for(x1=xtmp;x1<x2;x1++)
   {
    if(GetScrPixel(x1,y1)==color)
    {
     pt.x=x1;
     pt.y=y1;
     return pt;
    }
   }
return pt;
}

//比较屏幕pt点位图矩形大小的所有颜色和位置是否与位图相同
int Compare(POINT pt)
{
int x,y;
if((unsigned)bm.bmHeight>dwY-pt.y)return 0;
for(y=0;y<bm.bmHeight-1;y++)
{
  for(x=0;x<bm.bmWidth-1;x++)
  {
   if(GetBmpPixel(x,y)==GetScrPixel(pt.x+x,pt.y+y))
   {
   
   
   }
   else
   {
    return 0;
   }
  }
}
return 1;
}
//从数据char *lpBufscr中查找*lpBufbmp;
int FindBmpBuf(int x1,int y1,int x2,int y2,int &fx,int &fy)
{
POINT pt,pttmp;
fx=-1;
fy=-1;
pt=FindFirstPoint(x1,y1,x2,y2);
if(Compare(pt)){fx=pt.x;fy=pt.y;return 1;}

else//出现第一个点找到后却并不与位图吻合
  {
   pttmp.y=pt.y+1;
   while(pt=FindFirstPoint(pt.x+1,pt.y,x2,pt.y+1) ,pt.x>=0)
   {
    if(Compare(pt)){fx=pt.x;fy=pt.y;return 1;}
   }
   while(pt=FindFirstPoint(x1,pttmp.y,x2,y2) ,pt.x>=0)
   {
    if(Compare(pt)){fx=pt.x;fy=pt.y;return 1;}
    else
    {
     pttmp.y=pt.y+1;
     while(pt=FindFirstPoint(pt.x+1,pt.y,x2,pt.y+1) ,pt.x>=0)
      if(Compare(pt)){fx=pt.x;fy=pt.y;return 1;}
    }
   }
  }
return 0;
}
//从矩形x1,y1,x2,y2内查找与位图lpszName完全匹配的起始点
//若找到,(fx,fy)为图片第一次出现的左上角顶点,并返回1
//若找不到,(fx,fy)=(-1,-1)并返回0
int FindBmp(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int &fx,int &fy)
{
GetBmpData(lpszName);
GetScreenData(x2,y2);
FindBmpBuf(x1,y1,x2,y2,fx,fy);
ReleaseLP();
if(fx>0)return 1;
return 0;
}
//比较屏幕pt点位图矩形大小的所有颜色和位置是否与位图条件匹配
//位图backcolor颜色点忽略,颜色偏差errorcolor,允许不匹配点的个数errorcount
//比较成功返回1,否则返回0
int CompareEx(POINT pt,int backcolor,int errorcolor,int errorcount)
{
int x,y,colorB,colorS,count=0;
if((unsigned)bm.bmHeight>dwY-pt.y)return 0;
for(y=0;y<bm.bmHeight-1;y++)
  for(x=0;x<bm.bmWidth-1;x++)
  {
   colorB=GetBmpPixel(x,y);
   colorS=GetScrPixel(pt.x+x,pt.y+y);
   if(colorB==colorS || colorB==backcolor);
   else
   {
    if( abs(GetRValue((COLORREF)colorB)-GetRValue((COLORREF)colorS) > GetRValue((COLORREF)errorcolor))
     || abs(GetGValue((COLORREF)colorB)-GetGValue((COLORREF)colorS) > GetGValue((COLORREF)errorcolor))
     || abs(GetBValue((COLORREF)colorB)-GetBValue((COLORREF)colorS) > GetBValue((COLORREF)errorcolor)) ){
     count++;
    }
   }
  }
if(count>errorcount)
  return 0;
else
  return 1;
}
//从矩形x1,y1,x2,y2内查找与位图lpszName条件匹配的起始点
//若有匹配的,(fx,fy)为图片第一次出现的左上角顶点,并返回匹配个数
//忽略位图backcolor颜色点,颜色偏差errorcolor,允许不匹配点的个数errorcount
int FindBmpEx(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int backcolor,int errorcolor,int errorcount,int &fx,int &fy)
{
int x,y,color=-1;
GetBmpData(lpszName);
GetScreenData(x2,y2);
for(y=0;y<bm.bmHeight-1;y++)
{
  for(x=0;x<bm.bmWidth-1;x++)
   if(GetBmpPixel(x,y)!=errorcount)
   {
    color=GetBmpPixel(x,y);
    break;
   }
  if(color>0)break;
}
Points *head,*tail,*p,*q;
head=tail=NULL;
int xtmp=x1,countP=0,countB=0;
for(;y1<y2;y1++)
  for(x1=xtmp;x1<=x2;x1++)
   if(GetScrPixel(x1,y1)==color)
    {
     countP++;
     p=(struct Points *)malloc(sizeof(struct Points));
     (p->pts).x=x1-x;
     (p->pts).y=y1-y;
     if(head==NULL)q=head=tail=p;
     else
      tail=tail->next;
     tail->next=p;
    }
p->next=NULL;
while(q)
{
  if(CompareEx((q->pts),backcolor,errorcolor,errorcount))
  {
   countB++;
   if(countB==1)
   {
    fx=(q->pts).x;
    fy=(q->pts).y;
   }
  }
  p=q->next;
  free(q);
  q=p;
}
ReleaseLP();
return countB;
}

[url=http://www.70pv.com/]传奇sf一条龙服务端[/url]
回复

使用道具 举报

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

本版积分规则

天马阁|C/C++辅助教程|安卓逆向安全| 论坛导航|免责申明|Archiver||网站地图
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表天马阁立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2010-2021 All Right Reserved.
快速回复 返回顶部 返回列表