aliyun.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. rhttp "github.com/rdlucklib/rdluck_tools/http"
  11. )
  12. const (
  13. AlAppKey = "203889624"
  14. AlAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
  15. AlAppCode = "22553c4ba74545568aba70ac6cfd441d"
  16. )
  17. type OcrItem struct {
  18. Image string `json:"image"`
  19. }
  20. type OcrResult struct {
  21. Addr []string `json:"addr"`
  22. Company []string `json:"company"`
  23. Success bool `json:"success"`
  24. TelCell []string `json:"tel_cell"`
  25. }
  26. ///ocr_business_card.json
  27. func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
  28. postUrl := `https://dm-57.data.aliyun.com/rest/160601/ocr/ocr_business_card.json`
  29. imgBody, err := rhttp.Get(imgUrl)
  30. if err != nil {
  31. return
  32. }
  33. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  34. item := new(OcrItem)
  35. item.Image = imageBase64
  36. jsonBody, err := json.Marshal(item)
  37. if err != nil {
  38. fmt.Println("json.Marshal Err:" + err.Error())
  39. return
  40. }
  41. tr := &http.Transport{
  42. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  43. }
  44. client := &http.Client{Transport: tr}
  45. req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
  46. if err != nil {
  47. fmt.Println("err:" + err.Error())
  48. return
  49. }
  50. req.Header.Set("Content-Type", "application/json")
  51. req.Header.Set("Authorization", "APPCODE "+AlAppCode)
  52. resp, err := client.Do(req)
  53. if err != nil {
  54. fmt.Println("client.Do Err:" + err.Error())
  55. return
  56. }
  57. fmt.Println("resp")
  58. fmt.Println(resp)
  59. defer resp.Body.Close()
  60. body, err := ioutil.ReadAll(resp.Body)
  61. if err != nil {
  62. fmt.Println("err:" + err.Error())
  63. return
  64. }
  65. fmt.Println("body:", string(body))
  66. err = json.Unmarshal(body, &result)
  67. return
  68. }