qrscan

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

QR Code & Barcode Scanner for Go

Read QR codes and barcodes from an image file in Go. This package drives the installed qrscan command of the QR Code & Barcode Scanner engine and returns its JSON report unchanged. It holds no decoder of its own, uses the standard library only and needs no cgo.

QR codes Barcodes
Try the scanner live QR code scanner Barcode scanner
Go guide QR code scanner for Go Barcode scanner for Go
Create a code QR code generator Barcode generator

Install

go get github.com/qrcode-ai-com/qrcode-barcode-scanner-go@v0.11.0
import qrscan "github.com/qrcode-ai-com/qrcode-barcode-scanner-go"

The package follows the engine it drives: version 0.11.x of the package works with qrscan 0.11.x. The first version is 0.11.0.

What you need

  • Go 1.24 or later. CGO_ENABLED=0 works: there is no C code.
  • The qrscan command, version 0.11.x, installed separately. The distribution guide lists the routes: Homebrew, Scoop, the Debian and RPM packages, the AUR, cargo install, the release archives and the container image. This package never downloads it.

Scan an image

scanner := qrscan.New("") // qrscan on PATH; or qrscan.New("/path/to/qrscan")
report, err := scanner.Scan(ctx, "label.png", qrscan.Fast, qrscan.ProfileBudget)
if err != nil {
	return err
}
for _, detection := range report.Detections {
	fmt.Println(detection.Symbology, detection.Text)
}

Scan takes a context, the file, a profile and a time budget in milliseconds:

Profile What the engine does
qrscan.Frame Decodes only. The report carries no score.
qrscan.Fast Decodes first, with a reduced judgment.
qrscan.Full Decodes and judges how well a QR code scans.

qrscan.ProfileBudget keeps the profile's own budget, and a busy machine can then cut the scan short. qrscan.Unbounded sets none: the result then does not depend on how busy the machine is. Cancelling the context stops the command.

Read the report

report.JSON is the exact text the engine printed. Nothing is renamed, rounded or recalculated, so decode it yourself for the payload, the measured places and the trace. The other fields read the most used parts:

Field Meaning
Found() At least one code was decoded.
Detections One entry per distinct content: Symbology, Text and Raw, the exact bytes.
Score The scannability judgment, or nil.
Complete false when the time budget cut the scan.
EngineVersion The engine that wrote the report, such as 0.11.0.

Three things are easy to get wrong:

  • No code is a result, not an error. Found() is false and err is nil.
  • A missing score is not zero. Score is nil with qrscan.Frame, or when the budget ran out before the judgment.
  • An incomplete scan proves nothing. When Complete is false, an empty report does not mean the image holds no code. Scan again with a larger budget or with qrscan.Unbounded.

Decoded text is untrusted data: a QR code can hold any text. Do not follow its links or run it.

When something fails

Error When
qrscan.ErrBinaryNotFound (with errors.Is) qrscan could not be started: not installed, not on PATH, not executable. It is not remembered: install the command and the same Scanner works.
*qrscan.UnsupportedVersionError The installed qrscan is not 0.11.x. The check runs before the first scan.
*qrscan.ScanError The engine refused the file or the options. EngineCode holds its code when it gives one, such as QRS-001 for a file that is not an image.
the context's error The context was cancelled or its deadline passed; the command was stopped.

What it does and does not do

  • One scan starts one process. That suits a request or a job. It is the wrong tool for thousands of images in a tight loop.
  • It reads image files: PNG, JPEG, WebP and GIF. To scan bytes you hold in memory, write them to a temporary file first.
  • The command is started from a list of arguments, with no shell. A file name is never interpreted, whatever it holds: spaces, quotes, a leading dash or shell characters.
  • It writes nothing, fetches nothing and never opens what a code points to.

Why not WebAssembly

A first experiment compiled the engine to WebAssembly and ran it inside the Go process with wazero. Its reports were identical to the native command's, and one scan of a photo-sized image took about twenty times longer. The owner chose this route on 11 November 2026.

Develop

The tests run against a real qrscan, not a stand-in:

QRSCAN_BINARY=/path/to/qrscan QRSCAN_FIXTURES=/path/to/engine/fixtures go test ./...

They cover the three profiles on an image that holds a QR code and a Code 128, an image with no code, files that are not images, awkward file names, a missing command, an engine of another version and a cancelled context. The workflow Go adapter of the engine repository runs them on Linux, macOS and Windows against the released command.

Its own repository

A Go module is fetched from the root of a repository at a tag, so the package is served from a small repository of its own, qrcode-ai-com/qrcode-barcode-scanner-go. Nothing there is edited by hand: scripts/stage-go-package.py writes its whole tree from one commit of the engine repository, with the workflow of mirror/, the two test images, the licence and a SOURCE.json that names the commit and the sha256 of every file. The same program checks that a tree is exactly that. Change the package here, then stage it again.

Licence

AGPL-3.0-or-later, like the engine it drives. See LICENSE.

Documentation

Overview

Package qrscan reads QR codes and barcodes from an image file through the installed qrscan command of the QR Code & Barcode Scanner engine.

The package holds no decoder. It starts the command, which you install separately, and returns its JSON report unchanged. One scan is one process. The command is started from a list of arguments, with no shell: a file name is never interpreted, whatever it holds. The package uses the standard library only, with no cgo.

scanner := qrscan.New("") // qrscan on PATH
report, err := scanner.Scan(ctx, "label.png", qrscan.Fast, qrscan.ProfileBudget)
for _, detection := range report.Detections {
	fmt.Println(detection.Symbology, detection.Text)
}

Index

Constants

View Source
const (
	// ProfileBudget keeps the profile's own budget.
	ProfileBudget = -1
	// Unbounded sets no budget: the result then does not depend on how busy the machine is.
	Unbounded = 0
)

A time budget for the engine, in milliseconds.

View Source
const SupportedEngine = "0.11"

SupportedEngine is the engine line this package was checked against: every 0.11.x.

Variables

View Source
var ErrBinaryNotFound = errors.New("qrscan: the qrscan command could not be started")

ErrBinaryNotFound means the qrscan command could not be started: it is not installed, not on PATH or not executable.

Functions

This section is empty.

Types

type Detection

type Detection struct {
	// Symbology is the kind of code, such as qr_code or code128.
	Symbology string
	// Text is the decoded text. Treat it as untrusted data: a QR code can hold any text.
	Text string
	// Raw is the exact bytes of the content, for content that is not text.
	Raw []byte
}

Detection is one decoded content. The full entry, with its payload and measured places, is in Report.JSON.

type Profile

type Profile string

Profile says how much work one scan does. The values are the ones `qrscan --profile` takes.

const (
	// Full decodes and judges how well a QR code scans.
	Full Profile = "full"
	// Fast decodes first, with a reduced judgment.
	Fast Profile = "fast"
	// Frame decodes only: the report carries no score.
	Frame Profile = "frame"
)

type Report

type Report struct {
	// JSON is the engine's report, exactly as printed.
	JSON []byte
	// ExitCode is 0 when qrscan found a code and 1 when it found none.
	ExitCode int
	// Detections holds one entry per distinct content.
	Detections []Detection
	// Score is nil when the engine made no judgment: the frame profile, or a budget that ran
	// out. Nil is never zero.
	Score *Score
	// Complete is false when the time budget cut the scan: an empty report then proves nothing.
	Complete bool
	// EngineVersion is the engine that wrote the report, such as 0.11.0.
	EngineVersion string
}

Report is one scan report. JSON is the text the engine printed, unchanged: nothing is renamed, rounded or recalculated. The other fields only read it. A report with no detection is a valid result, not an error.

func (*Report) Found

func (r *Report) Found() bool

Found reports whether at least one code was decoded.

type ScanError

type ScanError struct {
	// ExitCode is the command's exit code.
	ExitCode int
	// EngineCode is the engine's own code, such as QRS-001 for a file that is not an image.
	// It is empty when the message carries none.
	EngineCode string
	// Message is one line of the command's own message.
	Message string
}

ScanError means the engine refused the file or the options, or its output could not be read.

func (*ScanError) Error

func (e *ScanError) Error() string

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner starts one qrscan command. It is safe for concurrent use.

func New

func New(binary string) *Scanner

New returns a Scanner for the given command: a name looked up on PATH or a path. An empty name means "qrscan".

func (*Scanner) Capabilities

func (s *Scanner) Capabilities(ctx context.Context) ([]byte, error)

Capabilities returns what the installed command can read, as the JSON it printed: its versions, decode engines and symbologies.

func (*Scanner) EngineVersion

func (s *Scanner) EngineVersion(ctx context.Context) (string, error)

EngineVersion returns the version of the installed engine, such as 0.11.0. It fails with UnsupportedVersionError when that version is outside the supported line.

func (*Scanner) Scan

func (s *Scanner) Scan(ctx context.Context, imagePath string, profile Profile, budgetMs int) (*Report, error)

Scan reads the codes of one image file (PNG, JPEG, WebP or GIF). budgetMs is ProfileBudget, Unbounded or a number of milliseconds. Cancelling ctx stops the command.

type Score

type Score struct {
	// Value is the composite score, from 0 to 100.
	Value int
}

Score is the scannability judgment of a QR code.

type UnsupportedVersionError

type UnsupportedVersionError struct {
	// Version is what the command reported, empty when it reported none.
	Version string
}

UnsupportedVersionError means the installed qrscan is not a version this package was checked against.

func (*UnsupportedVersionError) Error

func (e *UnsupportedVersionError) Error() string

Jump to

Keyboard shortcuts

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