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.
| Mode | Description |
|---|---|
AUTO | Selects the most compact encoding automatically |
CODE_A | Uppercase, digits, and control characters |
CODE_B | Uppercase, lowercase, and digits |
CODE_C | Digit pairs — optimized for numeric-only data |
CODE_AB | Switches between Code A and Code B |
CODE_AC | Switches between Code A and Code C |
CODE_BC | Switches 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.
| Property | Type | Default | Description |
|---|---|---|---|
full_ascii | bool | None | Enable Full ASCII mode (128-character range via shift pairs) |
add_check_digit | bool | None | Append 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.
| Property | Type | Values |
|---|---|---|
error_correction_level | QrErrorCorrectionLevel | L (7%), M (15%), Q (25%), H (30%) |
encoding_mode | QrEncodeMode | AUTO, NUMERIC, ALPHANUMERIC, BYTE, KANJI |
version | int | 1–40 (controls symbol dimensions) |
mask | int | 0–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.
| Property | Type | Default | Description |
|---|---|---|---|
allow_check_digit_input | bool | None | Accept 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:
| Property | Type | Description |
|---|---|---|
gs1_enabled | bool | None | Enable GS1 encoding for the symbology |
eci_assignment_number | int | None | Set ECI metadata for character set identification |
Tips and Best Practices
- Use
Code128EncodeMode.AUTOunless you have a specific reason to restrict character sets — it produces the shortest barcode for any input. - Set
QrErrorCorrectionLevel.Hfor 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=Truewhen receiving pre-validated data from another system. - Use
QrEncodeMode.NUMERICfor digit-only QR data to minimize symbol size.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
InvalidInputError on Code 39 | Input contains lowercase letters | Set full_ascii=True in Code39Options |
| QR code too large | Version set too low for the data length | Remove explicit version or increase it |
| Wrong check digit on EAN-13 | 13-digit input without allow_check_digit_input=True | Pass 12 digits (auto) or set the option to True |
SymbologyNotFoundError | Misspelled symbology name in generate() | Use one of: code128, code39, ean13, ean8, qr, upca, upce |