1 #include <iostream>
2 #include "alibabacloud/core/AlibabaCloud.h"
3 #include "alibabacloud/ocr/OcrClient.h"
4 #include "alibabacloud/core/CommonClient.h"
5 #include "model/common.h"
6 
7 extern "C" {
8 using namespace std;
9 using namespace AlibabaCloud;
10 using namespace AlibabaCloud::Ocr;
11 
recognizeIdentityCardFaceSide(char * url,AIModelCBFunc cb)12 int recognizeIdentityCardFaceSide(char *url, AIModelCBFunc cb)
13 {
14     InitializeSdk();
15     string key = getAccessKey();
16     string secret = getAccessSecret();
17     ClientConfiguration configuration;
18     configuration.setRegionId(CLOUD_AI_REGION_ID);
19     configuration.setEndpoint(CLOUD_AI_OCR_ENDPOINT);
20     OcrClient client(key, secret, configuration);
21     Model::RecognizeIdentityCardRequest request;
22     string imageURL;
23     OcrResultStruct result;
24     string address, birthDate, gender, nationality, iDNumber;
25     int ret = 0, i;
26 
27     imageURL = url;
28     request.setScheme("http");
29     request.setMethod(HttpRequest::Method::Post);
30     request.setImageURL(imageURL);
31     request.setSide("face");
32 
33     auto outcome = client.recognizeIdentityCard(request);
34     cout << endl << "ocr describeInstances returned:" << endl;
35     cout << "error code: " << outcome.error().errorCode() << endl;
36     cout << "requestId: " << outcome.result().requestId() << endl << endl;
37     cout << "Address: " << outcome.result().getData().frontResult.address << endl;
38     cout << "BirthDate: " << outcome.result().getData().frontResult.birthDate << endl;
39     cout << "gender: " << outcome.result().getData().frontResult.gender << endl;
40     cout << "nationality: " << outcome.result().getData().frontResult.nationality << endl;
41     cout << "iDNumber: " << outcome.result().getData().frontResult.iDNumber << endl;
42     cout << "faceRectangle.x: " << outcome.result().getData().frontResult.faceRectangle.center.x << endl;
43     cout << "faceRectangle.y: " << outcome.result().getData().frontResult.faceRectangle.center.y << endl;
44     cout << "faceRectangle.width: " << outcome.result().getData().frontResult.faceRectangle.size.width << endl;
45     cout << "faceRectangle.height: " << outcome.result().getData().frontResult.faceRectangle.size.height << endl;
46     cout << "faceRectangle.angle: " << outcome.result().getData().frontResult.faceRectangle.angle << endl;
47     result.identity.face.location.x = outcome.result().getData().frontResult.faceRectangle.center.x;
48     result.identity.face.location.y = outcome.result().getData().frontResult.faceRectangle.center.y;
49     result.identity.face.location.w = outcome.result().getData().frontResult.faceRectangle.size.width;
50     result.identity.face.location.h = outcome.result().getData().frontResult.faceRectangle.size.height;
51 
52     for (i = 0; i < 4; i++) {
53         cout << i << "cardAreas.x: " << outcome.result().getData().frontResult.cardAreas[i].x << endl;
54         cout << i << "cardAreas.y: " << outcome.result().getData().frontResult.cardAreas[i].y << endl;
55         cout << i << "faceRectVertices.x: " << outcome.result().getData().frontResult.faceRectVertices[i].x << endl;
56         cout << i << "faceRectVertices.y: " << outcome.result().getData().frontResult.faceRectVertices[i].y << endl;
57 
58         result.identity.face.cardX[i] = outcome.result().getData().frontResult.cardAreas[i].x;
59         result.identity.face.cardY[i] = outcome.result().getData().frontResult.cardAreas[i].y;
60         result.identity.face.faceX[i] = outcome.result().getData().frontResult.faceRectVertices[i].x;
61         result.identity.face.faceY[i] = outcome.result().getData().frontResult.faceRectVertices[i].y;
62     }
63     address = outcome.result().getData().frontResult.address;
64     birthDate = outcome.result().getData().frontResult.birthDate;
65     gender = outcome.result().getData().frontResult.gender;
66     nationality = outcome.result().getData().frontResult.nationality;
67     iDNumber = outcome.result().getData().frontResult.iDNumber;
68     result.identity.face.address = (char *)address.c_str();
69     result.identity.face.birthDate = (char *)birthDate.c_str();
70     result.identity.face.gender = (char *)gender.c_str();
71     result.identity.face.nationality = (char *)nationality.c_str();
72     result.identity.face.iDNumber = (char *)iDNumber.c_str();
73 
74     if (cb) {
75         ret = cb((void *)&result);
76     }
77 
78     ShutdownSdk();
79     return ret;
80 }
81 
recognizeIdentityCardBackSide(char * url,AIModelCBFunc cb)82 int recognizeIdentityCardBackSide(char *url, AIModelCBFunc cb)
83 {
84     InitializeSdk();
85     string key = getAccessKey();
86     string secret = getAccessSecret();
87     ClientConfiguration configuration;
88     configuration.setRegionId(CLOUD_AI_REGION_ID);
89     configuration.setEndpoint(CLOUD_AI_OCR_ENDPOINT);
90     OcrClient client(key, secret, configuration);
91     Model::RecognizeIdentityCardRequest request;
92     OcrResultStruct result;
93     string startDate, issue, endDate;
94     string imageURL;
95     int ret = 0;
96 
97     imageURL = url;
98     request.setScheme("http");
99     request.setMethod(HttpRequest::Method::Post);
100     request.setImageURL(imageURL);
101     request.setSide("back");
102 
103     auto outcome = client.recognizeIdentityCard(request);
104     cout << endl << "ocr describeInstances returned:" << endl;
105     cout << "error code: " << outcome.error().errorCode() << endl;
106     cout << "requestId: " << outcome.result().requestId() << endl << endl;
107     cout << "startDate: " << outcome.result().getData().backResult.startDate << endl;
108     cout << "issue: " << outcome.result().getData().backResult.issue << endl;
109     cout << "endDate: " << outcome.result().getData().backResult.endDate << endl;
110     startDate = outcome.result().getData().backResult.startDate;
111     issue = outcome.result().getData().backResult.issue;
112     endDate = outcome.result().getData().backResult.endDate;
113     result.identity.back.startDate = (char *)startDate.c_str();
114     result.identity.back.issue = (char *)issue.c_str();
115     result.identity.back.endDate = (char *)endDate.c_str();
116 
117     if (cb) {
118         ret = cb((void *)&result);
119     }
120     ShutdownSdk();
121     return ret;
122 }
123 
recognizeBankCard(char * url,AIModelCBFunc cb)124 int recognizeBankCard(char *url, AIModelCBFunc cb)
125 {
126     InitializeSdk();
127     string key = getAccessKey();
128     string secret = getAccessSecret();
129     ClientConfiguration configuration;
130     configuration.setRegionId(CLOUD_AI_REGION_ID);
131     configuration.setEndpoint(CLOUD_AI_OCR_ENDPOINT);
132     OcrClient client(key, secret, configuration);
133     Model::RecognizeBankCardRequest request;
134     OcrResultStruct result;
135     string bankName, cardNumber, validDate;
136     string imageURL;
137     int ret = 0;
138 
139     imageURL = url;
140     request.setScheme("http");
141     request.setMethod(HttpRequest::Method::Post);
142     request.setImageURL(imageURL);
143 
144     auto outcome = client.recognizeBankCard(request);
145     cout << endl << "ocr describeInstances returned:" << endl;
146     cout << "error code: " << outcome.error().errorCode() << endl;
147     cout << "requestId: " << outcome.result().requestId() << endl << endl;
148     cout << "bankName: " << outcome.result().getData().bankName << endl;
149     cout << "cardNumber: " << outcome.result().getData().cardNumber << endl;
150     cout << "validDate: " << outcome.result().getData().validDate << endl;
151 
152     bankName = outcome.result().getData().bankName;
153     cardNumber = outcome.result().getData().cardNumber;
154     validDate = outcome.result().getData().validDate;
155     result.bank.bankName = (char *)bankName.c_str();
156     result.bank.cardNumber = (char *)cardNumber.c_str();
157     result.bank.validDate = (char *)validDate.c_str();
158 
159     if (cb) {
160         ret = cb((void *)&result);
161     }
162     ShutdownSdk();
163     return ret;
164 }
165 
recognizeCharacter(char * url,AIModelCBFunc cb)166 int recognizeCharacter(char *url, AIModelCBFunc cb)
167 {
168     InitializeSdk();
169     string key = getAccessKey();
170     string secret = getAccessSecret();
171     ClientConfiguration configuration;
172     configuration.setRegionId(CLOUD_AI_REGION_ID);
173     configuration.setEndpoint(CLOUD_AI_OCR_ENDPOINT);
174     OcrClient client(key, secret, configuration);
175     Model::RecognizeCharacterRequest request;
176     OcrResultStruct result;
177     string text;
178     string imageURL;
179     int ret = 0, i;
180 
181     imageURL = url;
182     request.setScheme("http");
183     request.setMethod(HttpRequest::Method::Post);
184     request.setImageURL(imageURL);
185     request.setMinHeight(10);
186     request.setOutputProbability(true);
187 
188     auto outcome = client.recognizeCharacter(request);
189     cout << endl << "ocr recognizeCharacter describeInstances returned:" << endl;
190     cout << "error code: " << outcome.error().errorCode() << endl;
191     cout << "requestId: " << outcome.result().requestId() << endl << endl;
192     cout << "results size: " << outcome.result().getData().results.size() << endl;
193 
194     for (i = 0; i < outcome.result().getData().results.size(); i++) {
195         cout << i << "text: " << outcome.result().getData().results[i].text << endl;
196         cout << i << "probability: " << outcome.result().getData().results[i].probability << endl;
197         cout << i << "text left: " << outcome.result().getData().results[i].textRectangles.left << endl;
198         cout << i << "text angle: " << outcome.result().getData().results[i].textRectangles.angle << endl;
199         cout << i << "text top: " << outcome.result().getData().results[i].textRectangles.top << endl;
200         cout << i << "text height: " << outcome.result().getData().results[i].textRectangles.height << endl;
201         cout << i << "text: width:" << outcome.result().getData().results[i].textRectangles.width << endl;
202         text = outcome.result().getData().results[i].text;
203         result.character.text = (char *)text.c_str();
204         result.character.probability = outcome.result().getData().results[i].probability;
205         result.character.left = outcome.result().getData().results[i].textRectangles.left;
206         result.character.angle = outcome.result().getData().results[i].textRectangles.angle;
207         result.character.top = outcome.result().getData().results[i].textRectangles.top;
208         result.character.height = outcome.result().getData().results[i].textRectangles.height;
209         result.character.width = outcome.result().getData().results[i].textRectangles.width;
210         if (cb) {
211             ret = cb((void *)&result);
212         }
213     }
214     ShutdownSdk();
215     return ret;
216 }
217 
218 }
219