附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

img.go 4.7 KiB

2 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package weapp
  2. const (
  3. apiAICrop = "/cv/img/aicrop"
  4. apiScanQRCode = "/cv/img/qrcode"
  5. apiSuperResolution = "/cv/img/superResolution"
  6. )
  7. // AICropResponse 图片智能裁剪后的返回数据
  8. type AICropResponse struct {
  9. CommonError
  10. Results []struct {
  11. CropLeft uint `json:"crop_left"`
  12. CropTop uint `json:"crop_top"`
  13. CropRight uint `json:"crop_right"`
  14. CropBottom uint `json:"crop_bottom"`
  15. } `json:"results"`
  16. IMGSize struct {
  17. Width uint `json:"w"`
  18. Height uint `json:"h"`
  19. } `json:"img_size"`
  20. }
  21. // AICrop 本接口提供基于小程序的图片智能裁剪能力。
  22. func AICrop(token, filename string) (*AICropResponse, error) {
  23. api := baseURL + apiAICrop
  24. return aiCrop(api, token, filename)
  25. }
  26. func aiCrop(api, token, filename string) (*AICropResponse, error) {
  27. url, err := tokenAPI(api, token)
  28. if err != nil {
  29. return nil, err
  30. }
  31. res := new(AICropResponse)
  32. if err := postFormByFile(url, "img", filename, res); err != nil {
  33. return nil, err
  34. }
  35. return res, nil
  36. }
  37. // AICropByURL 本接口提供基于小程序的图片智能裁剪能力。
  38. func AICropByURL(token, url string) (*AICropResponse, error) {
  39. api := baseURL + apiAICrop
  40. return aiCropByURL(api, token, url)
  41. }
  42. func aiCropByURL(api, token, imgURL string) (*AICropResponse, error) {
  43. queries := requestQueries{
  44. "access_token": token,
  45. "img_url": imgURL,
  46. }
  47. url, err := encodeURL(api, queries)
  48. if err != nil {
  49. return nil, err
  50. }
  51. res := new(AICropResponse)
  52. if err := postJSON(url, nil, res); err != nil {
  53. return nil, err
  54. }
  55. return res, nil
  56. }
  57. // QRCodePoint 二维码角的位置
  58. type QRCodePoint struct {
  59. X uint `json:"x"`
  60. Y uint `json:"y"`
  61. }
  62. // ScanQRCodeResponse 小程序的条码/二维码识别后的返回数据
  63. type ScanQRCodeResponse struct {
  64. CommonError
  65. CodeResults []struct {
  66. TypeName string `json:"type_name"`
  67. Data string `json:"data"`
  68. Position struct {
  69. LeftTop QRCodePoint `json:"left_top"`
  70. RightTop QRCodePoint `json:"right_top"`
  71. RightBottom QRCodePoint `json:"right_bottom"`
  72. LeftBottom QRCodePoint `json:"left_bottom"`
  73. } `json:"pos"`
  74. } `json:"code_results"`
  75. IMGSize struct {
  76. Width uint `json:"w"`
  77. Height uint `json:"h"`
  78. } `json:"img_size"`
  79. }
  80. // ScanQRCode 本接口提供基于小程序的条码/二维码识别的API。
  81. func ScanQRCode(token, filename string) (*ScanQRCodeResponse, error) {
  82. api := baseURL + apiScanQRCode
  83. return scanQRCode(api, token, filename)
  84. }
  85. func scanQRCode(api, token, filename string) (*ScanQRCodeResponse, error) {
  86. url, err := tokenAPI(api, token)
  87. if err != nil {
  88. return nil, err
  89. }
  90. res := new(ScanQRCodeResponse)
  91. if err := postFormByFile(url, "img", filename, res); err != nil {
  92. return nil, err
  93. }
  94. return res, nil
  95. }
  96. // ScanQRCodeByURL 把网络文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。
  97. func ScanQRCodeByURL(token, imgURL string) (*ScanQRCodeResponse, error) {
  98. api := baseURL + apiScanQRCode
  99. return scanQRCodeByURL(api, token, imgURL)
  100. }
  101. func scanQRCodeByURL(api, token, imgURL string) (*ScanQRCodeResponse, error) {
  102. queries := requestQueries{
  103. "access_token": token,
  104. "img_url": imgURL,
  105. }
  106. url, err := encodeURL(api, queries)
  107. if err != nil {
  108. return nil, err
  109. }
  110. res := new(ScanQRCodeResponse)
  111. if err := postJSON(url, nil, res); err != nil {
  112. return nil, err
  113. }
  114. return res, nil
  115. }
  116. // SuperResolutionResponse 图片高清化后的返回数据
  117. type SuperResolutionResponse struct {
  118. CommonError
  119. MediaID string `json:"media_id"`
  120. }
  121. // SuperResolution 本接口提供基于小程序的图片高清化能力。
  122. func SuperResolution(token, filename string) (*SuperResolutionResponse, error) {
  123. api := baseURL + apiSuperResolution
  124. return superResolution(api, token, filename)
  125. }
  126. func superResolution(api, token, filename string) (*SuperResolutionResponse, error) {
  127. url, err := tokenAPI(api, token)
  128. if err != nil {
  129. return nil, err
  130. }
  131. res := new(SuperResolutionResponse)
  132. if err := postFormByFile(url, "img", filename, res); err != nil {
  133. return nil, err
  134. }
  135. return res, nil
  136. }
  137. // SuperResolutionByURL 把网络文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。
  138. func SuperResolutionByURL(token, imgURL string) (*SuperResolutionResponse, error) {
  139. api := baseURL + apiSuperResolution
  140. return superResolutionByURL(api, token, imgURL)
  141. }
  142. func superResolutionByURL(api, token, imgURL string) (*SuperResolutionResponse, error) {
  143. queries := requestQueries{
  144. "access_token": token,
  145. "img_url": imgURL,
  146. }
  147. url, err := encodeURL(api, queries)
  148. if err != nil {
  149. return nil, err
  150. }
  151. res := new(SuperResolutionResponse)
  152. if err := postJSON(url, nil, res); err != nil {
  153. return nil, err
  154. }
  155. return res, nil
  156. }