#include "THSActionUtil.h"
|
#include "ImgDivider.h"
|
#include "Win32Util.h"
|
|
RecognitionManager* THSActionUtil::recognitionManager;
|
|
bool isMainScreen(string str) {
|
if (str.find("ͬ»¨Ë³(") != string::npos) {
|
cout << str << endl;
|
if (str.find("¸±ÆÁ") == string::npos) {
|
if (str.find("ÈÈÃŹÉ") != string::npos || str.find("Îҵİå¿é") != string::npos) {
|
return true;
|
}
|
}
|
}
|
return false;
|
}
|
|
bool isSecondScreen(string str) {
|
return str.find("ͬ»¨Ë³(") != string::npos && str.find("¸±ÆÁ") != string::npos;
|
}
|
|
std::list<cv::Mat> splitGPCodeNum(cv::Mat img) {
|
std::list<cv::Mat> nums = ImgUtil::splitNum(img, 96);
|
if (nums.size() != 6) {
|
int retryCount = 0;
|
while (retryCount < 4) {
|
retryCount++;
|
nums = ImgUtil::splitNum(img, 96 + retryCount * 10);
|
if (nums.size() == 6) {
|
break;
|
}
|
}
|
}
|
if (nums.size() != 6) {
|
std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append(std::to_string(rand())).append("_listen.jpg");
|
imwrite(path, img);
|
throw string("´úÂëÊý×Ö·Ö¸ô³ö´í");
|
}
|
return nums;
|
}
|
|
|
|
//»ñÈ¡¸±ÆÁ
|
HWND getSecondWindow() {
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
string str = Win32Util::getWindowName(hwnd);
|
if (isSecondScreen(str))
|
{
|
return hwnd;
|
}
|
}
|
return 0;
|
}
|
|
HWND THSActionUtil::getL2Win() {
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
string str = Win32Util::getWindowName(hwnd);
|
|
|
if (isMainScreen(str)) {
|
return hwnd;
|
}
|
|
}
|
return 0;
|
}
|
bool THSActionUtil::checkEnv() {
|
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
list<HWND>::iterator ele;
|
bool fp = false;
|
bool zp = false;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
string str = Win32Util::getWindowName(hwnd);
|
if (isSecondScreen(str))
|
{
|
fp = true;
|
}
|
|
if (isMainScreen(str)) {
|
zp = true;
|
}
|
|
}
|
|
if (!zp) {
|
throw string("ͬ»¨Ë³Ö÷ÆÁL2´°¿Úδ´ò¿ª");
|
}
|
|
|
if (!fp) {
|
throw string("ͬ»¨Ë³¸±ÆÁûÓдò¿ª");
|
}
|
|
return true;
|
}
|
|
void THSActionUtil::openSecondScreen() {
|
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
|
bool open = false;
|
HWND mainPage = 0;
|
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
string str = Win32Util::getWindowName(hwnd);
|
if (isSecondScreen(str))
|
{
|
cout << str << endl;
|
SetForegroundWindow(hwnd);
|
SetFocus(hwnd);
|
open = true;
|
break;
|
}
|
|
if (isMainScreen(str)) {
|
cout << hwnd << endl;
|
//»ñÈ¡³ß´ç
|
RECT rc;
|
GetWindowRect(hwnd, &rc);
|
if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) {
|
mainPage = hwnd;
|
}
|
}
|
|
}
|
|
if (!open) {
|
if (mainPage <= 0) {
|
throw string("δÕÒµ½Ê×Ò³");
|
}
|
|
Win32Util::focus(mainPage);
|
//²éÕÒÖ÷´°¿ÚµÄ¹¤¾ßÀ¸
|
HWND tool = FindWindowExA(mainPage, NULL, "AfxControlBar100s", NULL);
|
|
//»ñÈ¡µÚÒ»¸ö×Ó´°¿Ú
|
tool = FindWindowExA(tool, NULL, NULL, NULL);
|
cout << "²Ù×÷À¸£º" << tool << endl;
|
//´ò¿ª¸±ÆÁ£¬¶à´°°´Å¥IDΪ:0x00007AF9
|
HWND btn = GetDlgItem(tool, 0x00007AF9);
|
RECT rc;
|
GetWindowRect(btn, &rc);
|
//»ñÈ¡ÐèÒªµã»÷µÄλÖÃ
|
int x = rc.left + (rc.right - rc.left) / 3;
|
int y = rc.top + (rc.bottom - rc.top) / 3;
|
//µã»÷¶à´°
|
Win32Util::click(x, y);
|
//µã»÷¸±ÆÁ1
|
Win32Util::click(x + 10, y + 21 * 7 + 5, 500);
|
}
|
}
|
//Ìí¼Ó¹ÉƱ
|
bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList) {
|
|
list<std::string> tempCodeList;
|
for (std::list<string>::iterator e = codeList.begin();e != codeList.end();e++) {
|
tempCodeList.push_back(*e);
|
}
|
|
//´ò¿ª¸±ÆÁ
|
HWND sw = getSecondWindow();
|
if (sw <= 0)
|
{
|
openSecondScreen();
|
Sleep(2000);
|
sw = getSecondWindow();
|
}
|
if (sw <= 0) {
|
throw string("δ´ò¿ª¸±ÆÁ");
|
}
|
|
//´ò¿ª°å¿é
|
RECT rect;
|
GetWindowRect(sw, &rect);
|
Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1);
|
//Win32Util::click(10);
|
Win32Util::focus(sw);
|
Win32Util::keyboardNum(quickCode, 200);
|
Win32Util::keyboard(VK_RETURN, 1000);
|
Sleep(2000);
|
//ÉèÖðå¿éÖÐµÄ¹ÉÆ±
|
|
//»ñÈ¡°å¿éÄÚÈݾä±ú
|
|
HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL);
|
cv::Mat oimg = CaptureUtil::capture(content);
|
cout << "¸±ÆÁ½ØÍ¼:" << oimg.rows << "-" << oimg.cols << endl;
|
|
if (oimg.cols <= 0 || oimg.rows <= 0) {
|
throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ");
|
}
|
|
cv::Mat img = ImgUtil::grayImage(oimg);
|
oimg.release();
|
std::list<GPCodeArea> areaList = recognitionGPArea(img);
|
|
std::list<string> addList;
|
std::list<GPCodeArea> delList;
|
std::list<GPCodeArea> resultList = recognitionNum(img, areaList);
|
|
for (std::list<GPCodeArea>::reverse_iterator ele = resultList.rbegin();ele != resultList.rend();++ele) {
|
|
bool contains = false;
|
std::list<string>::iterator e;
|
for (e = tempCodeList.begin();e != tempCodeList.end();e++) {
|
if (*e == (*ele).code) {
|
contains = true;
|
break;
|
}
|
}
|
|
if (!contains) {
|
delList.push_back(*ele);
|
}
|
else
|
{
|
tempCodeList.remove((*ele).code);
|
}
|
|
}
|
//-----ÏÈɾ³ýÐèҪɾ³ýµÄ
|
//»ñÈ¡ÄÚÈݰå¿é×ø±ê
|
|
|
GetWindowRect(sw, &rect);
|
Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1);
|
Win32Util::click(10);
|
|
for (std::list<GPCodeArea>::iterator ele = delList.begin();ele != delList.end();++ele) {
|
RECT rect;
|
GetWindowRect(content, &rect);
|
Win32Util::focus(sw);
|
int x = rect.left;
|
int y = rect.top;
|
x += (*ele).startx + 5;
|
y += (*ele).starty + 5; //Ñ¡ÖÐɾ³ý
|
Win32Util::mouseMove(x, y, 50);
|
Win32Util::click(10);
|
Win32Util::keyboard(VK_DELETE, 50);
|
}
|
img.release();
|
|
//----Ôö¼Ó
|
//½ØÍ¼£¬Ê¶±ð³öÔö¼Ó°´Å¥Î»Ö㬵ã»÷Ôö¼Ó£¬ÊäÈëÄÚÈÝ
|
for (std::list<string>::iterator ele = tempCodeList.begin();ele != tempCodeList.end();++ele) {
|
if ((*ele).length() > 0)
|
{
|
addGP(*ele);
|
Sleep(100);
|
}
|
}
|
|
oimg = CaptureUtil::capture(content);
|
if (oimg.cols <= 0 || oimg.rows <= 0) {
|
throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ");
|
}
|
img = ImgUtil::grayImage(oimg);
|
areaList = recognitionGPArea(img);
|
resultList = recognitionNum(img, areaList);
|
for (std::list<GPCodeArea>::iterator ele = resultList.begin();ele != resultList.end();++ele) {
|
bool contains = false;
|
std::list<string>::iterator e;
|
for (e = codeList.begin();e != codeList.end();e++) {
|
if (*e == (*ele).code) {
|
contains = true;
|
break;
|
}
|
}
|
if (contains) {
|
codeList.remove((*ele).code);
|
}
|
}
|
|
if (codeList.size() == 0) {
|
return true;
|
}
|
|
return false;
|
}
|
|
|
void THSActionUtil::addGP(string code) {
|
//´ò¿ª¸±ÆÁ
|
HWND sw = getSecondWindow();
|
if (sw <= 0)
|
{
|
openSecondScreen();
|
Sleep(2000);
|
sw = getSecondWindow();
|
}
|
if (sw <= 0) {
|
throw string("δ´ò¿ª¸±ÆÁ");
|
}
|
|
HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL);
|
cv::Mat oimg = CaptureUtil::capture(content);
|
cv::Mat img = ImgUtil::grayImage(oimg);
|
if (img.cols <= 0 || img.rows <= 0) {
|
throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ");
|
}
|
std::list<GPCodeArea> areaList = recognitionGPArea(img);
|
for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) {
|
GPCodeArea codeArea = *ele;
|
if (codeArea.type == IMG_TYPE_ADD) {
|
Win32Util::focus(sw);
|
RECT rc;
|
GetWindowRect(content, &rc);
|
int y = rc.top + codeArea.starty + (codeArea.endy - codeArea.starty) / 2;
|
int x = rc.left + codeArea.startx + (codeArea.endx - codeArea.startx) / 2;
|
Win32Util::click(x, y, 50);
|
Sleep(10);
|
Win32Util::click(x, y, 50);
|
Sleep(10);
|
Win32Util::click(x, y, 50);
|
//ÊäÈë¹ÉƱ´úÂë
|
Win32Util::keyboardNum(code, 1000);
|
Win32Util::keyboard(VK_RETURN, 1500);
|
//¹Ø±Õ°´Å¥
|
Sleep(100);
|
HWND close = FindWindowA(0, "Ìí¼Ó¹ÉƱ");
|
SendMessage(close, WM_CLOSE, 0, 0);
|
break;
|
}
|
}
|
}
|
|
list<GPCodeArea> splitGPCodeArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
|
|
list<GPCodeArea> resultList;
|
|
//·Ö¸ôÐÐÄÚÈÝ
|
int emptyStartRow = -1;
|
int emptyEndRow = -1;
|
int startf = -1;
|
int endf = -1;
|
std::list<int*> dataItemList;
|
|
for (int i = start_row;i < end_row;i++) {
|
bool empty = ImgDivider::isRowEmpty(img, i, start_col, start_col + 50, 1, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + (end_col - start_col) / 2 - 40, start_col + (end_col - start_col) / 2 + 40, 1, _IMG_BINARY_THRESHOLD);
|
if (empty) {
|
if (emptyStartRow < 0) {
|
emptyStartRow = i;
|
emptyEndRow = i;
|
}
|
else {
|
emptyEndRow = i;
|
}
|
|
if (emptyEndRow - emptyStartRow > 50 && dataItemList.size() > 0) {
|
//ûÓÐÊý¾ÝÁË
|
break;
|
}
|
|
if (startf > -1 && endf > -1 && emptyEndRow - emptyStartRow > 3) {
|
//ÄÚÈÝ×ø±ê
|
//LogUtil::debug("ÄÚÈݵĸ߶ÈΪ£º%d \n", endf - startf);
|
int* dd = (int*)malloc(sizeof(int) * 4);
|
*dd = start_col;
|
*(dd + 1) = startf;
|
*(dd + 2) = end_col;
|
*(dd + 3) = endf;
|
//ÐÐÊý¾Ý¸ß´óÓÚ6²ÅΪÓÐЧµÄÐиß
|
if (endf - startf > 6)
|
{
|
dataItemList.push_back(dd);
|
}
|
startf = -1;
|
endf = -1;
|
emptyStartRow = i;
|
emptyEndRow = i;
|
}
|
|
}
|
else
|
{
|
//Êý¾Ý¿ªÊ¼
|
if (startf < 0) {
|
startf = i;
|
endf = i;
|
}
|
else {
|
endf = i;
|
}
|
emptyStartRow = -1;
|
//Êý¾Ý½áÊø
|
}
|
}
|
|
//·Ö¸ôÁÐ
|
//·Ö¸ôÿһÁеÄÊý¾Ý
|
std::list<int*>::iterator ele;
|
int index = 0;
|
for (ele = dataItemList.begin(); ele != dataItemList.end();ele++) {
|
index++;
|
int startRow = *(*ele + 1);
|
int startCol = *(*ele + 0);
|
int endRow = *(*ele + 3);
|
int endCol = *(*ele + 2);
|
LogUtil::debug("%d %d %d %d\n", startRow, startCol, endRow, endCol);
|
|
//±£´æÐÐÊý¾Ý
|
if (false) {
|
std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\split\\";
|
path.append(std::to_string(index)).append(".jpg");
|
imwrite(path, cv::Mat(img, cv::Rect(startCol, startRow, endCol - startCol + 1, endRow - startRow + 1)));
|
}
|
|
int emptyStart = -1;
|
int emptyEnd = -1;
|
int dataStart = -1;
|
int dataEnd = -1;
|
|
std::list<int*> rowDataList;
|
|
for (int i = startCol;i <= endCol;i++) {
|
bool empty = ImgDivider::isColEmpty(img, i, startRow, endRow, 64);
|
if (empty) {
|
if (emptyStart < 0) {
|
emptyStart = i;
|
emptyEnd = i;
|
}
|
else {
|
emptyEnd = i;
|
}
|
|
//3¸ö¼°ÒÔÉϵĿհ×Êý¾Ý²Å·ÖÁÐ
|
if (emptyEnd - emptyStart >= 5 && dataEnd - dataStart > 0) {
|
int* dd = (int*)malloc(sizeof(int) * 4);
|
*dd = dataStart;
|
*(dd + 1) = startRow;
|
*(dd + 2) = dataEnd;
|
*(dd + 3) = endRow;
|
rowDataList.push_back(dd);
|
dataEnd = -1;
|
dataStart = -1;
|
if (rowDataList.size() >= 2) {
|
break;
|
}
|
}
|
}
|
else {
|
if (dataStart < 0) {
|
dataStart = i;
|
dataEnd = i;
|
}
|
else {
|
dataEnd = i;
|
}
|
|
emptyStart = -1;
|
emptyEnd = -1;
|
}
|
}
|
//ºóÃæµÄÊý¾ÝûÓÐ×ã¹»µÄ¿Õ°×·Ö¸ô
|
if (dataEnd - dataStart > 0) {
|
int* dd = (int*)malloc(sizeof(int) * 4);
|
*dd = dataStart;
|
*(dd + 1) = startRow;
|
*(dd + 2) = dataEnd;
|
*(dd + 3) = endRow;
|
rowDataList.push_back(dd);
|
}
|
|
int rowDataSize = rowDataList.size();
|
if (rowDataSize == 2) {
|
|
std::list<int*>::iterator ele = rowDataList.begin();
|
std::advance(ele, 1);
|
|
GPCodeArea area = GPCodeArea();
|
area.type = IMG_TYPE_GP;
|
area.startx = **ele;
|
area.starty = *(*ele + 1);
|
area.endx = *(*ele + 2);
|
area.endy = *(*ele + 3);
|
//¹ýÂË
|
if (area.endx - area.startx > 20)
|
{
|
resultList.push_back(area);
|
}
|
}
|
else {
|
std::list<int*>::iterator ele = rowDataList.begin();
|
GPCodeArea area = GPCodeArea();
|
area.type = IMG_TYPE_ADD;
|
area.startx = **ele;
|
area.starty = *(*ele + 1);
|
area.endx = *(*ele + 2);
|
area.endy = *(*ele + 3);
|
resultList.push_back(area);
|
}
|
|
|
|
//±£´æÊý¾Ý
|
if (false)
|
{
|
std::list<int*>::iterator ele1;
|
int cc = 0;
|
for (ele1 = rowDataList.begin(); ele1 != rowDataList.end();ele1++) {
|
if (*ele1)
|
{
|
int startCRow = *(*ele1 + 1);
|
int startCCol = *(*ele1 + 0);
|
int endCRow = *(*ele1 + 3);
|
int endCCol = *(*ele1 + 2);
|
cv::Mat temp = cv::Mat(img, cv::Rect(startCCol, startCRow, endCCol - startCCol + 1, endCRow - startCRow + 1));
|
std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\split\\";
|
path = path.append(std::to_string(index));
|
path = path.append("_");
|
path = path.append(std::to_string(cc));
|
path = path.append(".jpg");
|
imwrite(path.c_str(), temp);
|
}
|
cc++;
|
}
|
}
|
|
}
|
|
return resultList;
|
|
}
|
|
//ʶ±ð¹ÉƱ´úÂë
|
std::list<GPCodeArea> THSActionUtil::recognitionGPArea(cv::Mat img) {
|
|
//imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\test.jpg", img);
|
//»ñÈ¡title·Ö¸ôÏß
|
int rows = img.rows;
|
int cols = img.cols;
|
int r;
|
int contentStartRow = -1;
|
for (r = 5;r < img.rows;r++) {
|
if (ImgDivider::isRowFull(img, r, cols - 100, cols - 5, 2)) {
|
contentStartRow = r;
|
}
|
|
if (contentStartRow > -1) {
|
break;
|
}
|
}
|
|
if (contentStartRow < 0) {
|
throw string("ÆðʼÐзָô³ö´í");
|
}
|
|
//·Ö¸ôÁÐ
|
int c = 0;
|
int startC = -1;
|
int endC = -1;
|
for (c = 50;c < cols;c++) {
|
if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) {
|
if (startC < 0) {
|
startC = c;
|
}
|
else {
|
if (c - startC < 20) {
|
startC = c;
|
}
|
else {
|
endC = c;
|
break;
|
}
|
}
|
}
|
}
|
|
if (startC < 0 || endC < 0) {
|
throw string("ÄÚÈÝ¿ò·Ö¸ô³ö´í");
|
}
|
//cv::imshow("ÄÚÈÝ", cv::Mat(img, cv::Rect(startC, contentStartRow, endC - startC, rows - contentStartRow)));
|
|
startC += 1;
|
endC -= 1;
|
std::list<GPCodeArea> resultList;
|
try {
|
resultList = splitGPCodeArea(img, contentStartRow + 25, startC, rows, endC);
|
}
|
catch (...) {
|
throw string("·Ö¸ô´úÂëÇøÓò³ö´í");
|
}
|
return resultList;
|
}
|
|
std::list<GPCodeArea> THSActionUtil::recognitionNum(cv::Mat img, std::list<GPCodeArea> areaList) {
|
|
if (!recognitionManager) {
|
recognitionManager = new RecognitionManager();
|
}
|
//ʶ±ðÊý×Ö
|
std::list<GPCodeArea> codeList;
|
|
std::list<GPCodeArea>::iterator ele;
|
|
int index = 0;
|
for (ele = areaList.begin();ele != areaList.end();ele++) {
|
index++;
|
GPCodeArea codeArea = *ele;
|
if (codeArea.type == IMG_TYPE_GP) {
|
cv::Mat nums = cv::Mat(img, cv::Rect(codeArea.startx, codeArea.starty, codeArea.endx - codeArea.startx + 1, codeArea.endy - codeArea.starty + 1));
|
//±£´æ·Ö¸ôµÄÊý¾Ý
|
if (false) {
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append(to_string(index));
|
path.append(".jpg");
|
cv::imwrite(path, nums);
|
}
|
|
|
std::list<cv::Mat> list2 = splitGPCodeNum(nums);
|
|
if (false) {
|
std::list<cv::Mat>::iterator e;
|
int ci = 0;
|
for (e = list2.begin();e != list2.end();e++) {
|
ci++;
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append(to_string(index)).append("_").append(to_string(ci));
|
path.append(".jpg");
|
cv::imwrite(path, *e);
|
}
|
|
}
|
|
std::list<uchar> resultList = recognitionManager->recognitionGPCode(list2);
|
std::list<uchar>::iterator ele1;
|
string code = "";
|
for (ele1 = resultList.begin();ele1 != resultList.end();++ele1) {
|
code.append(to_string(*ele1));
|
}
|
codeArea.code = code;
|
cout << code << endl;
|
codeList.push_back(codeArea);
|
}
|
}
|
|
return codeList;
|
}
|
|
|
std::list<string> THSActionUtil::recognitionGPCode(cv::Mat img) {
|
cv::Mat grayImg = ImgUtil::grayImage(img);
|
std::list<GPCodeArea> areaList = recognitionGPArea(grayImg);
|
std::list<GPCodeArea> list = recognitionNum(grayImg, areaList);
|
std::list<string> resultList;
|
for (std::list<GPCodeArea>::iterator ele = list.begin();ele != list.end();ele++) {
|
resultList.push_back((*ele).code);
|
}
|
return resultList;
|
}
|
|
|
//·Ö¸ôL2Êý¾ÝµÄĿ¼
|
std::list<GPCodeArea> splitL2Cate(cv::Mat img) {
|
int cols = img.cols;
|
int contentStartRow = -1;
|
int contentEndRow = -1;
|
for (int r = 5;r < img.rows;r++) {
|
if (ImgDivider::isRowFull(img, r, 10, 200, 2, 10, 30) && ImgDivider::isRowFull(img, r, img.cols - 200, img.cols - 1, 2, 10, 30)) {
|
if (contentStartRow < 0)
|
{
|
contentStartRow = r;
|
}
|
else {
|
if (r - contentStartRow > 10) {
|
contentEndRow = r;
|
|
}
|
|
}
|
}
|
|
if (contentStartRow > -1 && contentEndRow > -1) {
|
break;
|
}
|
}
|
|
if (contentStartRow < 0 || contentEndRow < 0) {
|
throw string("ÆðʼÐлò½áÊøÐзָô³ö´í");
|
}
|
|
//·Ö¸ôÁÐ
|
list<int*> dataColIndexs;
|
int startf = -1;
|
int endf = -1;
|
int startIndex = -1;
|
for (int i = 10;i < cols;i++) {
|
if (startIndex == -1) {
|
startIndex = i;
|
}
|
|
bool full = ImgDivider::isColFull(img, i, contentStartRow, contentEndRow, 2);
|
if (full) {
|
if (startf < 0)
|
{
|
startf = i;
|
endf = i;
|
}
|
else {
|
endf = i;
|
}
|
}
|
else {
|
if (startf > -1 && endf > -1) {
|
int width = endf - startf + 1;
|
int* dd = (int*)malloc(sizeof(int) * 2);
|
|
*dd = startIndex;
|
*(dd + 1) = startf - 1;
|
|
dataColIndexs.push_back(dd);
|
startIndex = i;
|
}
|
startf = -1;
|
endf = -1;
|
}
|
|
}
|
|
if (startf > -1 && endf > -1) {
|
int width = endf - startf + 1;
|
int* dd = (int*)malloc(sizeof(int) * 2);
|
|
*dd = startIndex;
|
*(dd + 1) = startf - 1;
|
LogUtil::debug("ÁÐÊý¾Ý£º%d-%d\n", *dd, *(dd + 1));
|
|
dataColIndexs.push_back(dd);
|
|
startf = -1;
|
endf = -1;
|
}
|
|
if (cols - startIndex > 50) {
|
|
int* dd = (int*)malloc(sizeof(int) * 2);
|
|
*dd = startIndex;
|
*(dd + 1) = cols - 1;
|
|
dataColIndexs.push_back(dd);
|
}
|
|
|
|
|
std::list<GPCodeArea> areaList;
|
list<int*>::iterator ele;
|
for (ele = dataColIndexs.begin();ele != dataColIndexs.end();ele++) {
|
int* p = *ele;
|
int startx = *p;
|
int endx = *(p + 1);
|
|
GPCodeArea area = GPCodeArea();
|
area.startx = startx;
|
area.endx = endx;
|
area.starty = contentStartRow;
|
area.endy = contentEndRow;
|
areaList.push_back(area);
|
}
|
return areaList;
|
}
|
|
|
|
std::map<int, string> THSActionUtil::getListenL2GPCodes() {
|
std::list<GPCodeArea> areaList = getListenL2GP();
|
std::map<int, string> map;
|
for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
|
GPCodeArea data = (*ele);
|
if (map.count(data.cateIndex) == 0 && data.type == IMG_TYPE_GP) {
|
map[data.cateIndex] = data.code;
|
}
|
}
|
|
return map;
|
}
|
|
|
std::list<GPCodeArea> THSActionUtil::getListenL2GP() {
|
//½ØÍ¼µ±Ç°ÓÐÄÄЩ¹ÉƱ´úÂë
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
HWND mainPage = 0;
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
string str = Win32Util::getWindowName(hwnd);
|
if (isMainScreen(str)) {
|
cout << hwnd << endl;
|
//»ñÈ¡³ß´ç
|
RECT rc;
|
GetWindowRect(hwnd, &rc);
|
if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) {
|
mainPage = hwnd;
|
break;
|
}
|
}
|
}
|
if (mainPage <= 0) {
|
throw string("L2¼àÌýδ´ò¿ª");
|
}
|
|
HWND content = FindWindowExA(mainPage, NULL, "AfxFrameOrView100s", NULL);
|
//½ØÍ¼
|
cv::Mat oimg = CaptureUtil::capture(content);
|
cv::Mat img = ImgUtil::grayImage(oimg);
|
oimg.release();
|
//·Ö¸ôͼƬ
|
|
std::list<GPCodeArea> areaList = splitL2Cate(img);
|
std::list<GPCodeArea> fresultList;
|
|
int index = 0;
|
for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
|
GPCodeArea area = *ele;
|
index++;
|
std::list<GPCodeArea> resultList = splitGPCodeArea(img, area.starty + 20, area.startx, area.endy, area.endx);
|
|
|
for (std::list<GPCodeArea>::iterator ele1 = resultList.begin();ele1 != resultList.end();ele1++) {
|
GPCodeArea areaC = *ele1;
|
if (areaC.type == IMG_TYPE_GP) {
|
cv::Mat src = cv::Mat(img, cv::Rect(areaC.startx, areaC.starty, areaC.endx - areaC.startx + 1, areaC.endy - areaC.starty + 1));
|
if (false) {
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append(to_string(index)).append(".jpg");
|
cv::imwrite(path, src);
|
}
|
|
list<cv::Mat> nums = splitGPCodeNum(src);
|
if (!recognitionManager) {
|
recognitionManager = new RecognitionManager();
|
}
|
|
list<uchar> rresult = recognitionManager->recognitionGPCode(nums);
|
string num;
|
for (std::list<uchar>::iterator e = rresult.begin();e != rresult.end();e++) {
|
num.append(to_string(*e));
|
}
|
(*ele1).code = num;
|
(*ele1).cateIndex = index - 1;
|
|
fresultList.push_back(*ele1);
|
}
|
}
|
}
|
|
return fresultList;
|
|
}
|
bool THSActionUtil::setListenL2GP(int p, string code) {
|
|
|
std::list<GPCodeArea> resultList = getListenL2GP();
|
|
HWND content = FindWindowExA(getL2Win(), NULL, "AfxFrameOrView100s", NULL);
|
|
int index = 0;
|
for (std::list<GPCodeArea>::iterator ele = resultList.begin();ele != resultList.end();ele++) {
|
GPCodeArea result = *ele;
|
if (result.code == code) {
|
//µã»÷
|
RECT rc;
|
GetWindowRect(content, &rc);
|
int x = rc.left + result.startx + 5;
|
int y = rc.top + result.starty + 5;
|
Win32Util::focus(content);
|
Win32Util::click(x, y, 50);
|
return true;
|
}
|
}
|
return false;
|
}
|