-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (76 loc) · 1.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
package main
import (
"bufio"
"fmt"
"image/png"
"os"
"strconv"
"strings"
"time"
"github.com/kbinani/screenshot"
"github.com/signintech/gopdf"
)
func main() {
s := bufio.NewScanner(os.Stdin)
for {
pdfName := Scan(s, "保存するPDFファイル名を入力してください")
imageCount, err := strconv.Atoi(Scan(s, "何枚撮影するかを入力してください"))
if err != nil {
fmt.Println("撮影枚数は数字で入力してください。")
continue
}
imageTime, err := strconv.Atoi(Scan(s, "何秒間隔で撮影するか入力してください"))
if err != nil {
fmt.Println("撮影間隔は数字で入力してください。")
continue
}
images := ScreenToImage(imageCount, imageTime)
ImageToPDF(pdfName, images)
c := Scan(s, "まだPDFを作成しますか?y/N")
if strings.ToLower(c) != "y" {
break
}
}
}
func Scan(s *bufio.Scanner, text string) string {
for {
fmt.Print(text, ": ")
s.Scan()
input := s.Text()
if input != "" {
return input
}
}
}
func ScreenToImage(imageCount, imageTime int) []string {
var images []string
fmt.Println("3秒後に撮影を開始します。")
time.Sleep(3 * time.Second)
for i := 0; i < imageCount; i++ {
bounds := screenshot.GetDisplayBounds(1)
img, _ := screenshot.CaptureRect(bounds)
fileName := fmt.Sprintf("tmp_%d.png", i)
file, _ := os.Create(fileName)
defer file.Close()
png.Encode(file, img)
images = append(images, fileName)
time.Sleep(time.Duration(imageTime) * time.Second)
}
return images
}
func ImageToPDF(pdfName string, images []string) {
defer DeleteTmpPNG(images)
pdf := gopdf.GoPdf{}
SIZE := gopdf.Rect{W: 1920, H: 1080}
pdf.Start(gopdf.Config{PageSize: SIZE})
for _, image := range images {
pdf.AddPage()
pdf.Image(image, 0, 0, &SIZE)
}
pdf.WritePdf(fmt.Sprint(pdfName, ".pdf"))
}
func DeleteTmpPNG(images []string) {
for _, image := range images {
os.Remove(image)
}
}