#include "THSActionUtil.h"
|
#include "ImgDivider.h"
|
#include "Win32Util.h"
|
#include <fstream>
|
|
|
bool isL2Screen(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 isMainScreen(string str) {
|
if (str.find("ͬ»¨Ë³(") != string::npos) {
|
cout << str << endl;
|
if (str.find("¸±ÆÁ") == string::npos) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
bool isSecondScreen(string str) {
|
return str.find("ͬ»¨Ë³(") != string::npos && str.find("¸±ÆÁ1") != string::npos;
|
}
|
|
bool isThirdScreen(string str) {
|
return str.find("ͬ»¨Ë³(") != string::npos && str.find("¸±ÆÁ2") != 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("´úÂëÊý×Ö·Ö¸ô³ö´í(32)");
|
}
|
return nums;
|
}
|
|
//»ñÈ¡¸±ÆÁ2
|
HWND THSActionUtil::getThirdWindow() {
|
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 (isThirdScreen(str))
|
{
|
return hwnd;
|
}
|
}
|
return 0;
|
}
|
|
|
HWND THSActionUtil::getIndustryWindow() {
|
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 (str.find("ÐÐÒµ") != string::npos)
|
{
|
return hwnd;
|
}
|
}
|
return 0;
|
}
|
|
HWND THSActionUtil::getTradeDelegateWindow() {
|
list<HWND> wlist = Win32Util::searchWindow("רҵ°æÏµ¥");
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
return hwnd;
|
}
|
return 0;
|
}
|
|
HWND THSActionUtil::getTradeSimpleDelegateWindow()
|
{
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³Ô¶º½°æ");
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin(); ele != wlist.end(); ele++) {
|
HWND hwnd = *ele;
|
hwnd = FindWindowExA(hwnd, NULL, NULL, NULL);
|
string name = Win32Util::getWindowName(hwnd);
|
if (name.find("ÍøÉÏ¹ÉÆ±½»Ò×ϵͳ") != string::npos) {
|
return hwnd;
|
}
|
}
|
return 0;
|
}
|
|
|
HWND THSActionUtil::getTradeSuccessWindow() {
|
list<HWND> wlist = Win32Util::searchWindow("ÍøÉÏ¹ÉÆ±½»Ò×ϵͳ");
|
list<HWND>::iterator ele;
|
for (ele = wlist.begin();ele != wlist.end();ele++) {
|
HWND hwnd = *ele;
|
return hwnd;
|
}
|
return 0;
|
}
|
|
|
|
|
//»ñÈ¡¸±ÆÁ1
|
HWND THSActionUtil::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::getMainWin() {
|
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;
|
}
|
|
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 (isL2Screen(str)) {
|
return hwnd;
|
}
|
|
}
|
return 0;
|
}
|
bool THSActionUtil::checkEnv() {
|
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
list<HWND>::iterator ele;
|
bool fp = false;
|
bool fp2 = 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 (isThirdScreen(str))
|
{
|
fp2 = true;
|
}
|
|
if (isL2Screen(str)) {
|
zp = true;
|
}
|
|
}
|
|
if (!zp) {
|
throw string("ͬ»¨Ë³Ö÷ÆÁL2´°¿Úδ´ò¿ª(01)");
|
}
|
|
|
if (!fp) {
|
throw string("ͬ»¨Ë³¸±ÆÁ1ûÓдò¿ª(02)");
|
}
|
|
if (!fp2) {
|
throw string("ͬ»¨Ë³¸±ÆÁ2ûÓдò¿ª(03)");
|
}
|
|
return true;
|
}
|
|
void THSActionUtil::openL2Screen() {
|
|
|
HWND l2 = getL2Win();
|
if (l2 <= 0) {
|
//³¢ÊÔ´ò¿ªÖ÷ÆÁ
|
HWND main = THSActionUtil::getMainWin();
|
if (main > 0)
|
{
|
Win32Util::focus(main);
|
Win32Util::keyboardNum(".00", 200);
|
Win32Util::keyboard(13, 10);
|
Sleep(1000);
|
}
|
}
|
}
|
|
void THSActionUtil::openSecondScreen() {
|
|
HWND second = getSecondWindow();
|
if (second > 0) {
|
Win32Util::showWindow(second);
|
return;
|
}
|
|
list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
|
|
bool open = false;
|
HWND mainPage = getMainWin();
|
|
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 (isL2Screen(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("δÕÒµ½Ê×Ò³(04)");
|
}
|
|
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);
|
}
|
}
|
|
|
|
void THSActionUtil::openThirdScreen() {
|
|
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 (isThirdScreen(str))
|
{
|
cout << str << endl;
|
SetForegroundWindow(hwnd);
|
SetFocus(hwnd);
|
open = true;
|
break;
|
}
|
|
if (isL2Screen(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("δÕÒµ½Ê×Ò³(05)");
|
}
|
|
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);
|
Sleep(50);
|
//µã»÷¸±ÆÁ2
|
Win32Util::click(x + 10, y + 21 * 8 + 5, 500);
|
}
|
}
|
|
void THSActionUtil::openTradeSuccessScreen()
|
{
|
HWND hwnd = getTradeSuccessWindow();
|
if (hwnd <= 0) {
|
throw string("½»Ò×´°¿Úδ´ò¿ª(06)");
|
}
|
|
hwnd = FindWindowExA(hwnd, NULL, "AfxMDIFrame140s", NULL);
|
hwnd = FindWindowExA(hwnd, NULL, "AfxWnd140s", NULL);
|
hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd");
|
hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd2");
|
hwnd = FindWindowExA(hwnd, NULL, "SysTreeView32", NULL);
|
if (hwnd <= 0) {
|
throw string("δÕÒµ½½»Ò×´°¿Ú²Ëµ¥(07)");
|
}
|
//µã»÷µ±ÈÕίÍÐ
|
SendMessage(hwnd, WM_LBUTTONDOWN, 0x00000001, 0x00E70047);
|
SendMessage(hwnd, WM_LBUTTONUP, 0x00000000, 0x00E70047);
|
}
|
|
|
//Ìí¼Ó¹ÉƱ
|
bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList, RecognitionManager* recognitionManager) {
|
|
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("δ´ò¿ª¸±ÆÁ(11)");
|
}
|
|
//´ò¿ª°å¿é
|
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("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(12)");
|
}
|
|
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 = recognitionGPCodeNum(img, areaList, recognitionManager);
|
|
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("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(13)");
|
}
|
img = ImgUtil::grayImage(oimg);
|
areaList = recognitionGPArea(img);
|
resultList = recognitionGPCodeNum(img, areaList, recognitionManager);
|
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("δ´ò¿ª¸±ÆÁ(14)");
|
}
|
|
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("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(15)");
|
}
|
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, "Ìí¼Ó¹ÉƱ");
|
PostMessage(close, WM_CLOSE, 0, 0);
|
break;
|
}
|
}
|
}
|
|
|
std::list<int*> THSActionUtil::splitPlateRowArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
|
|
//·Ö¸ôÐÐÄÚÈÝ
|
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++) {
|
//TODO 100
|
bool empty = ImgDivider::isRowEmpty(img, i, start_col, start_col + 50, 1, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + 50, start_col + 150, 2, _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 && (dataItemList.size() > 0 && emptyEndRow - emptyStartRow > 3 || emptyEndRow - emptyStartRow > 0)) {
|
//ÄÚÈÝ×ø±ê
|
//LogUtil::debug("ÄÚÈݵĸ߶ÈΪ£º%d \n", endf - startf);
|
|
//ÐÐÊý¾Ý¸ß´óÓÚ6²ÅΪÓÐЧµÄÐиß
|
if (endf - startf > 6)
|
{
|
int* dd = (int*)malloc(sizeof(int) * 4);
|
*dd = start_col;
|
*(dd + 1) = startf;
|
*(dd + 2) = end_col;
|
*(dd + 3) = endf;
|
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;
|
//Êý¾Ý½áÊø
|
}
|
}
|
|
return dataItemList;
|
}
|
|
|
GPCodeArea THSActionUtil::splitPlateMenuArea(cv::Mat img) {
|
int rows = img.rows;
|
int cols = img.cols;
|
int r;
|
int contentStartRow = -1;
|
int contentEndRow = -1;
|
for (r = 5;r < img.rows;r++) {
|
if (ImgDivider::isRowFull(img, r, 0, cols - 5, 3, 12, 30)) {
|
|
if (contentStartRow < 0)
|
contentStartRow = r;
|
else if (r - contentStartRow > 100) {
|
contentEndRow = r;
|
}
|
|
|
}
|
|
if (contentStartRow > -1 && contentEndRow > -1) {
|
break;
|
}
|
}
|
|
if (contentEndRow < 0) {
|
contentEndRow = img.rows - 1;
|
}
|
|
if (contentStartRow < 0 || contentEndRow < 0) {
|
throw string("ÆðʼÐзָô³ö´í(16)");
|
}
|
|
//·Ö¸ôÁÐ
|
int c = 0;
|
int startC = -1;
|
for (c = 0;c < cols;c++) {
|
if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) {
|
if (startC < 0 && c>50) {
|
startC = c;
|
break;
|
}
|
}
|
}
|
|
if (startC < 0) {
|
throw string("ÄÚÈÝ¿ò·Ö¸ô³ö´í(17)");
|
}
|
GPCodeArea area = GPCodeArea();
|
area.startx = 0;
|
area.starty = contentStartRow;
|
area.endx = startC;
|
area.endy = contentEndRow;
|
return area;
|
}
|
|
//·Ö¸ô°å¿éÐÐÊý¾Ý
|
std::list<int*> THSActionUtil::splitPlateRowArea(cv::Mat img) {
|
|
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 || contentStartRow>rows - 100) {
|
throw string("ÆðʼÐзָô³ö´í(18)");
|
}
|
|
//·Ö¸ôÁÐ
|
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("ÄÚÈÝ¿ò·Ö¸ô³ö´í(19)");
|
}
|
|
//ÔٴηָôÄÚÈÝ¿ò,Åųý¸ÉÈÅ
|
int cut_index = -1;
|
for (int r = contentStartRow + 5;r < contentStartRow + 5 + 100;r++) {
|
|
if (ImgDivider::isRowFull(img, r, startC, endC, 2, 25, 30)) {
|
cut_index = r;
|
}
|
else {
|
if (cut_index > 0 && r - cut_index > 30) {
|
contentStartRow = cut_index;
|
break;
|
}
|
}
|
}
|
|
|
|
|
|
|
startC += 1;
|
endC -= 1;
|
|
int start_row = contentStartRow + 25;
|
int start_col = startC;
|
int end_row = rows;
|
int end_col = endC;
|
return splitPlateRowArea(img, start_row, start_col, end_row, end_col);
|
|
}
|
|
|
|
|
|
list<GPCodeArea> splitGPCodeArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
|
|
list<GPCodeArea> resultList;
|
|
//·Ö¸ôÐÐÄÚÈÝ
|
std::list<int*> dataItemList = THSActionUtil::splitPlateRowArea(img, start_row, start_col, end_row, end_col);
|
|
|
|
|
//·Ö¸ôÁÐ
|
//·Ö¸ôÿһÁеÄÊý¾Ý
|
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 if(rowDataList.size()>0) {
|
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 (rowDataList.size() > 0)
|
{
|
for (std::list<int*>::iterator e = rowDataList.begin();e != rowDataList.end();e++) {
|
free(*e);
|
}
|
}
|
|
free(*ele);
|
|
//±£´æÊý¾Ý
|
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("ÆðʼÐзָô³ö´í(20)");
|
}
|
|
//·Ö¸ôÁÐ
|
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("ÄÚÈÝ¿ò·Ö¸ô³ö´í(21)");
|
}
|
//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("·Ö¸ô´úÂëÇøÓò³ö´í(22)");
|
}
|
return resultList;
|
}
|
|
std::list<GPCodeArea> THSActionUtil::recognitionGPCodeNum(cv::Mat img, std::list<GPCodeArea> areaList, RecognitionManager* 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;
|
try {
|
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);
|
}
|
catch (...) {
|
|
|
}
|
|
}
|
}
|
|
return codeList;
|
}
|
|
|
std::list<string> THSActionUtil::recognitionGPCode(cv::Mat img, RecognitionManager* recognitionManager) {
|
cv::Mat grayImg = ImgUtil::grayImage(img);
|
std::list<GPCodeArea> areaList = recognitionGPArea(grayImg);
|
std::list<GPCodeArea> list = recognitionGPCodeNum(grayImg, areaList, recognitionManager);
|
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;
|
|
}
|
|
}
|
}
|
//contentStartRow > -1 && contentEndRow > -1
|
if (contentStartRow > -1) {
|
break;
|
}
|
}
|
//contentStartRow < 0 || contentEndRow < 0
|
if (contentStartRow < 0) {
|
throw string("ÆðʼÐлò½áÊøÐзָô³ö´í(23)");
|
}
|
|
contentEndRow = contentStartRow + 50;
|
|
//·Ö¸ôÁÐ
|
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);
|
|
int endRow = 0;
|
//ÅжϽáÊøÐÐ
|
for (int r = contentStartRow + 10;r < img.rows;r++) {
|
bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30);
|
if (full) {
|
endRow = r;
|
break;
|
}
|
}
|
|
|
|
GPCodeArea area = GPCodeArea();
|
area.startx = startx;
|
area.endx = endx;
|
area.starty = contentStartRow;
|
area.endy = endRow;
|
areaList.push_back(area);
|
|
free(*ele);
|
}
|
return areaList;
|
}
|
|
|
|
//·Ö¸ôL2½»Ò×¶ÓÁÐ
|
std::list<GPCodeArea> splitL2TradeQueue(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, 0, img.cols / 3, 2, 10, 30) && ImgDivider::isRowFull(img, r, img.cols * 2 / 3, img.cols, 2, 10, 30)) {
|
if (contentStartRow < 0)
|
{
|
contentStartRow = r;
|
break;
|
}
|
}
|
}
|
//contentStartRow < 0 || contentEndRow < 0
|
if (contentStartRow < 0) {
|
throw string("ÆðʼÐлò½áÊøÐзָô³ö´í(24)");
|
}
|
|
contentEndRow = contentStartRow + 50;
|
|
//·Ö¸ôÁÐ
|
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);
|
|
int endRow = 0;
|
//ÅжϽáÊøÐÐ
|
for (int r = contentStartRow + 10;r < img.rows;r++) {
|
bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30);
|
if (full) {
|
endRow = r;
|
break;
|
}
|
}
|
|
|
|
GPCodeArea area = GPCodeArea();
|
area.startx = startx;
|
area.endx = endx;
|
area.starty = contentStartRow;
|
area.endy = endRow;
|
areaList.push_back(area);
|
|
free(*ele);
|
}
|
return areaList;
|
}
|
|
|
std::map<int, string> THSActionUtil::getListenL2GPCodes(RecognitionManager* recognitionManager) {
|
std::list<GPCodeArea> areaList = getListenL2GPAreaAndCode(recognitionManager);
|
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;
|
}
|
|
|
void THSActionUtil::betchAddGP(HWND addDialog, list<string> codeList)
|
{
|
//½«´úÂëдÈëtxtÎļþ
|
string path = "D:\\gpcode\\gp.txt";
|
ofstream ofs;
|
ofs.open(path, ios::trunc);
|
int index = 0;
|
for (list<string>::iterator ele = codeList.begin();ele != codeList.end();++ele)
|
{
|
index++;
|
ofs << *ele;
|
if (index < codeList.size())
|
ofs << "\n";
|
}
|
|
Sleep(200);
|
addDialog = FindWindowExA(addDialog, NULL, "AfxWnd100s", "CEFBrowser");
|
addDialog = FindWindowExA(addDialog, NULL, "CefBrowserWindow", NULL);
|
addDialog = FindWindowExA(addDialog, NULL, "Chrome_WidgetWin_0", NULL);
|
addDialog = FindWindowExA(addDialog, NULL, "Chrome_RenderWidgetHostHWND", NULL);
|
cout << "Ìí¼Ó¹ÉƱµ¯¿ò£º" << addDialog << endl;
|
|
|
Win32Util::focus(addDialog);
|
//ͼƬѡÔñ°´Å¥
|
SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00370141);
|
SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00370141);
|
Sleep(100);
|
//ÉÏ´«Îļþ°´Å¥
|
SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x016000B1);
|
SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x016000B1);
|
|
|
HWND hwnd = HWND(0);
|
for (int i = 0;i < 10;i++)
|
{
|
HWND temp = FindWindowA("#32770", "´ò¿ª");
|
if (temp > 0) {
|
hwnd = temp;
|
break;
|
}
|
Sleep(1000);
|
}
|
|
Win32Util::selectTexFileWin10(hwnd, "D:\\gpcode\\");
|
Sleep(500);
|
Win32Util::focus(addDialog);
|
//µã»÷Ìí¼ÓÈ«²¿°´Å¥
|
SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00570139);
|
SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00570139);
|
//¹Ø±Õµ¯¿ò
|
HWND close = FindWindowA(0, "Ìí¼Ó¹ÉƱ");
|
SendMessage(close, WM_CLOSE, 0, 0);
|
}
|
|
|
std::list<GPCodeArea> THSActionUtil::getListenL2GPAreaAndCode(RecognitionManager* recognitionManager) {
|
//½ØÍ¼µ±Ç°ÓÐÄÄЩ¹ÉƱ´úÂë
|
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 (isL2Screen(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¼àÌýδ´ò¿ª(25)");
|
}
|
|
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;
|
if (false) {
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append("cate_").append(to_string(index)).append(".jpg");
|
cv::imwrite(path, cv::Mat(img, cv::Rect(area.startx, area.starty, area.endx - area.startx + 1, area.endy - area.starty + 1)));
|
}
|
|
|
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();
|
}
|
try {
|
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);
|
}
|
catch (...) {
|
|
}
|
}
|
}
|
}
|
|
return fresultList;
|
|
}
|
|
|
// »ñÈ¡²Ù×÷ÇøÓò
|
std::list<std::list<GPCodeArea>> THSActionUtil::getListenL2GPAreaActionBar() {
|
//½ØÍ¼µ±Ç°ÓÐÄÄЩ¹ÉƱ´úÂë
|
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 (isL2Screen(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¼àÌýδ´ò¿ª(25)");
|
}
|
|
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;
|
|
int startRow = -1;
|
int endRow = -1;
|
for (int r = area.endy; r >= area.starty; r--) {
|
|
if (!ImgDivider::isRowEmpty(img, r, area.startx, (area.endx + area.startx)/2)) {
|
if (startRow < 0)
|
{
|
startRow = r;
|
endRow = r;
|
}
|
endRow = r;
|
}
|
else {
|
if (startRow > 0 && endRow > 0) {
|
if (startRow - endRow > 10) {
|
GPCodeArea farea;
|
farea.startx = area.startx;
|
farea.endx = area.endx;
|
farea.starty = endRow;
|
farea.endy = startRow;
|
fresultList.push_back(farea);
|
// TODO ÔÝʱ´ò¿ª
|
if (false) {
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append("cate_").append(to_string(index)).append(".jpg");
|
cv::imwrite(path, cv::Mat(img, cv::Rect(farea.startx, farea.starty, farea.endx - farea.startx + 1, farea.endy - farea.starty + 1)));
|
}
|
break;
|
}
|
else {
|
startRow = endRow;
|
}
|
}
|
}
|
}
|
index++;
|
}
|
|
index = 0;
|
std::list<list<GPCodeArea>> ffresultList;
|
//Çиîÿһ¿éµÄ²Ëµ¥
|
for (std::list<GPCodeArea>::iterator ele = fresultList.begin(); ele != fresultList.end(); ele++) {
|
GPCodeArea area = *ele;
|
int start_col = area.startx;
|
|
list<GPCodeArea> tempList;
|
|
for (int c = area.startx; c <= area.endx; c++) {
|
//ͨ¹ýµÚÒ»ÐеĴ¿ºÚµã×ö·Ö¸ô
|
if (img.ptr<uchar>(area.starty)[c] <= 5) {
|
if (c - start_col > 10) {
|
//»ñÈ¡µ½·Ö¸ôµã
|
GPCodeArea child;
|
child.startx = start_col;
|
child.endx = c -1;
|
child.starty = area.starty;
|
child.endy = area.endy;
|
tempList.push_back(child);
|
start_col = c;
|
|
if (false) {
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
|
path.append("cate_").append(to_string(index)).append("_").append(to_string(c)).append(".jpg");
|
cv::imwrite(path, cv::Mat(img, cv::Rect(child.startx, child.starty, child.endx - child.startx + 1, child.endy - child.starty + 1)));
|
}
|
|
}
|
}
|
}
|
index++;
|
ffresultList.push_back(tempList);
|
}
|
|
|
return ffresultList;
|
|
}
|
|
cv::Mat getTHSTimeCapture() {
|
HWND hwnd = THSActionUtil::getMainWin();
|
if (hwnd <= 0) {
|
throw string("δÕÒµ½Í¬»¨Ë³Ö÷Ò³");
|
}
|
HWND bar = FindWindowEx(hwnd, NULL, TEXT("msctls_statusbar32"), NULL);
|
int count = 0;
|
int lastTop = 0;
|
HWND lastBar = 0;
|
do {
|
count++;
|
RECT rect;
|
GetWindowRect(bar, &rect);
|
if (rect.top > lastTop) {
|
lastTop = rect.top;
|
lastBar = bar;
|
}
|
bar = FindWindowEx(hwnd, bar, TEXT("msctls_statusbar32"), NULL);
|
if (count > 10 || bar <= 0) {
|
bar = 0;
|
break;
|
}
|
} while (TRUE);
|
|
if (lastBar <= 0) {
|
throw string("δÕÒµ½Í¬»¨Ë³Ê±¼ä´°¿Ú");
|
}
|
|
if (lastBar > 0) {
|
cv::Mat mat = CaptureUtil::capture(lastBar);
|
if (mat.cols > 100) {
|
mat = cv::Mat(mat, cv::Rect(mat.cols - 80, 0, 80, mat.rows));
|
mat = ImgUtil::grayImage(mat);
|
return mat;
|
}
|
}
|
|
throw string("δÕÒµ½Í¬»¨Ë³Ê±¼ä½ØÍ¼»ñȡʧ°Ü");
|
|
}
|
|
bool THSActionUtil::thsIsDead()
|
{
|
cv::Mat oldMat = getTHSTimeCapture();
|
Sleep(1200);
|
cv::Mat newMat = getTHSTimeCapture();
|
|
if (ImgUtil::isImgSame(oldMat, newMat)) {
|
return TRUE;
|
}
|
|
return FALSE;
|
}
|
void THSActionUtil::updateMainSite()
|
{
|
//»ñÈ¡Ö÷Ò³
|
HWND main = getMainWin();
|
if (main <= 0) {
|
throw string("ͬ»¨Ë³Ö÷ҳδ´ò¿ª");
|
}
|
Win32Util::showWindow(main);
|
//»ñÈ¡toolbar
|
HWND win = NULL;
|
do {
|
win = FindWindowExA(main, win, "AfxControlBar100s", "");
|
if (win <= 0) {
|
break;
|
}
|
|
RECT rect;
|
GetWindowRect(win, &rect);
|
if (rect.bottom - rect.top >= 40 && rect.bottom - rect.top <= 60) {
|
break;
|
}
|
} while (true);
|
|
if (win <= 0) {
|
throw string("²âËÙ°´Å¥Î´ÕÒµ½");
|
}
|
|
win = FindWindowExA(win, NULL, "#32770", "");
|
if (win <= 0) {
|
throw string("²âËÙ°´Å¥Î´ÕÒµ½");
|
}
|
|
HWND btn = GetDlgItem(win, 0x00007A17);
|
if (btn <= 0) {
|
throw string("²âËÙ°´Å¥Î´ÕÒµ½");
|
}
|
|
//µã»÷°´Å¥
|
Win32Util::visualClick(btn, MAKELONG(3,3));
|
for (int i = 0;i < 10;i++) {
|
list<HWND> _list = Win32Util::searchWindow("Ñ¡Ôñ×îÓÅÐÐÇéÖ÷Õ¾");
|
if (_list.size() > 0) {
|
HWND win = *(_list.begin());
|
HWND loginBtn = 0;
|
for (int j = 0;j < 10;j++)
|
{
|
loginBtn = FindWindowExA(win, NULL, "Button", "µÇ ¼");
|
if (loginBtn > 0) {
|
//µã»÷µÇ¼°´Å¥
|
Win32Util::visualClick(loginBtn, MAKELONG(2, 2));
|
break;
|
}
|
Sleep(200);
|
}
|
}
|
Sleep(400);
|
}
|
|
|
|
}
|
void THSActionUtil::clearPlateCodes(HWND hwnd)
|
{
|
HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL);
|
for (int i = 0;i < 15;i++)
|
{
|
//½ØÍ¼Ê¶±ðÊÇ·ñ»¹ÓдúÂë
|
cv::Mat oimg = CaptureUtil::capture(content);
|
if (oimg.cols <= 0 || oimg.rows <= 0) {
|
throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(26)");
|
}
|
|
cv::Mat img = ImgUtil::grayImage(oimg);
|
oimg.release();
|
std::list<GPCodeArea> areaList = recognitionGPArea(img);
|
cout << "Êý¾ÝÊýÁ¿:" << areaList.size() << endl;
|
if (areaList.size() <= 2) {
|
//Ö´ÐÐ×îºóÒ»´Îɾ³ý
|
Win32Util::focus(hwnd);
|
Win32Util::keyboard(VK_DELETE, 30);
|
break;
|
}
|
//Ö´ÐÐɾ³ýʼþ
|
for (int j = 0;j < 20;j++) {
|
Win32Util::focus(hwnd);
|
Win32Util::keyboard(VK_DELETE, 30);
|
}
|
}
|
}
|
|
RECT THSActionUtil::getPlateAddCodeBtnPostion(HWND win)
|
{
|
//»ñÈ¡Ìí¼Ó°´Å¥Î»ÖÃ
|
HWND content = FindWindowExA(win, NULL, "AfxFrameOrView100s", NULL);
|
RECT contentRect;
|
GetWindowRect(content, &contentRect);
|
|
cv::Mat oimg = CaptureUtil::capture(content);
|
|
if (oimg.cols <= 0 || oimg.rows <= 0) {
|
throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(27)");
|
}
|
|
cv::Mat img = ImgUtil::grayImage(oimg);
|
oimg.release();
|
std::list<GPCodeArea> areaList = recognitionGPArea(img);
|
for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
|
cout << "Ìí¼Ó°´Å¥Î»Öãº" << (*ele).startx << "," << (*ele).starty << "," << (*ele).endx << "," << (*ele).endy << endl;
|
if ((*ele).type == IMG_TYPE_ADD) {
|
RECT rect;
|
rect.left = contentRect.left + (*ele).startx;
|
rect.right = contentRect.left + (*ele).endx;
|
rect.top = contentRect.top + (*ele).starty;
|
rect.bottom = contentRect.top + (*ele).endy;
|
return rect;
|
}
|
}
|
throw string("ÉÐδÕÒµ½Ìí¼Ó°´Å¥(28)");
|
}
|
|
|
bool THSActionUtil::setListenL2GP(int p, string code, RecognitionManager* recognitionManager) {
|
|
|
std::list<GPCodeArea> resultList = getListenL2GPAreaAndCode(recognitionManager);
|
|
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);
|
//¼ä¸ô1200msÔÙµã»÷
|
Win32Util::click(x, y, 1200);
|
return true;
|
}
|
}
|
return false;
|
}
|
|
|
GPCodeArea THSActionUtil::recognitionPlateContentArea(cv::Mat grayImg) {
|
|
int rows = grayImg.rows;
|
int cols = grayImg.cols;
|
int r;
|
int contentStartRow = -1;
|
for (r = 5;r < grayImg.rows;r++) {
|
if (ImgDivider::isRowFull(grayImg, r, cols - 100, cols - 5, 2)) {
|
contentStartRow = r;
|
}
|
|
if (contentStartRow > -1) {
|
break;
|
}
|
}
|
|
if (contentStartRow < 0) {
|
throw string("ÆðʼÐзָô³ö´í(29)");
|
}
|
|
//·Ö¸ôÁÐ
|
int c = 0;
|
int startC = -1;
|
int endC = -1;
|
for (c = 50;c < cols;c++) {
|
if (ImgDivider::isColFull(grayImg, 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("ÄÚÈÝ¿ò·Ö¸ô³ö´í(30)");
|
}
|
|
GPCodeArea area = GPCodeArea();
|
area.startx = startC;
|
area.starty = contentStartRow;
|
area.endx = endC;
|
area.endy = rows - 1;
|
|
return area;
|
}
|
|
void THSActionUtil::addTargetCodes(list<std::string> codeList, RecognitionManager* recognitionManager)
|
{
|
//»ñȡͬ»¨Ë³Ö÷Ò³
|
HWND win = getSecondWindow();
|
Win32Util::focus(win);
|
//¿ì½Ý¼ü´ò¿ª°å¿é
|
Win32Util::keyboardNum("51", 200);
|
Win32Util::keyboard(13, 10);
|
Sleep(1000);
|
//ɾ³ý°å¿éÄÚµÄËùÓÐÄÚÈÝ
|
clearPlateCodes(win);
|
//»ñÈ¡Ìí¼Ó°´Å¥Î»ÖÃ
|
RECT codeArea = getPlateAddCodeBtnPostion(win);
|
//µã»÷Ìí¼Ó°´Å¥
|
Win32Util::click(codeArea.left + 5, codeArea.top + 3);
|
|
//ÅúÁ¿Ìí¼Ó´úÂë
|
HWND addDLG;
|
for (int i = 0;i < 10;i++) {
|
addDLG = FindWindowA("#32770", "Ìí¼Ó¹ÉƱ");
|
if (addDLG > 0) {
|
break;
|
}
|
Sleep(1000);
|
}
|
if (addDLG <= 0) {
|
throw string("Ìí¼Ó¹ÉƱ´°¿Ú´ò¿ªÊ§°Ü(31)");
|
}
|
if (codeList.size() >= 100)
|
{
|
betchAddGP(addDLG, codeList);
|
}
|
else {
|
//Öð¸öÌí¼Ó
|
for (list<std::string>::iterator ele = codeList.begin();ele != codeList.end();++ele) {
|
string code = *ele;
|
Win32Util::focus(addDLG);
|
for (int i = 0;i < 6;i++)
|
{
|
Win32Util::keyboard(VK_BACK, 10);
|
}
|
Win32Util::focus(addDLG);
|
Win32Util::keyboardNum(code, 1000);
|
Win32Util::focus(addDLG);
|
Win32Util::keyboard(VK_RETURN, 1500);
|
}
|
Sleep(100);
|
SendMessage(addDLG, WM_CLOSE, 0, 0);
|
}
|
//TODO УÑé´úÂëµÄÕýÈ·ÐÔ
|
}
|