123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "StdAfx.h"
- #include "OcrScreen.h"
- #include "common_funcs.h"
- OcrScreen::OcrScreen()
- {
- }
- OcrScreen::OcrScreen(SIZE maxsize) :OcrBase(maxsize)
- {
- m_CurrentSize = maxsize;
- }
- OcrScreen::OcrScreen(PBYTE pBits,SIZE maxsize):OcrBase(pBits,maxsize)
- {
- m_CurrentSize = maxsize;
- }
- OcrScreen::~OcrScreen(void)
- {
- }
- void OcrScreen::UpdateScreen(HWND handle)
- {
- RECT rc;
- SIZE size;
- GetWindowRect(handle, &rc);
- size.cx = rc.right - rc.left + 1;
- size.cy = rc.bottom - rc.top + 1;
- if(m_pBits == NULL)
- {
- m_LimitedSize = size;
- m_CurrentSize = size;
- m_PixesCount = size.cx*size.cy;
- m_pBits = new BYTE[size.cx*size.cy*sizeof(OCRCOLOR)];
- }
- if(!(m_LimitedSize.cx >= size.cx && m_LimitedSize.cy >= size.cy))
- {
- delete []m_pBits;
- m_LimitedSize = size;
- m_CurrentSize = size;
- m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
- m_pBits = new BYTE[size.cx*size.cy*sizeof(COLORPOINT)];
- }
- void *pBitBits = NULL;
- HDC hdcScreen = GetWindowDC(handle);
- HDC hdc = CreateCompatibleDC(hdcScreen);
- BITMAPINFO bmpInfo;
- ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
- bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
- bmpInfo.bmiHeader.biBitCount=32;
- bmpInfo.bmiHeader.biCompression = BI_RGB;
- bmpInfo.bmiHeader.biWidth=size.cx;
- bmpInfo.bmiHeader.biHeight=size.cy;
- bmpInfo.bmiHeader.biPlanes=1;
- bmpInfo.bmiHeader.biSizeImage=0;//size.cx*size.cy*32/8;
- HBITMAP bmp = CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,(void **)&pBitBits,NULL,0);
-
- HBITMAP oldbmp = (HBITMAP )::SelectObject(hdc,bmp);
- SendMessage(handle,WM_SETREDRAW,TRUE,0);
- PrintWindow(handle, hdc,0);
- memcpy(m_pBits,pBitBits,size.cx*size.cy*4);
- TrimTheA(m_pBits,size.cx,size.cy);
- ::SelectObject(hdc,oldbmp);
- DeleteDC(hdc);
- ReleaseDC(handle,hdcScreen);
-
- }
- void OcrScreen::UpdateScreen(PBYTE pBits,SIZE size)
- {
- if(m_LimitedSize.cx >= size.cx && m_LimitedSize.cy >= size.cy)
- {
- m_CurrentSize = size;
- m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
- memcpy(m_pBits,pBits,m_CurrentSize.cx*m_CurrentSize.cy*sizeof(OCRCOLOR));
- return;
- }
- delete []m_pBits;
- m_LimitedSize = size;
- m_CurrentSize = size;
- m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
- m_pBits = new BYTE[size.cx*size.cy*sizeof(COLORPOINT)];
- memcpy(m_pBits,pBits,size.cx*size.cy*sizeof(COLORPOINT));
- }
- void OcrScreen::GetPointColor(COLORPOINT &cpt)
- {
- //adjust mem
- DWORD StartBitsPoint;
- //do the loop
- if(cpt.pt.x >= m_CurrentSize.cx || cpt.pt.y >= m_CurrentSize.cy)
- {
- cpt.color = 0xFFFFFFFF;
- return;
- }
- StartBitsPoint = (((m_CurrentSize.cx)*(m_CurrentSize.cy - cpt.pt.y - 1) + cpt.pt.x)*4);
- memcpy(&(cpt.color),m_pBits + StartBitsPoint,sizeof(OCRCOLOR));
- }
- void OcrScreen::SaveTheAreaToBitMapFileWithName(TCHAR *pszFullFileName)
- {
- SaveImageDataToBMP(pszFullFileName,m_CurrentSize.cx,m_CurrentSize.cy,32,m_pBits);
- //SaveBitmap32To24(pszFullFileName,m_pBits,m_CurrentSize.cx,m_CurrentSize.cy);
- }
- void OcrScreen::SaveTheAreaToBitMapFile(RECT &area)
- {
- POINT offset;
- SIZE size;
- DWORD StartBitsPoint;
- COLORPOINT cpt;
- size.cx = GetRectWidth(area);
- size.cy = GetRectHeight(area);
- INT yu = size.cx % 4;
- if(yu != 0)
- {
- size.cx = (size.cx - yu) + 4;
- }
- PBYTE pBits = new BYTE[size.cx*size.cy*4];
- memset(pBits,0,size.cx*size.cy*4);
- area.right = area.left + size.cx;
- for(int i = area.left;i < area.right;i++)
- {
- for(int j = area.top;j < area.bottom;j++)
- {
- cpt.pt.x = i;
- cpt.pt.y = j;
- GetPointColor(cpt);
- offset.x = cpt.pt.x - area.left;
- offset.y = cpt.pt.y - area.top;
- StartBitsPoint = (size.cx * (size.cy - offset.y - 1) + offset.x) * 4;
- memcpy(pBits + StartBitsPoint,&(cpt.color),sizeof(OCRCOLOR));
- }
- }
- SaveBitmap32To24("./DumpAreaBitmap.bmp", pBits, GetRectWidth(area), GetRectHeight(area));
- SaveBitmap32To24("./DumpFullBitmap.bmp",m_pBits,m_CurrentSize.cx,m_CurrentSize.cy);
- delete []pBits;
- }
|