forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.go
377 lines (315 loc) · 11.3 KB
/
ocr.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package mp
import (
"encoding/json"
"net/url"
"github.com/accaolei/gochat/wx"
"github.com/tidwall/gjson"
)
// OCRMode 识别模式
type OCRMode string
// 微信支持的识别模式
var (
OCRPhoto OCRMode = "photo" // 拍照模式
OCRScan OCRMode = "scan" // 扫描模式
)
// OCRPosition 识别位置
type OCRPosition struct {
Pos ImagePosition `json:"pos"`
}
// IDCardFront 身份证前面
type IDCardFront struct {
Name string `json:"name"`
ID string `json:"id"`
Addr string `json:"addr"`
Gender string `json:"gender"`
Nationality string `json:"nationality"`
}
// OCRIDCardFront 身份证前面识别
func OCRIDCardFront(dest *IDCardFront, mode OCRMode, filename string) wx.Action {
return wx.NewAPI(OCRIDCardURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// OCRIDCardFrontByURL 身份证前面识别
func OCRIDCardFrontByURL(dest *IDCardFront, mode OCRMode, imgURL string) wx.Action {
return wx.NewAPI(OCRIDCardURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// IDCardBack 身份证背面
type IDCardBack struct {
ValidDate string `json:"valid_date"`
}
// OCRIDCardBack 身份证背面识别
func OCRIDCardBack(dest *IDCardBack, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRIDCardURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
dest.ValidDate = gjson.GetBytes(resp, "valid_date").String()
return nil
}),
)
}
// OCRIDCardBackByURL 身份证背面识别
func OCRIDCardBackByURL(dest *IDCardBack, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRIDCardURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
dest.ValidDate = gjson.GetBytes(resp, "valid_date").String()
return nil
}),
)
}
// BankCard 银行卡
type BankCard struct {
ID string `json:"id"`
}
// OCRBankCard 银行卡识别
func OCRBankCard(dest *BankCard, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRBankCardURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
dest.ID = gjson.GetBytes(resp, "number").String()
return nil
}),
)
}
// OCRBankCardByURL 银行卡识别
func OCRBankCardByURL(dest *BankCard, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRBankCardURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
dest.ID = gjson.GetBytes(resp, "number").String()
return nil
}),
)
}
// PlateNumber 车牌号
type PlateNumber struct {
ID string `json:"id"`
}
// OCRPlateNumber 车牌号识别
func OCRPlateNumber(dest *PlateNumber, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRPlateNumberURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
dest.ID = gjson.GetBytes(resp, "number").String()
return nil
}),
)
}
// OCRPlateNumberByURL 车牌号识别
func OCRPlateNumberByURL(dest *PlateNumber, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRPlateNumberURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
dest.ID = gjson.GetBytes(resp, "number").String()
return nil
}),
)
}
// DriverLicense 驾照
type DriverLicense struct {
IDNum string `json:"id_num"` // 证号
Name string `json:"name"` // 姓名
Sex string `json:"sex"` // 性别
Nationality string `json:"nationality"` // 国籍
Address string `json:"address"` // 住址
BirthDate string `json:"birth_date"` // 出生日期
IssueDate string `json:"issue_date"` // 初次领证日期
CarClass string `json:"car_class"` // 准驾车型
ValidFrom string `json:"valid_from"` // 有效期限起始日
ValidTo string `json:"valid_to"` // 有效期限终止日
OfficialSeal string `json:"official_seal"` // 印章文字
}
// OCRDriverLicense 驾照识别
func OCRDriverLicense(dest *DriverLicense, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRDriverLicenseURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// OCRDriverLicenseByURL 驾照识别
func OCRDriverLicenseByURL(dest *DriverLicense, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRDriverLicenseURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// VehicleLicense 行驶证
type VehicleLicense struct {
VehicleType string `json:"vhicle_type"` // 车辆类型
Owner string `json:"owner"` // 所有人
Addr string `json:"addr"` // 住址
UseCharacter string `json:"use_character"` // 使用性质
Model string `json:"model"` // 品牌型号
VIN string `json:"vin"` // 车辆识别代号
EngineNum string `json:"engine_num"` // 发动机号码
RegisterDate string `json:"register_date"` // 注册日期
IssueDate string `json:"issue_date"` // 发证日期
PlateNum string `json:"plate_num"` // 车牌号码
PlateNumB string `json:"plate_num_b"` // 车牌号码
Record string `json:"record"` // 号牌
PassengersNum string `json:"passengers_num"` // 核定载人数
TotalQuality string `json:"total_quality"` // 总质量
PrepareQuality string `json:"prepare_quality"` // 整备质量
OverallSize string `json:"overall_size"` // 外廓尺寸
CardPositionFront OCRPosition `json:"card_position_front"` // 卡片正面位置(检测到卡片正面才会返回)
CardPositionBack OCRPosition `json:"card_position_back"` // 卡片反面位置(检测到卡片反面才会返回)
}
// OCRVehicleLicense 行驶证识别
func OCRVehicleLicense(dest *VehicleLicense, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRVehicleLicenseURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// OCRVehicleLicenseByURL 行驶证识别
func OCRVehicleLicenseByURL(dest *VehicleLicense, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRVehicleLicenseURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// BusinessLicense 营业执照
type BusinessLicense struct {
RegNum string `json:"reg_num"` // 注册号
Serial string `json:"serial"` // 编号
LegalRepresentative string `json:"legal_representative"` // 法定代表人姓名
EnterpriseName string `json:"enterprise_name"` // 企业名称
TypeOfOrganization string `json:"type_of_organization"` // 组成形式
Address string `json:"address"` // 经营场所/企业住所
TypeOfEnterprise string `json:"type_of_enterprise"` // 公司类型
BusinessScope string `json:"business_scope"` // 经营范围
RegisteredCapital string `json:"registered_capital"` // 注册资本
PaidInCapital string `json:"paid_in_capital"` // 实收资本
ValidPeriod string `json:"valid_period"` // 营业期限
RegisteredDate string `json:"registered_date"` // 注册日期/成立日期
CertPosition OCRPosition `json:"cert_position"` // 营业执照位置
ImgSize ImageSize `json:"img_size"` // 图片大小
}
// OCRBusinessLicense 营业执照识别
func OCRBusinessLicense(dest *BusinessLicense, mode OCRMode, filename string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
return wx.NewAPI(OCRBusinessLicenseURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// OCRBusinessLicenseByURL 营业执照识别
func OCRBusinessLicenseByURL(dest *BusinessLicense, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRBusinessLicenseURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// PrintedText 通用印刷体
type PrintedText struct {
Items []*PrintedTextItem `json:"items"`
ImgSize ImageSize `json:"img_size"`
}
// PrintedTextItem 通用印刷体内容项
type PrintedTextItem struct {
Text string `json:"text"`
Pos ImagePosition `json:"pos"`
}
// OCRPrintedText 通用印刷体识别
func OCRPrintedText(dest *PrintedText, mode OCRMode, filename string) wx.Action {
return wx.NewAPI(OCRPrintedTextURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mode)),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// OCRPrintedTextByURL 通用印刷体识别
func OCRPrintedTextByURL(dest *PrintedText, mode OCRMode, imgURL string) wx.Action {
query := url.Values{}
query.Set("type", string(mode))
query.Set("img_url", imgURL)
return wx.NewAPI(OCRPrintedTextURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}