qrcode

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 8 Imported by: 20

README

qrcode

golang 编码/解码二维码

  1. 编码采用 github.com/boombuler/barcode

  2. 解码支持采用golang或zbar(zbar更准确)

    zbar解码需要 #include <zbar.h> c语言库的支持(例如:pip install zbar)

例子

package main  

import (  
	"fmt"  
	"image/png"  
	"os"  

	"github.com/admpub/qrcode"
)

func main() {  
	err := png.EncodeToFile("test qrcode", 300, 300,"./test.png")  
	if err != nil {  
		fmt.Println(err)  
		return  
	}

	value := qrcode.DecodeFile("./test.png")  
	fmt.Println(value)
}

如何安装zbar

Ubuntu

sudo apt-get install libzbar-dev

CentOS

sudo yum install zbar-devel

sudo yum -y install epel-release pygtk2.x86_64 zbar-pygtk.x86_64 pygtk2-devel.x86_64 pygtk2-doc.noarch pygobject2.x86_64 pygobject2-devel.x86_64 pygobject2-doc.x86_64 gtk2 gtk2-devel gtk2-devel-docs pdftk ImageMagick ImageMagick-devel ghostscript Python-imaging python-devel python-gtk2-dev libqt4-dev PyQt4.x86_64 PyQt4-devel.x86_64

wget http://ftp.gnome.org/pub/GNOME/sources/pygtk/2.24/pygtk-2.24.0.tar.gz

tar -zvxf pygtk-2.24.0.tar.gz

cd pygtk-2.24.0

./configure

make

sudo make install

wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz

tar -zvxf zbar-0.10.tar.gz

cd zbar-0.10

./configure --disable-video

make

sudo make install

Documentation

Index

Constants

View Source
const (
	VERSION = 1.0
)

Variables

This section is empty.

Functions

func Decode

func Decode(reader io.Reader, imageType string) (string, error)

Decode reads a QR code from the given reader and returns its content. The imageType parameter specifies the format of the input image (e.g., "png", "jpeg"). Returns the decoded content as a string or an error if decoding fails.

func DecodeFile

func DecodeFile(imgPath string) (string, error)

DecodeFile reads the QR code from the specified image file path and returns its decoded content. If the file cannot be opened or the QR code cannot be decoded, it returns an error.

func Encode

func Encode(value string, width, height int) (image.Image, error)

Encode generates a QR code image from the given string value with specified width and height. Returns the generated image or an error if encoding fails or if the encoded content differs from the input.

func EncodeToFile

func EncodeToFile(value string, width, height int, qrcodeFile string) error

EncodeToFile encodes the given value as a QR code and saves it to the specified file. The width and height parameters determine the dimensions of the generated QR code image. Returns an error if file creation or QR code generation fails.

func EncodeToWriter

func EncodeToWriter(value string, width, height int, writer io.Writer) error

EncodeToWriter encodes the given string value into a QR code image with specified width and height, then writes the PNG-encoded image data to the provided io.Writer. Returns an error if encoding or writing fails.

Types

type Name

type Name interface {
	Name() string
}

Name is an interface that provides a method to get the name of an object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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