#include "TradeQueueCapture.h" #include "CaptureUtil.h" #include "Win32Util.h" #include TradeQueueCapture::TradeQueueCapture() { openCLExcuter = new OpenCLExcuter(); openCLExcuter->init(); } list TradeQueueCapture::split(cv::Mat img) { list dataItemList; // ´ÓͼƬÖмä·Ö¸ô int start_row = img.rows / 2 + 1; //´Ó·Ö¸îÏß¿ªÊ¼»ñÈ¡µÚÒ»ÐеÄÊý¾Ý int start_index = -1; int end_index = -1; for (int r = start_row;r < img.rows;r++) { if (!ImgDivider::isRowEmpty(img, r, 0, img.cols / 2, 1)) { if (start_index < 0) { start_index = r; } end_index = r; } else { if (start_index > -1 && end_index > -1) { break; } } } int start_col = 0; //ÒѾ­Ïµ¥µÄ if (end_index - start_index + 1 > _NUMBER_TRADE_QUEUE_HEIGHT + 4) { //Çиʼµã for (int c = img.cols - 5; c >= 0; c--) { if (ImgDivider::isColFull(img,c,start_index,end_index,1)) { start_col = c + 1; break; } } } start_row = -1; int end_row = -1; if (start_col > 0) { //È¥³ýÉÏÏÂµÄºÚ±ß for (int r = start_index; r <= end_index; r++) { if (!ImgDivider::isRowEmpty(img, r, start_col, img.cols - 1)) { if (start_row < 0) { start_row = r; end_row = r; } end_row = r; } else { if (start_row > 0 && end_row > 0) { break; } } } }else { start_col = 0; start_row = start_index; end_row = end_index; } std::list posList; if (start_row < 0 || end_row < 0) { return posList; } int start_col_ = -1; int end_col_ = -1; for (int c = img.cols - 1;c >= start_col;c--) { if (!ImgDivider::isColEmpty(img, c, start_row, end_row)) { if (start_col_ < 0) { start_col_ = c; } end_col_ = c; } else { if (end_col_ > -1 && end_col_ - c > 5) { int* pos = (int*)malloc(sizeof(int) * 4); pos[0] = end_col_; pos[1] = start_row; pos[2] = start_col_; pos[3] = end_row; posList.push_back(pos); start_col_ = -1; end_col_ = -1; if (posList.size() >= 2) { break; } } } } if (start_col_ > -1 && end_col_ > -1 && posList.size()<2) { int* pos = (int*)malloc(sizeof(int) * 4); pos[0] = end_col_; pos[1] = start_row; pos[2] = start_col_; pos[3] = end_row; posList.push_back(pos); } return posList; } TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat img, list posList) { int c = 0; unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_TRADE_QUEUE_WIDTH * _NUMBER_TRADE_QUEUE_HEIGHT); for (int r = 0;r < _NUMBER_TRADE_QUEUE_HEIGHT;r++) { for (int c = 0;c < _NUMBER_TRADE_QUEUE_WIDTH;c++) { zeroData[r * _NUMBER_TRADE_QUEUE_WIDTH + c] = ImgUtil::NUMS_TRADE_QUEUE[0].data.ptr(r)[c]; } } int length_per_num = 8; int ele_count = 2; int* splitResult = (int*)malloc(sizeof(int) * 1 * 4 * ele_count); list::iterator ele = posList.begin(); int index = 1; for (list::iterator ele = posList.begin(); ele != posList.end(); ++ele) { splitResult[index * 4 + 0] = (*ele)[0]; splitResult[index * 4 + 1] = (*ele)[1]; splitResult[index * 4 + 2] = (*ele)[2]; splitResult[index * 4 + 3] = (*ele)[3]; index--; } int line_number_count = ele_count * length_per_num; unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count); unsigned char types[] = { NUM_TYPE_PRICE, NUM_TYPE_NUM}; UcharDataInfo typesData = UcharDataInfo(); typesData.length = ele_count; typesData.data = types; openCLExcuter->splitTradeQueueNum(img, IntDataInfo({ splitResult,(int)(ele_count * 1) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, ele_count, length_per_num); free(splitResult); uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count); openCLExcuter->createNumberTemplates(1, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count, ImgUtil::numsOneLevel_trade_queue, templateNums); uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, 1 * _NUMBER_TRADE_QUEUE_HEIGHT, _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count); TradeQueueResult result; for (int i = 0; i < ele_count; i++) { string st = ""; for (int j = 0; j < length_per_num; j++) { if (j == length_per_num - 2 && i==0) { st.append("."); } st.append(to_string(numberResult[0][length_per_num* i + j])); } if (i == 0) { result.buy_1_price = st; } else if (i == 1) { result.buy_1_volumn = stoi(st); } } free(numberResult[0]); int volumn = 0;//stoi(st); //printf("½á¹û:%d\n", volumn); free(totalNumberData); free(numberResult); free(zeroData); free(templateNums); img.release(); return result; } TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(HWND hwnd) { //Åжϴ°¿ÚÊÇ·ñÏÔʾ if (!Win32Util::isWindowShow(hwnd)) { throw string("½»Ò×´°¿Ú¾ä±ú²»¿É¼û"); } //---»ñÈ¡´úÂëÃû³Æ //»ñÈ¡½»Ò××Ó´°¿Ú HWND child = NULL; HWND trade_win = NULL; int count = 0; do { count++; child = FindWindowExA(hwnd, child, "#32770", NULL); if (child <= 0) { break; } HWND temp = FindWindowExA(child, NULL, "Edit", NULL); if (temp > 0) { trade_win = child; break; } } while (count < 5); if (!trade_win) { throw string("×Ó½»Ò×´°¿ÚδÕÒµ½"); } HWND code_name_win = GetDlgItem(trade_win, 0x000005C2); if (code_name_win <= 0) { throw string("´úÂëÃû³Æ¿Ø¼þδÕÒµ½"); } wstring code_name = Win32Util::getText(code_name_win); if (code_name.length() < 2) { throw string("½»Ò×´°¿ÚδÌî³ä´úÂë"); } //²éÕÒ½»Ò×¶ÓÁоä±ú HWND queue_win = NULL; child = NULL; count = 0; do { count++; child = FindWindowExA(hwnd, child, "#32770", NULL); if (child <= 0) { break; } if (child != trade_win) { if (Win32Util::isWindowShow(child)) { RECT rect; GetWindowRect(child, &rect); if (rect.bottom - rect.top > 100) { queue_win = child; break; } } } } while (count < 5); if (!queue_win) { throw string("½»Ò×¶ÓÁÐδÕÒµ½"); } string now_time = Win32Util::getNowTime(); cv::Mat img = CaptureUtil::capture(queue_win); uchar* data = (uchar*)malloc(sizeof(uchar) * img.cols * img.rows); if (img.channels() == 3) { openCLExcuter->rgb2ThresholdInvert(img.data, img.cols, img.rows, data, 150); } else if (img.channels() == 4) { openCLExcuter->rgba2ThresholdInvert(img.data, img.cols, img.rows, data, 150); } cv::Mat grayImg = cv::Mat::zeros(img.rows, img.cols, CV_8UC1); grayImg.data = data; img.release(); //string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gray.jpg"; //cv::imwrite(path, grayImg); list posList = split(grayImg); if (posList.size() < 2) { free(data); throw string("Êý¾Ý·Ö¸ô³ö´í"); } TradeQueueResult result = recognition_buy_1_volumn(grayImg, posList); for (list::iterator ele = posList.begin(); ele != posList.end(); ele++) { free(*ele); } free(data); //std::wstring_convert> converter; // wstring תstring£¬Ö§³ÖÖÐÎÄ const char* GBK_LOCALE_NAME = ".936"; wstring_convert> converter(new codecvt_byname(GBK_LOCALE_NAME)); result.code_name = converter.to_bytes(code_name); result.time = now_time; return result; } TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat grayImg) { list rowDataList = split(grayImg); return recognition_buy_1_volumn(grayImg, rowDataList); }