forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
135 lines (115 loc) · 3.23 KB
/
image.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mp
import (
"encoding/json"
"net/url"
"github.com/accaolei/gochat/wx"
"github.com/tidwall/gjson"
)
// ImageSize 图片尺寸
type ImageSize struct {
W int `json:"w"`
H int `json:"h"`
}
// Position 位置信息
type Position struct {
X int `json:"x"`
Y int `json:"y"`
}
// ImagePosition 图片位置
type ImagePosition struct {
LeftTop Position `json:"left_top"`
RightTop Position `json:"right_top"`
RightBottom Position `json:"right_bottom"`
LeftBottom Position `json:"left_bottom"`
}
// CropPosition 裁切位置
type CropPosition struct {
CropLeft int `json:"crop_left"`
CropTop int `json:"crop_top"`
CropRight int `json:"crop_right"`
CropBottom int `json:"crop_bottom"`
}
// AICropResult 图片裁切结果
type AICropResult struct {
Results []*CropPosition `json:"results"`
ImgSize ImageSize `json:"img_size"`
}
// AICrop 图片智能裁切
func AICrop(dest *AICropResult, filename string) wx.Action {
return wx.NewAPI(AICropURL,
wx.WithMethod(wx.MethodUpload),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// AICropByURL 图片智能裁切
func AICropByURL(dest *AICropResult, imgURL string) wx.Action {
return wx.NewAPI(AICropURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// QRCodeScanData 二维码扫描数据
type QRCodeScanData struct {
TypeName string `json:"type_name"`
Data string `json:"data"`
Pos ImagePosition `json:"pos"`
}
// QRCodeScanResult 二维码扫描结果
type QRCodeScanResult struct {
CodeResults []*QRCodeScanData `json:"code_results"`
ImgSize ImageSize `json:"img_size"`
}
// ScanQRCode 条码/二维码识别
func ScanQRCode(dest *QRCodeScanResult, filename string) wx.Action {
return wx.NewAPI(ScanQRCodeURL,
wx.WithMethod(wx.MethodUpload),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// ScanQRCodeByURL 条码/二维码识别
func ScanQRCodeByURL(dest *QRCodeScanResult, imgURL string) wx.Action {
return wx.NewAPI(ScanQRCodeURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// SuperreSolutionResult 图片高清化结果
type SuperreSolutionResult struct {
MediaID string `json:"media_id"`
}
// SuperreSolution 图片高清化
func SuperreSolution(dest *SuperreSolutionResult, filename string) wx.Action {
return wx.NewAPI(SuperreSolutionURL,
wx.WithMethod(wx.MethodUpload),
wx.WithUploadForm("img", filename, nil),
wx.WithDecode(func(resp []byte) error {
dest.MediaID = gjson.GetBytes(resp, "media_id").String()
return nil
}),
)
}
// SuperreSolutionByURL 图片高清化
func SuperreSolutionByURL(dest *SuperreSolutionResult, imgURL string) wx.Action {
query := url.Values{}
query.Set("img_url", imgURL)
return wx.NewAPI(SuperreSolutionURL,
wx.WithMethod(wx.MethodPost),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(resp []byte) error {
dest.MediaID = gjson.GetBytes(resp, "media_id").String()
return nil
}),
)
}