qrcode

package module
v0.0.0-...-105df13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2020 License: BSD-3-Clause Imports: 10 Imported by: 0

README

  • 赞助 BTC: 1Cbd6oGAUUyBi7X7MaR4np4nTmQZXVgkCW
  • 赞助 ETH: 0x623A3C3a72186A6336C79b18Ac1eD36e1c71A8a6
  • Go语言付费QQ群: 1055927514

QR code encoder/decoder

PkgDoc: http://godoc.org/github.com/chai2010/qrcode

Install

Install GCC or MinGW (download here) at first, and then run these commands:

  1. go get -d github.com/chai2010/qrcode
  2. go generate and go install
  3. go run hello.go

Notes

If use TDM-GCC, and build error like this:

c:\tdm-gcc-64\x86_64-w64-mingw32\include\aviriff.h:3:25: error: 'refer' does not
 name a type
 * No warranty is given; refer to the file DISCLAIMER within this package.
 ...

You need fix the C:\TDM-GCC-64\x86_64-w64-mingw32\include\aviriff.h header first:

** // fixit: ** -> /**
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER within this package.
*/

Example

// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ingore

// QR codes encoder.
package main

import (
	"bytes"
	"flag"
	"fmt"
	"image"
	_ "image/jpeg"
	_ "image/png"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/chai2010/qrcode"
)

var (
	flagText   = flag.String("text", "chaishushan@gmail.com", "Set text")
	flagLevel  = flag.String("n", "Q", "Set QR encode level(L|M|Q|H), default is 'Q'.")
	flagOutput = flag.String("o", "qrcode.png", "Set output filename (PNG only).")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] text\n", filepath.Base(os.Args[0]))
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	filename := filepath.Clean(*flagOutput)
	if filename == "" {
		filename = "qr.png"
	}
	if !strings.HasSuffix(strings.ToLower(filename), ".png") {
		filename += ".png"
	}

	level := qrcode.Q
	switch strings.ToUpper(*flagLevel) {
	case "L":
		level = qrcode.L
	case "M":
		level = qrcode.M
	case "Q":
		level = qrcode.Q
	case "H":
		level = qrcode.H
	}

	code, err := qrcode.Encode(*flagText, level)
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile(filename, code.PNG(), 0666)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Save as:", filename)

	// Load file data
	data, err := ioutil.ReadFile("testdata/01-1.jpg")
	if err != nil {
		log.Fatal(err)
	}

	// Decode image
	m, _, err := image.Decode(bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

	// Decode QR Code
	text, err := qrcode.Decode(m)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("testdata/01-1.jpg:", text)
}

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

Documentation

Overview

Package qrcode implements a decoder and encoder for QR code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(m image.Image) (text string, err error)

Types

type Code

type Code struct {
	Bitmap []byte // 1 is black, 0 is white
	Size   int    // number of pixels on a side
	Stride int    // number of bytes per row
	Scale  int    // number of image pixels per QR pixel
}

A Code is a square pixel grid. It implements image.Image and direct PNG encoding.

func Encode

func Encode(text string, level Level) (*Code, error)

Encode returns an encoding of text at the given error correction level.

func (*Code) Black

func (c *Code) Black(x, y int) bool

Black returns true if the pixel at (x,y) is black.

func (*Code) Image

func (c *Code) Image() image.Image

Image returns an Image displaying the code.

func (*Code) PNG

func (c *Code) PNG() []byte

PNG returns a PNG image displaying the code.

PNG uses a custom encoder tailored to QR codes. Its compressed size is about 2x away from optimal, but it runs about 20x faster than calling png.Encode on c.Image().

type Level

type Level int

A Level denotes a QR error correction level. From least to most tolerant of errors, they are L, M, Q, H.

const (
	L Level = iota // 20% redundant
	M              // 38% redundant
	Q              // 55% redundant
	H              // 65% redundant
)

Directories

Path Synopsis
internal
coding
Package coding implements low-level QR coding details.
Package coding implements low-level QR coding details.
gf256
Package gf256 implements arithmetic over the Galois Field GF(256).
Package gf256 implements arithmetic over the Galois Field GF(256).
libqrencode
Package libqrencode wraps the C libqrencode library.
Package libqrencode wraps the C libqrencode library.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL