Documentation
¶
Overview ¶
Package barcode is a Go port of milon/barcode: generate 1D and 2D barcodes as SVG, HTML and PNG, and 1D barcodes as JPEG, for Go and Laravel for Go.
Upstream: https://github.com/milon/barcode
Example ¶
generator, err := barcode.New1D(barcode.InlineSVG(true), barcode.Padding(10))
if err != nil {
panic(err)
}
svg, err := generator.SVG("SKU-1042", barcode.C128)
if err != nil {
panic(err)
}
qr, err := barcode.QR(barcode.QRErrorCorrection(barcode.QRHigh))
if err != nil {
panic(err)
}
uri, err := (barcode.Generator2D{}).DataURI("https://example.test/item/7", qr, barcode.PNGFormat)
if err != nil {
panic(err)
}
fmt.Println(strings.HasPrefix(string(svg), "<svg "))
fmt.Println(strings.HasPrefix(uri, "data:image/png;base64,"))
Output: true true
Index ¶
- Constants
- Variables
- type Bars
- type CheckDigitError
- type CodeError
- type Element
- type Format
- type Generator1D
- func (g Generator1D) Base64(code string, t Type1D, f Format) (string, error)
- func (g Generator1D) DataURI(code string, t Type1D, f Format) (string, error)
- func (Generator1D) Encode(code string, typ Type1D) (Bars, error)
- func (g Generator1D) Generate(code string, t Type1D, f Format) ([]byte, error)
- func (g Generator1D) HTML(code string, t Type1D) ([]byte, error)
- func (g Generator1D) JPEG(code string, t Type1D) ([]byte, error)
- func (g Generator1D) PNG(code string, t Type1D) ([]byte, error)
- func (g Generator1D) Render(b Bars, f Format) ([]byte, error)
- func (g Generator1D) SVG(code string, t Type1D) ([]byte, error)
- func (g Generator1D) Save(path, code string, t Type1D, f Format) error
- func (g Generator1D) With(opts ...Option1D) (Generator1D, error)
- func (g Generator1D) WriteTo(w io.Writer, code string, t Type1D, f Format) error
- type Generator2D
- func (g Generator2D) Base64(code string, t Type2D, f Format) (string, error)
- func (g Generator2D) DataURI(code string, t Type2D, f Format) (string, error)
- func (Generator2D) Encode(code string, typ Type2D) (Matrix, error)
- func (g Generator2D) Generate(code string, t Type2D, f Format) ([]byte, error)
- func (g Generator2D) HTML(code string, t Type2D) ([]byte, error)
- func (g Generator2D) PNG(code string, t Type2D) ([]byte, error)
- func (g Generator2D) Render(m Matrix, f Format) ([]byte, error)
- func (g Generator2D) SVG(code string, t Type2D) ([]byte, error)
- func (g Generator2D) Save(path, code string, t Type2D, f Format) error
- func (g Generator2D) With(opts ...Option2D) (Generator2D, error)
- func (g Generator2D) WriteTo(w io.Writer, code string, t Type2D, f Format) error
- type MacroField
- type Matrix
- type Option
- type Option1D
- type Option2D
- type PDF417ECLevel
- type PDF417Option
- type QRLevel
- type QROption
- type Type1D
- type Type2D
Examples ¶
Constants ¶
const MaxInputBytes = 16384
MaxInputBytes is the largest input accepted for matrix encoding.
const MaxLinearInputBytes = 1024
MaxLinearInputBytes is the largest input accepted by Encode for a linear symbol.
const MaxLogoBytes = 16777216
MaxLogoBytes is the largest accepted encoded logo.
const MaxLogoPixels = 16777216
MaxLogoPixels is the largest accepted logo area.
const MaxLogoSide = 4096
MaxLogoSide is the largest accepted logo dimension.
const MaxModule = 1024
MaxModule is the maximum module or font size.
const MaxPixels = 67108864
MaxPixels is the maximum area of a raster canvas.
const MaxSide = 16384
MaxSide is the maximum canvas width or height.
const MaxSpecBytes = 4096
MaxSpecBytes bounds a matrix symbology specification before parsing.
const Version = "0.1.0"
Version is the version of this module.
Variables ¶
var ( // ErrUnsupportedType indicates an unknown symbology. ErrUnsupportedType = errors.New("unsupported barcode type") // ErrInvalidCode indicates data that cannot be encoded in the selected symbology. ErrInvalidCode = errors.New("invalid barcode data") // ErrDataTooLong indicates data beyond the input limit or symbol capacity. ErrDataTooLong = errors.New("barcode data too long") )
var ErrCheckDigit = fmt.Errorf("wrong barcode check digit: %w", ErrInvalidCode)
ErrCheckDigit indicates a supplied digit that does not match the computed digit.
var ErrInvalidOption = errors.New("invalid barcode option")
ErrInvalidOption indicates an invalid symbology or rendering parameter.
var ErrTooLarge = errors.New("barcode canvas too large")
ErrTooLarge indicates that the render canvas exceeds a size limit.
var ErrUnsupportedFormat = errors.New("unsupported barcode format")
ErrUnsupportedFormat indicates that the format cannot carry the requested output.
Functions ¶
This section is empty.
Types ¶
type Bars ¶
type Bars struct {
// contains filtered or unexported fields
}
Bars is an immutable encoded linear symbol.
type CheckDigitError ¶
CheckDigitError reports the expected and supplied retail check digits.
func (*CheckDigitError) Error ¶
func (e *CheckDigitError) Error() string
func (*CheckDigitError) Unwrap ¶
func (e *CheckDigitError) Unwrap() error
Unwrap returns ErrCheckDigit.
type CodeError ¶
type CodeError struct {
Type string
Offset int
Reason string
// contains filtered or unexported fields
}
CodeError describes an encoding failure without retaining the caller's data.
type Format ¶
type Format uint8
Format identifies an output format.
func (Format) ContentType ¶
ContentType returns the MIME type for a supported format.
type Generator1D ¶
type Generator1D struct {
// contains filtered or unexported fields
}
Generator1D encodes linear barcodes.
func New1D ¶
func New1D(opts ...Option1D) (Generator1D, error)
New1D creates a linear generator with the supplied render options.
func (Generator1D) Encode ¶
func (Generator1D) Encode(code string, typ Type1D) (Bars, error)
Encode encodes a linear symbol, as used by PHP's DNS1D render methods.
func (Generator1D) HTML ¶
func (g Generator1D) HTML(code string, t Type1D) ([]byte, error)
HTML encodes and renders a HTML barcode.
func (Generator1D) JPEG ¶
func (g Generator1D) JPEG(code string, t Type1D) ([]byte, error)
JPEG encodes and renders a JPEG barcode.
func (Generator1D) PNG ¶
func (g Generator1D) PNG(code string, t Type1D) ([]byte, error)
PNG encodes and renders a PNG barcode.
func (Generator1D) Render ¶
func (g Generator1D) Render(b Bars, f Format) ([]byte, error)
Render converts an encoded linear barcode to the given format.
func (Generator1D) SVG ¶
func (g Generator1D) SVG(code string, t Type1D) ([]byte, error)
SVG encodes and renders a SVG barcode.
func (Generator1D) Save ¶
func (g Generator1D) Save(path, code string, t Type1D, f Format) error
Save atomically writes a barcode to a file in an existing directory.
func (Generator1D) With ¶
func (g Generator1D) With(opts ...Option1D) (Generator1D, error)
With returns a copy with the supplied render options applied in order.
type Generator2D ¶
type Generator2D struct {
// contains filtered or unexported fields
}
Generator2D encodes matrix barcodes.
func New2D ¶
func New2D(opts ...Option2D) (Generator2D, error)
New2D creates a matrix generator with the supplied render options.
func (Generator2D) Encode ¶
func (Generator2D) Encode(code string, typ Type2D) (Matrix, error)
Encode encodes a matrix symbol, as used by PHP's DNS2D render methods.
func (Generator2D) HTML ¶
func (g Generator2D) HTML(code string, t Type2D) ([]byte, error)
HTML encodes and renders a HTML barcode.
func (Generator2D) PNG ¶
func (g Generator2D) PNG(code string, t Type2D) ([]byte, error)
PNG encodes and renders a PNG barcode.
func (Generator2D) Render ¶
func (g Generator2D) Render(m Matrix, f Format) ([]byte, error)
Render converts an encoded matrix barcode to SVG, HTML or PNG.
func (Generator2D) SVG ¶
func (g Generator2D) SVG(code string, t Type2D) ([]byte, error)
SVG encodes and renders a SVG barcode.
func (Generator2D) Save ¶
func (g Generator2D) Save(path, code string, t Type2D, f Format) error
Save atomically writes a barcode to a file in an existing directory.
func (Generator2D) With ¶
func (g Generator2D) With(opts ...Option2D) (Generator2D, error)
With returns a copy with the supplied render options applied in order.
type MacroField ¶
type MacroField interface {
// contains filtered or unexported methods
}
MacroField is an optional PDF417 Macro metadata field.
func SegmentCount ¶
func SegmentCount(v int) MacroField
SegmentCount sets the optional Macro segment count.
type Matrix ¶
type Matrix struct {
// contains filtered or unexported fields
}
Matrix is an immutable encoded matrix symbol.
type Option ¶
Option configures either kind of generator.
func Background ¶
Background fills PNG's otherwise transparent canvas with an opaque color.
func ForegroundCSS ¶
ForegroundCSS sets a named, hexadecimal, or rgb() foreground color.
type Option1D ¶
type Option1D interface {
// contains filtered or unexported methods
}
Option1D configures a linear generator.
type Option2D ¶
type Option2D interface {
// contains filtered or unexported methods
}
Option2D configures a matrix generator.
func ModuleSize ¶
ModuleSize sets the matrix column width and row height in pixels.
type PDF417ECLevel ¶
type PDF417ECLevel int
PDF417ECLevel selects a PDF417 error correction level.
const ( // PDF417Auto chooses a correction level based on input size. PDF417Auto PDF417ECLevel = iota - 1 // PDF417EC0 selects correction level 0. PDF417EC0 // PDF417EC1 selects correction level 1. PDF417EC1 // PDF417EC2 selects correction level 2. PDF417EC2 // PDF417EC3 selects correction level 3. PDF417EC3 // PDF417EC4 selects correction level 4. PDF417EC4 // PDF417EC5 selects correction level 5. PDF417EC5 // PDF417EC6 selects correction level 6. PDF417EC6 // PDF417EC7 selects correction level 7. PDF417EC7 // PDF417EC8 selects correction level 8. PDF417EC8 )
type PDF417Option ¶
type PDF417Option interface {
// contains filtered or unexported methods
}
PDF417Option configures PDF417 encoding.
func AspectRatio ¶
func AspectRatio(ratio float64) PDF417Option
AspectRatio sets the PDF417 target width-to-height ratio.
func Macro ¶
func Macro(total, index, fileID int, fields ...MacroField) PDF417Option
Macro sets the PDF417 segment identifiers and optional metadata.
func PDF417ErrorCorrection ¶
func PDF417ErrorCorrection(level PDF417ECLevel) PDF417Option
PDF417ErrorCorrection selects the equivalent of PHP's PDF417 correction field.
type QROption ¶
type QROption interface {
// contains filtered or unexported methods
}
QROption configures QR encoding.
func QRErrorCorrection ¶
QRErrorCorrection selects the equivalent of PHP's QRCODE level field.
type Type1D ¶
type Type1D int
Type1D identifies a linear symbology.
const ( // C39 selects the PHP C39 symbology. C39 Type1D // C39Plus selects the PHP C39+ symbology. C39Plus // C39E selects the PHP C39E symbology. C39E // C39EPlus selects the PHP C39E+ symbology. C39EPlus // C93 selects the PHP C93 symbology. C93 // S25 selects the PHP S25 symbology. S25 // S25Plus selects the PHP S25+ symbology. S25Plus // I25 selects the PHP I25 symbology. I25 // I25Plus selects the PHP I25+ symbology. I25Plus // C128 selects the PHP C128 symbology. C128 // C128A selects the PHP C128A symbology. C128A // C128B selects the PHP C128B symbology. C128B // C128C selects the PHP C128C symbology. C128C // GS1128 selects the PHP GS1-128 symbology. GS1128 // EAN2 selects the PHP EAN2 symbology. EAN2 // EAN5 selects the PHP EAN5 symbology. EAN5 // EAN8 selects the PHP EAN8 symbology. EAN8 // EAN13 selects the PHP EAN13 symbology. EAN13 // UPCA selects the PHP UPCA symbology. UPCA // UPCE selects the PHP UPCE symbology. UPCE // MSI selects the PHP MSI symbology. MSI // MSIPlus selects the PHP MSI+ symbology. MSIPlus // POSTNET selects the PHP POSTNET symbology. POSTNET // PLANET selects the PHP PLANET symbology. PLANET // RMS4CC selects the PHP RMS4CC symbology. RMS4CC // KIX selects the PHP KIX symbology. KIX // IMB selects the PHP IMB symbology. IMB // Codabar selects the PHP CODABAR symbology. Codabar // Code11 selects the PHP CODE11 symbology. Code11 // Pharma selects the PHP PHARMA symbology. Pharma // Pharma2T selects the PHP PHARMA2T symbology. Pharma2T )
func ParseType1D ¶
ParseType1D parses a PHP symbology name, ignoring ASCII letter case.
type Type2D ¶
type Type2D interface {
String() string
// contains filtered or unexported methods
}
Type2D is a matrix symbology and its encoding parameters.
func DataMatrix ¶
func DataMatrix() Type2D
DataMatrix constructs the DATAMATRIX symbology (ECC 200).
func PDF417 ¶
func PDF417(opts ...PDF417Option) (Type2D, error)
PDF417 constructs a PDF417 symbology with the supplied options.
func ParseType2D ¶
ParseType2D parses a PHP matrix symbology specification.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package laravelbarcode integrates barcode generators and storage with Laravel for Go.
|
Package laravelbarcode integrates barcode generators and storage with Laravel for Go. |