-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
50 lines (44 loc) · 937 Bytes
/
console.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
package qrcode
import (
"bytes"
"fmt"
"image"
"image/color"
)
const (
black = 30
white = 47
)
func ConsolePrint(qrCode image.Image) {
// 将二维码转换为控制台颜色代码
pixels := convertQRCodeToConsolePixels(qrCode)
// 在控制台显示二维码
buffer := bytes.Buffer{}
for _, row := range pixels {
for _, pixel := range row {
if pixel == white {
buffer.WriteString("\033[47m \033[0m")
} else {
buffer.WriteString("\033[40m \033[0m")
}
}
buffer.WriteString("\n")
}
fmt.Println(buffer.String())
}
func convertQRCodeToConsolePixels(qr image.Image) [][]int {
bounds := qr.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
pixels := make([][]int, height)
for y := 0; y < height; y++ {
pixels[y] = make([]int, width)
for x := 0; x < width; x++ {
if qr.At(x, y) == color.Black {
pixels[y][x] = black
} else {
pixels[y][x] = white
}
}
}
return pixels
}