-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (129 loc) · 3.91 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
"tool-image/cache"
)
func main() {
checkDirections()
stReader := bufio.NewReader(os.Stdin)
fmt.Printf("请输入操作类型(1=裁剪图片, 2=合并图片):")
kind, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + kind)
return
}
kind = clearString(kind)
k := parseToInt(kind)
if k == 2 {
fmt.Printf("请输入画布宽:")
bgWidth, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + bgWidth)
return
}
fmt.Printf("请输入画布高:")
bgHeight, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + bgHeight)
return
}
fmt.Printf("请输入图片宽:")
imgWidth, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + imgWidth)
return
}
fmt.Printf("请输入图片高:")
imgHeight, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + imgHeight)
return
}
fmt.Printf("请输入图片之间的间隔高度:")
space, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + space)
return
}
bgWidth = clearString(bgWidth)
bgHeight = clearString(bgHeight)
imgWidth = clearString(imgWidth)
imgHeight = clearString(imgHeight)
space = clearString(space)
fmt.Println(fmt.Sprintf("即将合并图片,画布尺寸:{width: %s, height:%s}, 图片尺寸:{width: %s, height: %s}, 间隔:%s ", bgWidth, bgHeight, imgWidth,imgHeight, space))
cache.MergeImages("images/input", "images/output/", "images/bg.jpg", cache.Vector2{X: parseToInt(imgWidth), Y: parseToInt(imgHeight)},
cache.Vector2{X: parseToInt(bgWidth), Y: parseToInt(bgHeight)}, parseToInt(space))
fmt.Println("处理完成!!!")
}else if k == 1 {
fmt.Printf("请输入图片宽:")
imgWidth, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + imgWidth)
return
}
fmt.Printf("请输入图片高:")
imgHeight, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + imgHeight)
return
}
fmt.Printf("请输入裁剪图片顶边距:")
top, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + top)
return
}
fmt.Printf("请输入裁剪图片左边距:")
left, err := stReader.ReadString('\n')
if err != nil {
fmt.Println("There were errors reading, exiting program." + left)
return
}
imgWidth = clearString(imgWidth)
imgHeight = clearString(imgHeight)
top = clearString(top)
left = clearString(left)
fmt.Println(fmt.Sprintf("即将裁剪图片,图片边距:{top: %s, left:%s}, 图片尺寸:{width: %s, height: %s}", top, left, imgWidth,imgHeight))
cache.ClipImages("images/input","images/output/", cache.Vector2{X: parseToInt(imgWidth), Y: parseToInt(imgHeight)},
cache.Vector2{X: parseToInt(top), Y: parseToInt(left)})
fmt.Println("处理完成!!!")
}else{
fmt.Println("不合法的操作类型.")
}
for {
time.Sleep(time.Second * time.Duration(100))
}
}
func clearString(msg string) string {
msg = strings.Replace(msg,"\n", "", -1)
msg = strings.Replace(msg,"\r", "", -1)
return msg
}
func parseToInt(msg string) uint {
num,err := strconv.ParseInt(msg, 10, 64)
if err != nil {
fmt.Println(err.Error())
return 0
}
return uint(num)
}
func checkDirections() {
input := "images/input"
output := "images/output"
if !cache.PathIsExist(input) {
fmt.Println("相对路径下找不到源文件夹:/images/origin")
return
}
if !cache.PathIsExist(output) {
err := os.MkdirAll(output, 0644)
if err != nil {
fmt.Println(err.Error())
return
}
}
}