OcrScreen.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "StdAfx.h"
  2. #include "OcrScreen.h"
  3. #include "common_funcs.h"
  4. OcrScreen::OcrScreen()
  5. {
  6. }
  7. OcrScreen::OcrScreen(SIZE maxsize) :OcrBase(maxsize)
  8. {
  9. m_CurrentSize = maxsize;
  10. }
  11. OcrScreen::OcrScreen(PBYTE pBits,SIZE maxsize):OcrBase(pBits,maxsize)
  12. {
  13. m_CurrentSize = maxsize;
  14. }
  15. OcrScreen::~OcrScreen(void)
  16. {
  17. }
  18. void OcrScreen::UpdateScreen(HWND handle)
  19. {
  20. RECT rc;
  21. SIZE size;
  22. GetWindowRect(handle, &rc);
  23. size.cx = rc.right - rc.left + 1;
  24. size.cy = rc.bottom - rc.top + 1;
  25. if(m_pBits == NULL)
  26. {
  27. m_LimitedSize = size;
  28. m_CurrentSize = size;
  29. m_PixesCount = size.cx*size.cy;
  30. m_pBits = new BYTE[size.cx*size.cy*sizeof(OCRCOLOR)];
  31. }
  32. if(!(m_LimitedSize.cx >= size.cx && m_LimitedSize.cy >= size.cy))
  33. {
  34. delete []m_pBits;
  35. m_LimitedSize = size;
  36. m_CurrentSize = size;
  37. m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
  38. m_pBits = new BYTE[size.cx*size.cy*sizeof(COLORPOINT)];
  39. }
  40. void *pBitBits = NULL;
  41. HDC hdcScreen = GetWindowDC(handle);
  42. HDC hdc = CreateCompatibleDC(hdcScreen);
  43. BITMAPINFO bmpInfo;
  44. ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  45. bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  46. bmpInfo.bmiHeader.biBitCount=32;
  47. bmpInfo.bmiHeader.biCompression = BI_RGB;
  48. bmpInfo.bmiHeader.biWidth=size.cx;
  49. bmpInfo.bmiHeader.biHeight=size.cy;
  50. bmpInfo.bmiHeader.biPlanes=1;
  51. bmpInfo.bmiHeader.biSizeImage=0;//size.cx*size.cy*32/8;
  52. HBITMAP bmp = CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,(void **)&pBitBits,NULL,0);
  53. HBITMAP oldbmp = (HBITMAP )::SelectObject(hdc,bmp);
  54. SendMessage(handle,WM_SETREDRAW,TRUE,0);
  55. PrintWindow(handle, hdc,0);
  56. memcpy(m_pBits,pBitBits,size.cx*size.cy*4);
  57. TrimTheA(m_pBits,size.cx,size.cy);
  58. ::SelectObject(hdc,oldbmp);
  59. DeleteDC(hdc);
  60. ReleaseDC(handle,hdcScreen);
  61. }
  62. void OcrScreen::UpdateScreen(PBYTE pBits,SIZE size)
  63. {
  64. if(m_LimitedSize.cx >= size.cx && m_LimitedSize.cy >= size.cy)
  65. {
  66. m_CurrentSize = size;
  67. m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
  68. memcpy(m_pBits,pBits,m_CurrentSize.cx*m_CurrentSize.cy*sizeof(OCRCOLOR));
  69. return;
  70. }
  71. delete []m_pBits;
  72. m_LimitedSize = size;
  73. m_CurrentSize = size;
  74. m_PixesCount = m_CurrentSize.cx*m_CurrentSize.cy;
  75. m_pBits = new BYTE[size.cx*size.cy*sizeof(COLORPOINT)];
  76. memcpy(m_pBits,pBits,size.cx*size.cy*sizeof(COLORPOINT));
  77. }
  78. void OcrScreen::GetPointColor(COLORPOINT &cpt)
  79. {
  80. //adjust mem
  81. DWORD StartBitsPoint;
  82. //do the loop
  83. if(cpt.pt.x >= m_CurrentSize.cx || cpt.pt.y >= m_CurrentSize.cy)
  84. {
  85. cpt.color = 0xFFFFFFFF;
  86. return;
  87. }
  88. StartBitsPoint = (((m_CurrentSize.cx)*(m_CurrentSize.cy - cpt.pt.y - 1) + cpt.pt.x)*4);
  89. memcpy(&(cpt.color),m_pBits + StartBitsPoint,sizeof(OCRCOLOR));
  90. }
  91. void OcrScreen::SaveTheAreaToBitMapFileWithName(TCHAR *pszFullFileName)
  92. {
  93. SaveImageDataToBMP(pszFullFileName,m_CurrentSize.cx,m_CurrentSize.cy,32,m_pBits);
  94. //SaveBitmap32To24(pszFullFileName,m_pBits,m_CurrentSize.cx,m_CurrentSize.cy);
  95. }
  96. void OcrScreen::SaveTheAreaToBitMapFile(RECT &area)
  97. {
  98. POINT offset;
  99. SIZE size;
  100. DWORD StartBitsPoint;
  101. COLORPOINT cpt;
  102. size.cx = GetRectWidth(area);
  103. size.cy = GetRectHeight(area);
  104. INT yu = size.cx % 4;
  105. if(yu != 0)
  106. {
  107. size.cx = (size.cx - yu) + 4;
  108. }
  109. PBYTE pBits = new BYTE[size.cx*size.cy*4];
  110. memset(pBits,0,size.cx*size.cy*4);
  111. area.right = area.left + size.cx;
  112. for(int i = area.left;i < area.right;i++)
  113. {
  114. for(int j = area.top;j < area.bottom;j++)
  115. {
  116. cpt.pt.x = i;
  117. cpt.pt.y = j;
  118. GetPointColor(cpt);
  119. offset.x = cpt.pt.x - area.left;
  120. offset.y = cpt.pt.y - area.top;
  121. StartBitsPoint = (size.cx * (size.cy - offset.y - 1) + offset.x) * 4;
  122. memcpy(pBits + StartBitsPoint,&(cpt.color),sizeof(OCRCOLOR));
  123. }
  124. }
  125. SaveBitmap32To24("./DumpAreaBitmap.bmp", pBits, GetRectWidth(area), GetRectHeight(area));
  126. SaveBitmap32To24("./DumpFullBitmap.bmp",m_pBits,m_CurrentSize.cx,m_CurrentSize.cy);
  127. delete []pBits;
  128. }