How to Generate Barcodes with Aspose.BarCode FOSS

How to Generate Barcodes with Aspose.BarCode FOSS

Problem

Generate 1D and 2D barcodes from data strings using Aspose.BarCode FOSS for Python. The library provides per-symbology helper functions (code128(), qr(), ean13(), etc.) and a generic generate() function that accepts a symbology name string.

import aspose_barcode_foss as barcode

bc = barcode.code128("Hello World")
svg = bc.to_svg()

Prerequisites

  • Python 3.12 or later
  • pip package manager
  • aspose-barcode-foss installed via pip install aspose-barcode-foss

Using Per-Symbology Helpers

Each supported symbology has a dedicated helper function that returns a Barcode object. The helper validates input and selects the correct encoder automatically.

import aspose_barcode_foss as barcode

# 1D barcodes
bc_128 = barcode.code128("ABC-123")
bc_39 = barcode.code39("HELLO")
bc_ean13 = barcode.ean13("590123412345")
bc_ean8 = barcode.ean8("1234567")
bc_upca = barcode.upca("01234567890")
bc_upce = barcode.upce("012345")

# 2D barcode
bc_qr = barcode.qr("https://example.com")

Using the Generic Generate Function

The generate() function accepts a symbology name string as its first argument. This is useful when the symbology is determined at runtime.

import aspose_barcode_foss as barcode

bc = barcode.generate("code128", "Dynamic data")
svg = bc.to_svg()

# Also works with other symbology names
bc_qr = barcode.generate("qr", "https://example.com")
bc_ean = barcode.generate("ean13", "590123412345")

Combining Generation with Encode Options

Pass an encode parameter to control symbology-specific options during generation.

import aspose_barcode_foss as barcode
from aspose_barcode_foss import QrOptions, QrErrorCorrectionLevel

bc = barcode.qr(
    "https://example.com",
    encode=QrOptions(error_correction_level=QrErrorCorrectionLevel.Q),
)
svg = bc.to_svg()

Reading the Encoded Symbol

The Barcode object exposes a symbol property of type EncodedSymbol, which contains the matrix (a ModuleMatrix with the module grid) and metadata (a SymbolMetadata with the symbology name, display text, and normalized data).

import aspose_barcode_foss as barcode

bc = barcode.code128("Test")
symbol = bc.symbol

# Access the module matrix
print(f"Width: {symbol.matrix.width}")
print(f"Height: {symbol.matrix.height}")

# Access symbol metadata
print(f"Symbology: {symbol.metadata.symbology}")
print(f"Display text: {symbol.metadata.display_text}")

Saving the Output

After generating a Barcode, call to_svg() for an SVG string or to_png() for PNG bytes. Write the result to a file.

import aspose_barcode_foss as barcode

bc = barcode.code128("SAVE-ME")

# Save as SVG
with open("barcode.svg", "w") as f:
    f.write(bc.to_svg())

# Save as PNG
with open("barcode.png", "wb") as f:
    f.write(bc.to_png())

Supported Symbologies

SymbologyTypeHelper FunctionOptions Class
Code 1281Dcode128()Code128Options
Code 391Dcode39()Code39Options
EAN-131Dean13()Ean13Options
EAN-81Dean8()Ean8Options
QR Code2Dqr()QrOptions
UPC-A1Dupca()UpcaOptions
UPC-E1Dupce()UpceOptions

See Also

 English