diff options
Diffstat (limited to 'vendor/github.com/Baozisoftware/qrcode-terminal-go')
4 files changed, 247 insertions, 0 deletions
diff --git a/vendor/github.com/Baozisoftware/qrcode-terminal-go/.gitignore b/vendor/github.com/Baozisoftware/qrcode-terminal-go/.gitignore new file mode 100644 index 00000000..daf913b1 --- /dev/null +++ b/vendor/github.com/Baozisoftware/qrcode-terminal-go/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/Baozisoftware/qrcode-terminal-go/LICENSE b/vendor/github.com/Baozisoftware/qrcode-terminal-go/LICENSE new file mode 100644 index 00000000..72223a6e --- /dev/null +++ b/vendor/github.com/Baozisoftware/qrcode-terminal-go/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2017, Baozisoftware +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/Baozisoftware/qrcode-terminal-go/README.md b/vendor/github.com/Baozisoftware/qrcode-terminal-go/README.md new file mode 100644 index 00000000..40e850ad --- /dev/null +++ b/vendor/github.com/Baozisoftware/qrcode-terminal-go/README.md @@ -0,0 +1,39 @@ + +# qrcode-terminal-go +QRCode terminal for golang. + +# Example +```go +package main + +import "github.com/Baozisoftware/qrcode-terminal-go" + +func main() { + Test1() + Test2() +} + +func Test1(){ + content := "Hello, 世界" + obj := qrcodeTerminal.New() + obj.Get(content).Print() +} + +func Test2(){ + content := "https://github.com/Baozisoftware/qrcode-terminal-go" + obj := qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightBlue,qrcodeTerminal.ConsoleColors.BrightGreen,qrcodeTerminal.QRCodeRecoveryLevels.Low) + obj.Get([]byte(content)).Print() +} +``` + +## Screenshots +### Windows XP +![winxp](https://github.com/Baozisoftware/qrcode-terminal-go/blob/master/screenshots/winxp.png) +### Windows 7 +![win7](https://github.com/Baozisoftware/qrcode-terminal-go/blob/master/screenshots/win7.png) +### Windows 10 +![win10](https://github.com/Baozisoftware/qrcode-terminal-go/blob/master/screenshots/win10.png) +### Ubuntu +![ubuntu](https://github.com/Baozisoftware/qrcode-terminal-go/blob/master/screenshots/ubuntu.png) +### macOS +![macos](https://github.com/Baozisoftware/qrcode-terminal-go/blob/master/screenshots/macos.png) diff --git a/vendor/github.com/Baozisoftware/qrcode-terminal-go/qrcodeTerminal.go b/vendor/github.com/Baozisoftware/qrcode-terminal-go/qrcodeTerminal.go new file mode 100644 index 00000000..6d0f64f3 --- /dev/null +++ b/vendor/github.com/Baozisoftware/qrcode-terminal-go/qrcodeTerminal.go @@ -0,0 +1,155 @@ +package qrcodeTerminal + +import ( + "fmt" + + "github.com/skip2/go-qrcode" + "github.com/mattn/go-colorable" + "image/png" + nbytes "bytes" +) + +type consoleColor string +type consoleColors struct { + NormalBlack consoleColor + NormalRed consoleColor + NormalGreen consoleColor + NormalYellow consoleColor + NormalBlue consoleColor + NormalMagenta consoleColor + NormalCyan consoleColor + NormalWhite consoleColor + BrightBlack consoleColor + BrightRed consoleColor + BrightGreen consoleColor + BrightYellow consoleColor + BrightBlue consoleColor + BrightMagenta consoleColor + BrightCyan consoleColor + BrightWhite consoleColor +} +type qrcodeRecoveryLevel qrcode.RecoveryLevel +type qrcodeRecoveryLevels struct { + Low qrcodeRecoveryLevel + Medium qrcodeRecoveryLevel + High qrcodeRecoveryLevel + Highest qrcodeRecoveryLevel +} + +var ( + ConsoleColors consoleColors = consoleColors{ + NormalBlack: "\033[38;5;0m \033[0m", + NormalRed: "\033[38;5;1m \033[0m", + NormalGreen: "\033[38;5;2m \033[0m", + NormalYellow: "\033[38;5;3m \033[0m", + NormalBlue: "\033[38;5;4m \033[0m", + NormalMagenta: "\033[38;5;5m \033[0m", + NormalCyan: "\033[38;5;6m \033[0m", + NormalWhite: "\033[38;5;7m \033[0m", + BrightBlack: "\033[48;5;0m \033[0m", + BrightRed: "\033[48;5;1m \033[0m", + BrightGreen: "\033[48;5;2m \033[0m", + BrightYellow: "\033[48;5;3m \033[0m", + BrightBlue: "\033[48;5;4m \033[0m", + BrightMagenta: "\033[48;5;5m \033[0m", + BrightCyan: "\033[48;5;6m \033[0m", + BrightWhite: "\033[48;5;7m \033[0m"} + QRCodeRecoveryLevels = qrcodeRecoveryLevels{ + Low: qrcodeRecoveryLevel(qrcode.Low), + Medium: qrcodeRecoveryLevel(qrcode.Medium), + High: qrcodeRecoveryLevel(qrcode.High), + Highest: qrcodeRecoveryLevel(qrcode.Highest)} +) + +type QRCodeString string + +func (v *QRCodeString) Print() { + fmt.Fprint(outer, *v) +} + +type qrcodeTerminal struct { + front consoleColor + back consoleColor + level qrcodeRecoveryLevel +} + +func (v *qrcodeTerminal) Get(content interface{}) (result *QRCodeString) { + var qr *qrcode.QRCode + var err error + if t, ok := content.(string); ok { + qr, err = qrcode.New(t, qrcode.RecoveryLevel(v.level)) + } else if t, ok := content.([]byte); ok { + qr, err = qrcode.New(string(t), qrcode.RecoveryLevel(v.level)) + } + if qr != nil && err == nil { + data := qr.Bitmap() + result = v.getQRCodeString(data) + } + return +} + +func (v *qrcodeTerminal) Get2(bytes []byte) (result *QRCodeString) { + data, err := parseQR(bytes) + if err == nil { + result = v.getQRCodeString(data) + } + return +} + +func New2(front, back consoleColor, level qrcodeRecoveryLevel) *qrcodeTerminal { + obj := qrcodeTerminal{front: front, back: back, level: level} + return &obj +} + +func New() *qrcodeTerminal { + front, back, level := ConsoleColors.BrightBlack, ConsoleColors.BrightWhite, QRCodeRecoveryLevels.Medium + return New2(front, back, level) +} + +func (v *qrcodeTerminal) getQRCodeString(data [][]bool) (result *QRCodeString) { + str := "" + for ir, row := range data { + lr := len(row) + if ir == 0 || ir == 1 || ir == 2 || + ir == lr-1 || ir == lr-2 || ir == lr-3 { + continue + } + for ic, col := range row { + lc := len(data) + if ic == 0 || ic == 1 || ic == 2 || + ic == lc-1 || ic == lc-2 || ic == lc-3 { + continue + } + if col { + str += fmt.Sprint(v.front) + } else { + str += fmt.Sprint(v.back) + } + } + str += fmt.Sprintln() + } + obj := QRCodeString(str) + result = &obj + return +} + +func parseQR(bytes []byte) (data [][]bool, err error) { + r := nbytes.NewReader(bytes) + img, err := png.Decode(r) + if err == nil { + rect := img.Bounds() + mx, my := rect.Max.X, rect.Max.Y + data = make([][]bool, mx) + for x := 0; x < mx; x++ { + data[x] = make([]bool, my) + for y := 0; y < my; y++ { + c := img.At(x, y) + r, _, _, _ := c.RGBA() + data[x][y] = r == 0 + } + } + } + return +} + +var outer = colorable.NewColorableStdout() |