123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package services
- import (
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "net/url"
- )
- type BdAccessToken struct {
- AccessToken string `json:"access_token"`
- Error string
- }
- func GetAccessToken() (accessTokenStr string, err error) {
- baiduApiKey := "nMfRuZ0qBCsVWfGGKekaoMqh"
- baiduBdSecretKey := "N6xcrUizYijMf47iTDDyPBm3lTOrvVSF"
- getUrl := `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=` + baiduApiKey + `&client_secret=` + baiduBdSecretKey + `&`
- body, err := http.Post(getUrl, "")
- var accessToken BdAccessToken
- err = json.Unmarshal(body, &accessToken)
- if err != nil {
- fmt.Println("err:" + err.Error())
- return
- }
- if accessToken.Error != "" {
- err = errors.New("获取token失败,Err:" + accessToken.Error)
- return
- }
- accessTokenStr = accessToken.AccessToken
- return
- }
- type BdBussinessCard struct {
- Errno int `json:"error_code"`
- ErrorMsg string `json:"error_msg"`
- Logid string
- WordsResultNum int
- WordsResult struct {
- ADDR []string `json:"ADDR"`
- FAX []string `json:"FAX"`
- MOBILE []string `json:"MOBILE"`
- NAME []string `json:"NAME"`
- PC []string `json:"PC"`
- URL []string `json:"URL"`
- TEL []string `json:"TEL"`
- COMPANY []string `json:"COMPANY"`
- TITLE []string `TITLE`
- EMAIL []string `EMAIL`
- } `json:"words_result"`
-
- }
- func GetBusinessCard(imgUrl string) (item *BdBussinessCard, err error) {
- imgBody, err := http.Get(imgUrl)
- if err != nil {
- return
- }
-
-
-
-
-
-
-
-
-
- accessToken, err := GetAccessToken()
- fmt.Println("accessToken")
- fmt.Println(accessToken)
- if err != nil {
- fmt.Print("GetAccessToken Err:" + err.Error())
- }
- bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?access_token=` + accessToken
-
-
-
-
-
- imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
- imgData := imageBase64
- param := url.Values{}
- param.Set("image", imgData)
- cardBody, err := http.Post(bussinessCardUrl, param.Encode())
- fmt.Println("body:", string(cardBody))
-
- if err != nil {
- fmt.Println("err:", err.Error())
- return
- }
- err = json.Unmarshal(cardBody, &item)
- if err != nil {
- fmt.Println("line err:", err.Error())
- }
- return
- }
- type CardAccurate struct {
- WordsResultNum int `json:"words_result_num"`
- WordsResult []struct {
- Words string `json:"words"`
- } `json:"words_result"`
- ErrorCode int `json:"error_code"`
- ErrorMsg string `json:"error_msg"`
- }
- func GetBusinessCardByAccurate(imgUrl string) (item *CardAccurate, err error) {
- imgBody, err := http.Get(imgUrl)
- if err != nil {
- return
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- accessToken, err := GetAccessToken()
- fmt.Println("accessToken")
- fmt.Println(accessToken)
- if err != nil {
- fmt.Print("GetAccessToken Err:" + err.Error())
- }
- bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=` + accessToken
-
-
-
-
-
-
- imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
- param := url.Values{}
- param.Set("image", imageBase64)
- cardBody, err := http.Post(bussinessCardUrl, param.Encode())
- if err != nil {
- fmt.Println("err:", err.Error())
- return
- }
- err = json.Unmarshal(cardBody, &item)
- return
- }
- func GetBdGeneralBasic(imgUrl string) (item *CardAccurate, err error) {
- imgBody, err := http.Get(imgUrl)
- if err != nil {
- return
- }
- accessToken, err := GetAccessToken()
- if err != nil {
- fmt.Print("GetAccessToken Err:" + err.Error())
- }
- bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=` + accessToken
- imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
- param := url.Values{}
- param.Set("image", imageBase64)
- cardBody, err := http.Post(bussinessCardUrl, param.Encode())
- if err != nil {
- fmt.Println("err:", err.Error())
- return
- }
- err = json.Unmarshal(cardBody, &item)
- return
- }
|