baidu.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package services
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/http"
  8. "hongze/hz_crm_api/utils"
  9. "net/url"
  10. )
  11. /*
  12. //名片识别
  13. func init() {
  14. fmt.Println("start")
  15. services.GetBusinessCard()
  16. fmt.Println("end")
  17. }
  18. */
  19. type BdAccessToken struct {
  20. AccessToken string `json:"access_token"`
  21. Error string
  22. }
  23. func GetAccessToken() (accessTokenStr string, err error) {
  24. baiduApiKey := "nMfRuZ0qBCsVWfGGKekaoMqh"
  25. baiduBdSecretKey := "N6xcrUizYijMf47iTDDyPBm3lTOrvVSF"
  26. getUrl := `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=` + baiduApiKey + `&client_secret=` + baiduBdSecretKey + `&`
  27. body, err := http.Post(getUrl, "")
  28. var accessToken BdAccessToken
  29. err = json.Unmarshal(body, &accessToken)
  30. if err != nil {
  31. fmt.Println("err:" + err.Error())
  32. return
  33. }
  34. if accessToken.Error != "" {
  35. err = errors.New("获取token失败,Err:" + accessToken.Error)
  36. return
  37. }
  38. accessTokenStr = accessToken.AccessToken
  39. return
  40. }
  41. type BdBussinessCard struct {
  42. Errno int `json:"error_code"`
  43. ErrorMsg string `json:"error_msg"`
  44. Logid string
  45. WordsResultNum int
  46. WordsResult struct {
  47. ADDR []string `json:"ADDR"`
  48. FAX []string `json:"FAX"`
  49. MOBILE []string `json:"MOBILE"`
  50. NAME []string `json:"NAME"`
  51. PC []string `json:"PC"`
  52. URL []string `json:"URL"`
  53. TEL []string `json:"TEL"`
  54. COMPANY []string `json:"COMPANY"`
  55. TITLE []string `TITLE`
  56. EMAIL []string `EMAIL`
  57. } `json:"words_result"`
  58. //{"log_id":1332206693559304192,"error_msg":"target detect error","error_code":282102}
  59. }
  60. func GetBusinessCard(imgUrl string) (item *BdBussinessCard, err error) {
  61. imgBody, err := http.Get(imgUrl)
  62. if err != nil {
  63. return
  64. }
  65. //rnStr := utils.GetRandStringNoSpecialChar(5)
  66. //savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
  67. //err = file.SaveFile(imgBody, savePath)
  68. //if err != nil {
  69. // return
  70. //}
  71. //defer func() {
  72. // os.Remove(savePath)
  73. //}()
  74. accessToken, err := GetAccessToken()
  75. fmt.Println("accessToken")
  76. fmt.Println(accessToken)
  77. if err != nil {
  78. fmt.Print("GetAccessToken Err:" + err.Error())
  79. }
  80. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?access_token=` + accessToken
  81. //ff, _ := os.Open(savePath)
  82. //defer ff.Close()
  83. //sourcebuffer := make([]byte, 500000)
  84. //n, _ := ff.Read(sourcebuffer)
  85. //sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
  86. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  87. imgData := imageBase64
  88. param := url.Values{}
  89. param.Set("image", imgData)
  90. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  91. fmt.Println("body:", string(cardBody))
  92. //fmt.Println(string(cardBody))
  93. //utils.FileLog.Info("cardBody:%s" + string(cardBody))
  94. utils.FileLog.Info(fmt.Sprintf("baidu card;imgUrl:%s; Body:%s", imgUrl, string(cardBody)))
  95. if err != nil {
  96. fmt.Println("err:", err.Error())
  97. return
  98. }
  99. err = json.Unmarshal(cardBody, &item)
  100. if err != nil {
  101. fmt.Println("line err:", err.Error())
  102. }
  103. return
  104. }
  105. type CardAccurate struct {
  106. WordsResultNum int `json:"words_result_num"`
  107. WordsResult []struct {
  108. Words string `json:"words"`
  109. } `json:"words_result"`
  110. ErrorCode int `json:"error_code"`
  111. ErrorMsg string `json:"error_msg"`
  112. }
  113. // 通用文字(高精度)
  114. func GetBusinessCardByAccurate(imgUrl string) (item *CardAccurate, err error) {
  115. imgBody, err := http.Get(imgUrl)
  116. if err != nil {
  117. return
  118. }
  119. //rnStr := utils.GetRandStringNoSpecialChar(5)
  120. //savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
  121. //err = file.SaveFile(imgBody, savePath)
  122. //if err != nil {
  123. // return
  124. //}
  125. //defer func() {
  126. // os.Remove(savePath)
  127. //}()
  128. //
  129. //nowStr:=time.Now().Format(utils.FormatDateTimeUnSpace)
  130. //outPath := "./"+nowStr+".jpg"
  131. //缩放
  132. //file, err := os.Open(savePath)
  133. //if err != nil {
  134. // fmt.Println("file err:" + err.Error())
  135. // return
  136. //}
  137. //
  138. //// decode jpeg into image.Image
  139. //img, err := jpeg.Decode(file)
  140. //if err != nil {
  141. // fmt.Println("jpeg Decode:" + err.Error())
  142. // return
  143. //}
  144. //width:=img.Bounds().Dx()
  145. //height:=img.Bounds().Dy()
  146. //fmt.Println(width,height)
  147. //
  148. //if width>=4096 {
  149. // width=4000
  150. //}
  151. //
  152. //if height>=4096 {
  153. // height=4000
  154. //}
  155. //
  156. //file.Close()
  157. //
  158. //fmt.Println(width,height)
  159. //
  160. //m := resize.Resize(uint(width),uint(height), img, resize.Lanczos3)
  161. //
  162. //out, err := os.Create(outPath)
  163. //if err != nil {
  164. // fmt.Println("Create:" + err.Error())
  165. // return
  166. //}
  167. //defer out.Close()
  168. //
  169. //jpeg.Encode(out, m, nil)
  170. accessToken, err := GetAccessToken()
  171. fmt.Println("accessToken")
  172. fmt.Println(accessToken)
  173. if err != nil {
  174. fmt.Print("GetAccessToken Err:" + err.Error())
  175. }
  176. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=` + accessToken
  177. //ff, _ := os.Open(outPath)
  178. //defer ff.Close()
  179. //sourcebuffer := make([]byte, 500000)
  180. //n, _ := ff.Read(sourcebuffer)
  181. //sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
  182. //imgData := sourcestring
  183. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  184. param := url.Values{}
  185. param.Set("image", imageBase64)
  186. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  187. utils.FileLog.Info("GetBusinessCardByAccurate:%s" + string(cardBody))
  188. if err != nil {
  189. fmt.Println("err:", err.Error())
  190. return
  191. }
  192. err = json.Unmarshal(cardBody, &item)
  193. return
  194. }
  195. // 通用文字(高精度)
  196. func GetBdGeneralBasic(imgUrl string) (item *CardAccurate, err error) {
  197. imgBody, err := http.Get(imgUrl)
  198. if err != nil {
  199. return
  200. }
  201. accessToken, err := GetAccessToken()
  202. if err != nil {
  203. fmt.Print("GetAccessToken Err:" + err.Error())
  204. }
  205. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=` + accessToken
  206. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  207. param := url.Values{}
  208. param.Set("image", imageBase64)
  209. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  210. utils.FileLog.Info("GetBusinessCardByAccurate:%s" + string(cardBody))
  211. if err != nil {
  212. fmt.Println("err:", err.Error())
  213. return
  214. }
  215. err = json.Unmarshal(cardBody, &item)
  216. return
  217. }