ocr.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package ocr
  2. import (
  3. "fmt"
  4. "net/url"
  5. "github.com/silenceper/wechat/v2/miniprogram/context"
  6. "github.com/silenceper/wechat/v2/util"
  7. )
  8. const (
  9. ocrIDCardURL = "https://api.weixin.qq.com/cv/ocr/idcard"
  10. ocrBankCardURL = "https://api.weixin.qq.com/cv/ocr/bankcard"
  11. ocrDrivingURL = "https://api.weixin.qq.com/cv/ocr/driving"
  12. ocrDrivingLicenseURL = "https://api.weixin.qq.com/cv/ocr/drivinglicense"
  13. ocrBizLicenseURL = "https://api.weixin.qq.com/cv/ocr/bizlicense"
  14. ocrCommonURL = "https://api.weixin.qq.com/cv/ocr/comm"
  15. )
  16. // OCR struct
  17. type OCR struct {
  18. *context.Context
  19. }
  20. // coordinate 坐标
  21. type coordinate struct {
  22. X int64 `json:"x,omitempty"`
  23. Y int64 `json:"y,omitempty"`
  24. }
  25. // position 位置
  26. type position struct {
  27. LeftTop coordinate `json:"left_top"`
  28. RightTop coordinate `json:"right_top"`
  29. RightBottom coordinate `json:"right_bottom"`
  30. LeftBottom coordinate `json:"left_bottom"`
  31. }
  32. // imageSize 图片尺寸
  33. type imageSize struct {
  34. Width int64 `json:"w,omitempty"`
  35. Height int64 `json:"h,omitempty"`
  36. }
  37. // ResDriving 行驶证返回结果
  38. type ResDriving struct {
  39. util.CommonError
  40. PlateNumber string `json:"plate_num,omitempty"`
  41. VehicleType string `json:"vehicle_type,omitempty"`
  42. Owner string `json:"owner,omitempty"`
  43. Address string `json:"addr,omitempty"`
  44. UseCharacter string `json:"use_character,omitempty"`
  45. Model string `json:"model,omitempty"`
  46. Vin string `json:"vin,omitempty"`
  47. EngineNumber string `json:"engine_num,omitempty"`
  48. RegisterDate string `json:"register_date,omitempty"`
  49. IssueDate string `json:"issue_date,omitempty"`
  50. PlateNumberB string `json:"plate_num_b,omitempty"`
  51. Record string `json:"record,omitempty"`
  52. PassengersNumber string `json:"passengers_num,omitempty"`
  53. TotalQuality string `json:"total_quality,omitempty"`
  54. PrepareQuality string `json:"prepare_quality,omitempty"`
  55. OverallSize string `json:"overall_size,omitempty"`
  56. CardPositionFront map[string]position `json:"card_position_front,omitempty"`
  57. CardPositionBack map[string]position `json:"card_position_back,omitempty"`
  58. ImageSize imageSize `json:"img_size,omitempty"`
  59. }
  60. // ResIDCard 身份证返回结果
  61. type ResIDCard struct {
  62. util.CommonError
  63. Type string `json:"type,omitempty"`
  64. Name string `json:"name,omitempty"`
  65. ID string `json:"id,omitempty"`
  66. Address string `json:"addr,omitempty"`
  67. Gender string `json:"gender,omitempty"`
  68. Nationality string `json:"nationality,omitempty"`
  69. ValidDate string `json:"valid_date,omitempty"`
  70. }
  71. // ResBankCard 银行卡返回结果
  72. type ResBankCard struct {
  73. util.CommonError
  74. Number string `json:"number,omitempty"`
  75. }
  76. // ResDrivingLicense 驾驶证返回结果
  77. type ResDrivingLicense struct {
  78. util.CommonError
  79. IDNumber string `json:"id_num,omitempty"`
  80. Name string `json:"name,omitempty"`
  81. Sex string `json:"sex,omitempty"`
  82. Nationality string `json:"nationality,omitempty"`
  83. Address string `json:"address,omitempty"`
  84. Birthday string `json:"birth_date,omitempty"`
  85. IssueDate string `json:"issue_date,omitempty"`
  86. CarClass string `json:"car_class,omitempty"`
  87. ValidFrom string `json:"valid_from,omitempty"`
  88. ValidTo string `json:"valid_to,omitempty"`
  89. OfficialSeal string `json:"official_seal,omitempty"`
  90. }
  91. // ResBizLicense 营业执照返回结果
  92. type ResBizLicense struct {
  93. util.CommonError
  94. RegisterNumber string `json:"reg_num,omitempty"`
  95. Serial string `json:"serial,omitempty"`
  96. LegalRepresentative string `json:"legal_representative,omitempty"`
  97. EnterpriseName string `json:"enterprise_name,omitempty"`
  98. TypeOfOrganization string `json:"type_of_organization,omitempty"`
  99. Address string `json:"address,omitempty"`
  100. TypeOfEnterprise string `json:"type_of_enterprise,omitempty"`
  101. BusinessScope string `json:"business_scope,omitempty"`
  102. RegisteredCapital string `json:"registered_capital,omitempty"`
  103. PaidInCapital string `json:"paid_in_capital,omitempty"`
  104. ValidPeriod string `json:"valid_period,omitempty"`
  105. RegisterDate string `json:"registered_date,omitempty"`
  106. CertPosition map[string]position `json:"cert_position,omitempty"`
  107. ImageSize imageSize `json:"img_size,omitempty"`
  108. }
  109. // ResCommon 公共印刷品返回结果
  110. type ResCommon struct {
  111. util.CommonError
  112. Items []commonItem `json:"items,omitempty"`
  113. ImageSize imageSize `json:"img_size,omitempty"`
  114. }
  115. // commonItem 公共元素
  116. type commonItem struct {
  117. Position position `json:"pos"`
  118. Text string `json:"text"`
  119. }
  120. // NewOCR 实例
  121. func NewOCR(c *context.Context) *OCR {
  122. ocr := new(OCR)
  123. ocr.Context = c
  124. return ocr
  125. }
  126. // IDCard 身份证OCR识别接口
  127. func (ocr *OCR) IDCard(path string) (resIDCard ResIDCard, err error) {
  128. accessToken, err := ocr.GetAccessToken()
  129. if err != nil {
  130. return
  131. }
  132. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrIDCardURL, url.QueryEscape(path), accessToken), "")
  133. if err != nil {
  134. return
  135. }
  136. err = util.DecodeWithError(response, &resIDCard, "OCRIDCard")
  137. return
  138. }
  139. // BankCard 银行卡OCR识别接口
  140. func (ocr *OCR) BankCard(path string) (resBankCard ResBankCard, err error) {
  141. accessToken, err := ocr.GetAccessToken()
  142. if err != nil {
  143. return
  144. }
  145. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBankCardURL, url.QueryEscape(path), accessToken), "")
  146. if err != nil {
  147. return
  148. }
  149. err = util.DecodeWithError(response, &resBankCard, "OCRBankCard")
  150. return
  151. }
  152. // Driving 行驶证OCR识别接口
  153. func (ocr *OCR) Driving(path string) (resDriving ResDriving, err error) {
  154. accessToken, err := ocr.GetAccessToken()
  155. if err != nil {
  156. return
  157. }
  158. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingURL, url.QueryEscape(path), accessToken), "")
  159. if err != nil {
  160. return
  161. }
  162. err = util.DecodeWithError(response, &resDriving, "OCRDriving")
  163. return
  164. }
  165. // DrivingLicense 驾驶证OCR识别接口
  166. func (ocr *OCR) DrivingLicense(path string) (resDrivingLicense ResDrivingLicense, err error) {
  167. accessToken, err := ocr.GetAccessToken()
  168. if err != nil {
  169. return
  170. }
  171. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingLicenseURL, url.QueryEscape(path), accessToken), "")
  172. if err != nil {
  173. return
  174. }
  175. err = util.DecodeWithError(response, &resDrivingLicense, "OCRDrivingLicense")
  176. return
  177. }
  178. // BizLicense 营业执照OCR识别接口
  179. func (ocr *OCR) BizLicense(path string) (resBizLicense ResBizLicense, err error) {
  180. accessToken, err := ocr.GetAccessToken()
  181. if err != nil {
  182. return
  183. }
  184. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBizLicenseURL, url.QueryEscape(path), accessToken), "")
  185. if err != nil {
  186. return
  187. }
  188. err = util.DecodeWithError(response, &resBizLicense, "OCRBizLicense")
  189. return
  190. }
  191. // Common 通用印刷体OCR识别接口
  192. func (ocr *OCR) Common(path string) (resCommon ResCommon, err error) {
  193. accessToken, err := ocr.GetAccessToken()
  194. if err != nil {
  195. return
  196. }
  197. response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrCommonURL, url.QueryEscape(path), accessToken), "")
  198. if err != nil {
  199. return
  200. }
  201. err = util.DecodeWithError(response, &resCommon, "OCRCommon")
  202. return
  203. }