Last active
July 8, 2016 11:16
-
-
Save itang/c1754b909c0a5551ea0cb2ce7f245a4b to your computer and use it in GitHub Desktop.
二维码识别 golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
//"github.com/shezadkhan137/go-qrcode/qrcode" | |
"encoding/base64" | |
"io/ioutil" | |
"strings" | |
"image" | |
"image/jpeg" | |
"image/png" | |
"golang.org/x/image/bmp" | |
"bytes" | |
"gopkg.in/bieber/barcode.v0" | |
"github.com/itang/gotang" | |
) | |
func main() { | |
for i := 1; i <= 3; i++ { | |
content, err := ioutil.ReadFile(fmt.Sprintf("d%v.data", i)) | |
gotang.AssertNoError(err, "") | |
arr := strings.Split(string(content), ",") | |
imageContent := arr[1] | |
//fmt.Println(imageContent) | |
byteData, err := base64.StdEncoding.DecodeString(imageContent) | |
gotang.AssertNoError(err, "") | |
fmt.Println("len:", len(byteData)) | |
gotang.Time(func() { | |
var src image.Image | |
var err error | |
if strings.Contains(arr[0], "jpeg") { | |
src, err = jpeg.Decode(bytes.NewReader(byteData)) | |
gotang.AssertNoError(err, "") | |
} else if strings.Contains(arr[0], "bmp") { | |
src, err = bmp.Decode(bytes.NewReader(byteData)) | |
gotang.AssertNoError(err, "") | |
} else { | |
src, err = png.Decode(bytes.NewReader(byteData)) | |
gotang.AssertNoError(err, "") | |
} | |
img := barcode.NewImage(src) | |
scanner := barcode.NewScanner(). | |
SetEnabledAll(true) | |
symbols, _ := scanner.ScanImage(img) | |
for _, s := range symbols { | |
fmt.Println(s.Type.Name(), s.Data, s.Quality, s.Boundary) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment