forked from shenghui0779/sdk-go
/
image.go
201 lines (164 loc) · 4.39 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package minip
import (
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
// 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"`
}
// ResultAICrop 图片裁切结果
type ResultAICrop struct {
Results []*CropPosition `json:"results"`
ImgSize ImageSize `json:"img_size"`
}
// AICrop 图像处理 - 图片智能裁切
func AICrop(imgPath string, result *ResultAICrop) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.MinipAICrop,
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// AICropByURL 图像处理 - 图片智能裁切
func AICropByURL(imgURL string, result *ResultAICrop) wx.Action {
return wx.NewPostAction(urls.MinipAICrop,
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// QRCodeScanData 二维码扫描数据
type QRCodeScanData struct {
TypeName string `json:"type_name"`
Data string `json:"data"`
Pos ImagePosition `json:"pos"`
}
// ResultQRCodeScan 二维码扫描结果
type ResultQRCodeScan struct {
CodeResults []*QRCodeScanData `json:"code_results"`
ImgSize ImageSize `json:"img_size"`
}
// ScanQRCode 图像处理 - 条码/二维码识别
func ScanQRCode(imgPath string, result *ResultQRCodeScan) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.MinipScanQRCode,
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ScanQRCodeByURL 图像处理 - 条码/二维码识别
func ScanQRCodeByURL(imgURL string, result *ResultQRCodeScan) wx.Action {
return wx.NewPostAction(urls.MinipScanQRCode,
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultSuperreSolution 图片高清化结果
type ResultSuperreSolution struct {
MediaID string `json:"media_id"`
}
// SuperreSolution 图像处理 - 图片高清化
func SuperreSolution(imgPath string, result *ResultSuperreSolution) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.MinipSuperreSolution,
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// SuperreSolutionByURL 图像处理 - 图片高清化
func SuperreSolutionByURL(imgURL string, result *ResultSuperreSolution) wx.Action {
return wx.NewPostAction(urls.MinipSuperreSolution,
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}