admin
2023-02-09 125db633619a0b4c7bd1d498ea2bf1cefa4f73d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "pch.h"
#include "OcrUtil.h"
#include "SocketManager.h"
 
list<OCRResult> OcrUtil::mat_ocr(string key_regex, cv::Mat mat)
{
 
    int len = mat.rows * mat.cols;
    chrono::time_point<chrono::system_clock, chrono::microseconds> tpMicro
        = chrono::time_point_cast<chrono::microseconds>(chrono::system_clock::now());
    // (΢Ã뾫¶ÈµÄ)ʱ¼äµã => (΢Ã뾫¶ÈµÄ)ʱ¼ä´Á
    time_t totalMicroSeconds = tpMicro.time_since_epoch().count();
    string matId = string(to_string(totalMicroSeconds)).append("_").append(to_string(mat.rows)).append("x").append(to_string(mat.cols)).append("_").append(to_string(rand()));
 
    int BUFFER_SIZE = 1024 * 2;
    int maxSize = mat.rows * mat.cols;
    int maxIndex = maxSize / BUFFER_SIZE;
 
    //Éú³ÉmatID
    //·Ö¿é·¢ËÍ£¬Ã¿Ò»ÐÐΪ1¿é
    for (int index = 0; index <= maxIndex; index++) {
        Json::Value array;
        for (int c = 0; c < BUFFER_SIZE; c++) {
            int dataIndex = index * BUFFER_SIZE + c;
            if (dataIndex >= maxSize) {
                break;
            }
            array[c] = mat.data[index * BUFFER_SIZE + c];
        }
        Json::Value data;
        data["data"] = array;
        data["matId"] = matId;
        data["index"] = index;
        data["maxIndex"] = maxIndex;
        data["width"] = mat.cols;
        data["height"] = mat.rows;
        data["key"] = key_regex.c_str();
        Json::Value root;
        root["data"] = data;
        root["type"] = 100;
        string result_str = JsonUtil::toJsonStr(root);
        for (int i = 0; i < 10; i++)
        {
            try {
                string result = SocketManager::sendOcrMsg(result_str.c_str());
                return parseOCRResult(result);
            }
            catch (int code) {
                if (code == -1) {
                    //Êý¾Ý²»ÍêÕû
                    Sleep(100);
                    continue;
                }
                else if (code == 1) {
                    //µÈ´ý¼ÌÐøÉÏ´«
                    break;
                }
                else {
                    break;
                }
            }
            catch (...) {
            }
        }
    }
    throw string("ʶ±ðʧ°Ü");
}