admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "pch.h"
#include "OcrUtil.h"
#include "SocketManager.h"
#include <curl/curl.h>
#include "MyNetWorkManager.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 * 1024;
    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 {
                map<string, string> params;
                params["data"] = result_str;
                string result = MyNetWorkManager::getInstance()->PostHttpRequest(string("http://").append(SocketManager::ADDR).append(":").append(to_string(SocketManager::OCR_PORT)).append("/ocr") ,"",params);
                return parseOCRResult(result);
            }
            catch (int code) {
                if (code == -1) {
                    //Êý¾Ý²»ÍêÕû
                    Sleep(100);
                    continue;
                }
                else if (code == 1) {
                    //µÈ´ý¼ÌÐøÉÏ´«
                    break;
                }
                else {
                    break;
                }
            }
            catch (string st) {
                throw st;
            }
        }
    }
    throw string("ʶ±ðʧ°Ü");
}
 
string OcrUtil::code_ocr_by_name(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;
        Json::Value root;
        root["data"] = data;
        root["type"] = 101;
        string result_str = JsonUtil::toJsonStr(root);
        for (int i = 0; i < 10; i++)
        {
            try {
                map<string, string> params;
                params["data"] = result_str;
                string result = MyNetWorkManager::getInstance()->PostHttpRequest(string("http://").append(SocketManager::ADDR).append(":").append(to_string(SocketManager::OCR_PORT)).append("/ocr"), "", params);
                Json::Value resultJSON = JsonUtil::parseJson(result);
                if (resultJSON["code"].asInt() == 0) {
                    string code = resultJSON["data"]["code"].asString();
                    return code;
                }
                else {
                    throw data["code"].asInt();
                }
                throw string("ʶ±ð²»Æ¥Åä");
            }
            catch (int code) {
                if (code == -1) {
                    //Êý¾Ý²»ÍêÕû
                    Sleep(100);
                    continue;
                }
                else if (code == 1) {
                    //µÈ´ý¼ÌÐøÉÏ´«
                    break;
                }
                else {
                    break;
                }
            }
            catch (string st) {
                throw st;
            }
        }
    }
    throw string("ʶ±ðʧ°Ü");
}