How to Configure Encode Options in Aspose.BarCode FOSS

How to Configure Encode Options in Aspose.BarCode FOSS

Problem

Each barcode symbology has its own encoding rules — error correction levels for QR Code, encode modes for Code 128, check digit handling for EAN/UPC. Aspose.BarCode FOSS for Python provides a dedicated options class per symbology so you can control these parameters at generation time.

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.H),
)
svg = bc.to_svg()

Prerequisites

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

QR Code Options

The QrOptions class controls QR Code encoding. Set error_correction_level to one of QrErrorCorrectionLevel.L, M, Q, or H. Higher levels increase redundancy at the cost of larger symbols. Use encoding_mode to select a specific QR data mode (QrEncodeMode.NUMERIC, ALPHANUMERIC, BYTE, or KANJI), or leave it as AUTO for automatic selection. The version property (1–40) controls symbol size, and mask (0–7) overrides the mask pattern.

from aspose_barcode_foss import QrOptions, QrErrorCorrectionLevel, QrEncodeMode

options = QrOptions(
    error_correction_level=QrErrorCorrectionLevel.H,
    encoding_mode=QrEncodeMode.BYTE,
    version=5,
)
bc = barcode.qr("DATA", encode=options)

Code 128 Options

The Code128Options class exposes encode_mode with values from Code128EncodeMode: AUTO, CODE_A, CODE_B, CODE_C, CODE_AB, CODE_AC, CODE_BC. In AUTO mode the encoder selects the shortest representation. Restricting to a specific subset (e.g., CODE_C for numeric-only data) can reduce symbol width.

from aspose_barcode_foss import Code128Options, Code128EncodeMode

options = Code128Options(encode_mode=Code128EncodeMode.CODE_C)
bc = barcode.code128("123456", encode=options)

Code 39 Options

The Code39Options class has two key properties: full_ascii enables Full ASCII encoding (extended character set beyond uppercase letters and digits), and add_check_digit appends a modulo-43 check digit to the symbol.

from aspose_barcode_foss import Code39Options

options = Code39Options(full_ascii=True, add_check_digit=True)
bc = barcode.code39("Hello", encode=options)

EAN and UPC Check Digit Options

Ean13Options, Ean8Options, UpcaOptions, and UpceOptions all support allow_check_digit_input. By default, the library computes the check digit automatically — supply only the payload digits (12 for EAN-13, 7 for EAN-8, 11 for UPC-A). Set allow_check_digit_input=True to pass a pre-computed check digit as part of the input.

from aspose_barcode_foss import Ean13Options

# Auto-compute check digit (12 digits)
bc = barcode.ean13("590123412345")

# Pre-computed check digit (13 digits)
bc = barcode.ean13(
    "5901234123457",
    encode=Ean13Options(allow_check_digit_input=True),
)

UPC-E Number System

UpceOptions adds a number_system property. UPC-E barcodes use number system “0” or “1”. Set this property to control which number system prefix the encoder uses.

from aspose_barcode_foss import UpceOptions

options = UpceOptions(number_system="0", allow_check_digit_input=False)
bc = barcode.upce("012345", encode=options)

Options Summary

SymbologyOptions ClassKey Properties
Code 128Code128Optionsencode_mode
Code 39Code39Optionsfull_ascii, add_check_digit
EAN-13Ean13Optionsallow_check_digit_input
EAN-8Ean8Optionsallow_check_digit_input
QR CodeQrOptionserror_correction_level, encoding_mode, version, mask
UPC-AUpcaOptionsallow_check_digit_input
UPC-EUpceOptionsnumber_system, allow_check_digit_input

See Also

 English