Working with Encode Options

Working with Encode Options

Working with Encode Options

Each barcode symbology in Aspose.BarCode FOSS for Python has a dedicated options class that controls encoding behavior. All option classes inherit from EncodeOptions, which provides gs1_enabled and eci_assignment_number properties shared across symbologies.


Code128Options

Code128Options controls which Code 128 character sets the encoder selects. The encode_mode property accepts a Code128EncodeMode enum value.

ModeDescription
AUTOSelects the most compact encoding automatically
CODE_AUppercase, digits, and control characters
CODE_BUppercase, lowercase, and digits
CODE_CDigit pairs — optimized for numeric-only data
CODE_ABSwitches between Code A and Code B
CODE_ACSwitches between Code A and Code C
CODE_BCSwitches between Code B and Code C
import aspose_barcode_foss as barcode
from aspose_barcode_foss import Code128Options, Code128EncodeMode

# AUTO mode — library picks the most compact encoding
bc_auto = barcode.code128("ABC123", encode=Code128Options(encode_mode=Code128EncodeMode.AUTO))

# CODE_C mode — compact encoding for digit-only data
bc_c = barcode.code128("9876543210", encode=Code128Options(encode_mode=Code128EncodeMode.CODE_C))

# CODE_B mode — full alphanumeric support
bc_b = barcode.code128("Hello World", encode=Code128Options(encode_mode=Code128EncodeMode.CODE_B))

Code39Options

Code39Options controls whether the encoder uses the base 43-character set or the Full ASCII extension.

PropertyTypeDefaultDescription
full_asciiboolNoneEnable Full ASCII mode (128-character range via shift pairs)
add_check_digitboolNoneAppend a modulo-43 check digit
from aspose_barcode_foss import Code39Options

# Base mode — uppercase letters, digits, and a few special characters
bc_base = barcode.code39("CODE 39")

# Full ASCII mode — encodes the full 128-character ASCII range
bc_full = barcode.code39(
    "Hello, World!",
    encode=Code39Options(full_ascii=True, add_check_digit=True),
)

QrOptions

QrOptions has the most configuration surface among all symbologies.

PropertyTypeValues
error_correction_levelQrErrorCorrectionLevelL (7%), M (15%), Q (25%), H (30%)
encoding_modeQrEncodeModeAUTO, NUMERIC, ALPHANUMERIC, BYTE, KANJI
versionint1–40 (controls symbol dimensions)
maskint0–7 (manual mask pattern selection)
from aspose_barcode_foss import QrOptions, QrErrorCorrectionLevel, QrEncodeMode

# High error correction with byte encoding
bc = barcode.qr(
    "https://example.com",
    encode=QrOptions(
        error_correction_level=QrErrorCorrectionLevel.H,
        encoding_mode=QrEncodeMode.BYTE,
    ),
)

# Numeric mode for digit-only data — most compact QR
bc_num = barcode.qr(
    "0123456789",
    encode=QrOptions(encoding_mode=QrEncodeMode.NUMERIC),
)

# Explicit version and mask
bc_v5 = barcode.qr(
    "TEST",
    encode=QrOptions(version=5, mask=3),
)

Ean13Options

Ean13Options controls check digit handling for EAN-13 barcodes.

PropertyTypeDefaultDescription
allow_check_digit_inputboolNoneAccept 13-digit input with pre-computed check digit
from aspose_barcode_foss import Ean13Options

# 12 digits — library computes the 13th check digit
bc = barcode.ean13("590123412345")

# 13 digits — library validates the provided check digit
bc2 = barcode.ean13(
    "5901234123457",
    encode=Ean13Options(allow_check_digit_input=True),
)

Ean8Options

Ean8Options works identically to Ean13Options but for 8-digit EAN barcodes. Supply 7 payload digits (library computes the 8th), or set allow_check_digit_input=True for 8-digit input.

from aspose_barcode_foss import Ean8Options

bc = barcode.ean8("1234567")
bc2 = barcode.ean8("12345670", encode=Ean8Options(allow_check_digit_input=True))

UpcaOptions and UpceOptions

UpcaOptions and UpceOptions follow the same allow_check_digit_input pattern as EAN options. UpceOptions also has a number_system property.

from aspose_barcode_foss import UpcaOptions, UpceOptions

# UPC-A: 11 digits, library adds check digit
bc_a = barcode.upca("01234567890")

# UPC-E with pre-computed check digit
bc_e = barcode.upce(
    "01234565",
    encode=UpceOptions(allow_check_digit_input=True),
)

EncodeOptions Base Properties

All option classes inherit these properties from EncodeOptions:

PropertyTypeDescription
gs1_enabledbool | NoneEnable GS1 encoding for the symbology
eci_assignment_numberint | NoneSet ECI metadata for character set identification

Tips and Best Practices

  • Use Code128EncodeMode.AUTO unless you have a specific reason to restrict character sets — it produces the shortest barcode for any input.
  • Set QrErrorCorrectionLevel.H for QR codes that will be printed on surfaces prone to damage.
  • Let the library compute check digits by default — only set allow_check_digit_input=True when receiving pre-validated data from another system.
  • Use QrEncodeMode.NUMERIC for digit-only QR data to minimize symbol size.

Common Issues

IssueCauseFix
InvalidInputError on Code 39Input contains lowercase lettersSet full_ascii=True in Code39Options
QR code too largeVersion set too low for the data lengthRemove explicit version or increase it
Wrong check digit on EAN-1313-digit input without allow_check_digit_input=TruePass 12 digits (auto) or set the option to True
SymbologyNotFoundErrorMisspelled symbology name in generate()Use one of: code128, code39, ean13, ean8, qr, upca, upce

See Also

 English