ocr.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrIDCardURL, url.QueryEscape(path), accessToken)
  139. response, err := util.HTTPPost(uri, "")
  140. if err != nil {
  141. return
  142. }
  143. err = util.DecodeWithError(response, &ResIDCard, "OCRIDCard")
  144. return
  145. }
  146. //BankCard 银行卡OCR识别接口
  147. func (ocr *OCR) BankCard(path string) (ResBankCard ResBankCard, err error) {
  148. accessToken, err := ocr.GetAccessToken()
  149. if err != nil {
  150. return
  151. }
  152. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBankCardURL, url.QueryEscape(path), accessToken)
  153. response, err := util.HTTPPost(uri, "")
  154. if err != nil {
  155. return
  156. }
  157. err = util.DecodeWithError(response, &ResBankCard, "OCRBankCard")
  158. return
  159. }
  160. //Driving 行驶证OCR识别接口
  161. func (ocr *OCR) Driving(path string) (ResDriving ResDriving, err error) {
  162. accessToken, err := ocr.GetAccessToken()
  163. if err != nil {
  164. return
  165. }
  166. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingURL, url.QueryEscape(path), accessToken)
  167. response, err := util.HTTPPost(uri, "")
  168. if err != nil {
  169. return
  170. }
  171. err = util.DecodeWithError(response, &ResDriving, "OCRDriving")
  172. return
  173. }
  174. //DrivingLicense 驾驶证OCR识别接口
  175. func (ocr *OCR) DrivingLicense(path string) (ResDrivingLicense ResDrivingLicense, err error) {
  176. accessToken, err := ocr.GetAccessToken()
  177. if err != nil {
  178. return
  179. }
  180. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingLicenseURL, url.QueryEscape(path), accessToken)
  181. response, err := util.HTTPPost(uri, "")
  182. if err != nil {
  183. return
  184. }
  185. err = util.DecodeWithError(response, &ResDrivingLicense, "OCRDrivingLicense")
  186. return
  187. }
  188. //BizLicense 营业执照OCR识别接口
  189. func (ocr *OCR) BizLicense(path string) (ResBizLicense ResBizLicense, err error) {
  190. accessToken, err := ocr.GetAccessToken()
  191. if err != nil {
  192. return
  193. }
  194. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBizLicenseURL, url.QueryEscape(path), accessToken)
  195. response, err := util.HTTPPost(uri, "")
  196. if err != nil {
  197. return
  198. }
  199. err = util.DecodeWithError(response, &ResBizLicense, "OCRBizLicense")
  200. return
  201. }
  202. //Common 通用印刷体OCR识别接口
  203. func (ocr *OCR) Common(path string) (ResCommon ResCommon, err error) {
  204. accessToken, err := ocr.GetAccessToken()
  205. if err != nil {
  206. return
  207. }
  208. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrCommonURL, url.QueryEscape(path), accessToken)
  209. response, err := util.HTTPPost(uri, "")
  210. if err != nil {
  211. return
  212. }
  213. err = util.DecodeWithError(response, &ResCommon, "OCRCommon")
  214. return
  215. }
  216. //PlateNumber 车牌OCR识别接口
  217. func (ocr *OCR) PlateNumber(path string) (ResPlateNumber ResPlateNumber, err error) {
  218. accessToken, err := ocr.GetAccessToken()
  219. if err != nil {
  220. return
  221. }
  222. uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrPlateNumberURL, url.QueryEscape(path), accessToken)
  223. response, err := util.HTTPPost(uri, "")
  224. if err != nil {
  225. return
  226. }
  227. err = util.DecodeWithError(response, &ResPlateNumber, "OCRPlateNumber")
  228. return
  229. }