ocr.go 8.2 KB

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