Golang barcode detection module based on Dynamsoft C/C++ Barcode SDK
MIT License
This project serves as a Golang wrapper for the Dynamsoft Barcode Reader C++ SDK. With this module, you can efficiently read various types of barcodes and symbols, including QR Codes (and Micro QR Codes), Data Matrix, PDF417 (and Micro PDF417), Aztec Code, MaxiCode (modes 2-5), and DotCode from images and PDF files.
# Windows
.\run_windows_test.ps1
# Linux
./run_linux_test.sh
## mac
chmod +x run_mac_test.sh
sudo ./run_mac_test.sh
Download the Go module:
go get github.com/yushulx/goBarcodeQrSDK
Import the package in your Go file:
import (
"github.com/yushulx/goBarcodeQrSDK"
)
Specify the dynamic library path at runtime before running Go apps.
Windows
$originalPath = $env:PATH
# Get the GOPATH
$GOPATH = $(go env GOPATH)
# Find the path to the shared libraries
$PACKAGE_PATH = Get-ChildItem -Path "$GOPATH\pkg\mod\github.com\yushulx" -Directory | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
Write-Host "PACKAGE_PATH set to $PACKAGE_PATH"
$LIBRARY_PATH = "$PACKAGE_PATH\lib\windows"
Write-Host "LIBRARY_PATH set to $LIBRARY_PATH"
# Update PATH to include the assembly path
$env:PATH = "$LIBRARY_PATH;" + $env:PATH
# Write-Host "PATH set to $($env:PATH)"
# Run your Go application
go run test.go test.png
$env:PATH = $originalPath
Linux
#!/bin/bash
# Save the original PATH
originalPath=$LD_LIBRARY_PATH
# Get the GOPATH
GOPATH=$(go env GOPATH)
# Find the path to the shared libraries
PACKAGE_PATH=$(find "$GOPATH/pkg/mod/github.com/yushulx" -mindepth 1 -maxdepth 1 -type d | sort -r | head -n 1)
echo "PACKAGE_PATH set to $PACKAGE_PATH"
LIBRARY_PATH="$PACKAGE_PATH/lib/linux"
echo "LIBRARY_PATH set to $LIBRARY_PATH"
export LD_LIBRARY_PATH="$LIBRARY_PATH:$originalPath"
# Run your Go application
go run test.go test.png
# Restore the original PATH
export LD_LIBRARY_PATH=$originalPath
macOS
#!/bin/bash
# Save the original PATH
originalPath=$LD_LIBRARY_PATH
# Get the GOPATH
GOPATH=$(go env GOPATH)
# Find the path to the shared libraries
PACKAGE_PATH=$(find "$GOPATH/pkg/mod/github.com/yushulx" -mindepth 1 -maxdepth 1 -type d | sort -r | head -n 1)
echo "PACKAGE_PATH set to $PACKAGE_PATH"
RPATH="$PACKAGE_PATH/lib/mac"
echo "LIBRARY_PATH set to $LIBRARY_PATH"
TARGET="testapp"
go build -o $TARGET
if ! otool -l $TARGET | grep -q $RPATH; then
echo "Adding rpath $RPATH to $TARGET"
install_name_tool -add_rpath $RPATH $TARGET
else
echo "RPATH $RPATH already exists in $TARGET"
fi
./$TARGET test.png
rm ./$TARGET
Set the license key within the InitLicense()
function, and replace the image-file
with the path of the image file you wish to decode.
package main
import (
"fmt"
"time"
"github.com/yushulx/goBarcodeQrSDK"
)
func main() {
ret, _ := goBarcodeQrSDK.InitLicense("LICENSE-KEY")
if ret != 0 {
fmt.Printf(`initLicense("") = %d`, ret)
}
obj := goBarcodeQrSDK.CreateBarcodeReader()
obj.SetParameters("{\"ImageParameter\":{\"BarcodeFormatIds\":[\"BF_ONED\",\"BF_PDF417\",\"BF_QR_CODE\",\"BF_DATAMATRIX\"],\"BarcodeFormatIds_2\":null,\"Name\":\"sts\",\"RegionDefinitionNameArray\":[\"region0\"]},\"RegionDefinition\":{\"Bottom\":100,\"Left\":0,\"MeasuredByPercentage\":1,\"Name\":\"region0\",\"Right\":100,\"Top\":0}}")
startTime := time.Now()
code, barcodes := obj.DecodeFile("image-file")
elapsed := time.Since(startTime)
fmt.Println("DecodeFile() time cost: ", elapsed)
if code != 0 {
fmt.Printf(`DecodeFile() = %d`, code)
}
for i := 0; i < len(barcodes); i++ {
barcode := barcodes[i]
fmt.Println(barcode.Text)
fmt.Println(barcode.Format)
fmt.Println(barcode.X1)
fmt.Println(barcode.Y1)
fmt.Println(barcode.X2)
fmt.Println(barcode.Y2)
fmt.Println(barcode.X3)
fmt.Println(barcode.Y3)
fmt.Println(barcode.X4)
fmt.Println(barcode.Y4)
}
}
Build and run barcode QR code reader in Docker:
docker build -t golang-barcode-qr-reader .
docker run -it --rm golang-barcode-qr-reader
Read barcode and QR code from a local image file:
docker run -it --rm -v <image-folder>:/app golang-barcode-qr-reader reader /app/<image-file> <license-key> <template-file>
https://hub.docker.com/repository/docker/yushulx/golang-barcode-qr-reader
docker run -it --rm -v <image-folder>:/app yushulx/golang-barcode-qr-reader:latest reader /app/<image-file> <license-key> <template-file>