-
Notifications
You must be signed in to change notification settings - Fork 1
/
picture.go.off
119 lines (105 loc) · 2.66 KB
/
picture.go.off
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package BaiduTranslate
import (
"bytes"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"github.com/tidwall/gjson"
)
func MD5(str string) string {
data := []byte(str)
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
return md5str
}
func (BaiduInfo *BaiduInfo) PictureTr(FilePath string, From string, To string, paste int) (string, string, string, []byte, error) {
// 读取图片文件
dat, err := os.Open(FilePath)
if err != nil {
log.Println("读取文件错误:" + err.Error())
}
// 准备post所需信息并计算sign
salt := Salt(10)
cuid := "APICUID"
mac := "mac"
appid := BaiduInfo.AppID
secretkey := BaiduInfo.SecretKey
montage := appid + fmt.Sprintf("%x", md5.Sum(dat)) + salt + cuid + mac + secretkey
sign := MD5(montage)
// HTTP POST
/*
info := make(map[string]interface{})
info["image"] = dat
info["from"] = From
info["to"] = To
info["appid"] = appid
info["salt"] = salt
info["sign"] = sign
info["cuid"] = cuid
info["mac"] = mac
info["paste"] = "1"
info["version"] = "3"
*/
defer dat.Close()
bodyBuffer := &bytes.Buffer{}
writer := multipart.NewWriter(bodyBuffer)
part, _ := writer.CreateFormFile("image", FilePath)
io.Copy(part, dat)
form := url.Values{
"image": {string(dat)},
"from": {From},
"to": {To},
"appid": {appid},
"salt": {salt},
"sign": {sign},
"cuid": {cuid},
"mac": {mac},
"paste": {"1"},
"version": {"3"},
}
reader := bytes.NewBufferString(form.Encode())
req, err := http.NewRequest("POST", "https://fanyi-api.baidu.com/api/trans/sdk/picure", reader)
if err != nil {
log.Println(err.Error())
}
req.Header.Add("Content-Type", "multipart/form-data")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err.Error())
}
// json解析
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err.Error())
}
errorCode := gjson.Get(string(body), "error_code").String()
errorMsg := gjson.Get(string(body), "error_msg").String()
sumSrc := gjson.Get(string(body), "data.sumSrc").String()
sumDst := gjson.Get(string(body), "data.sumDst").String()
var imgByte []byte
var pasteImg string
if paste == 1 {
pasteImg := gjson.Get(string(body), "data.pasteImg").String()
imgByte, err = base64.StdEncoding.DecodeString(pasteImg)
if err != nil {
log.Println(err.Error())
}
} else {
pasteImg = ""
}
if errorCode != "0" {
err := errors.New("错误码:" + errorCode + ",错误信息:" + errorMsg)
return sign, string(body), fmt.Sprintf("%v", form), imgByte, err
} else {
return sumSrc, sumDst, pasteImg, imgByte, nil
}
}