使用 Encode 选项
使用编码选项
在 Aspose.BarCode FOSS for Python 中,每种条形码符号都有一个专用的选项类,用于控制编码行为。所有选项类都继承自 EncodeOptions,它提供了在各符号之间共享的 gs1_enabled 和 eci_assignment_number 属性。
Code128Options
Code128Options 控制编码器选择的 Code 128 字符集。encode_mode 属性接受一个 Code128EncodeMode 枚举值。
| Mode | 描述 |
|---|---|
AUTO | 自动选择最紧凑的编码 |
CODE_A | 大写字母、数字和控制字符 |
CODE_B | 大写字母、小写字母和数字 |
CODE_C | 数字对 — 为仅数字数据优化 |
CODE_AB | 在 Code A 和 Code B 之间切换 |
CODE_AC | 在 Code A 和 Code C 之间切换 |
CODE_BC | 在 Code B 和 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 控制编码器是使用基础的 43 字符集还是完整的 ASCII 扩展。
| 属性 | 类型 | Default | 描述 |
|---|---|---|---|
full_ascii | bool | None | 启用完整 ASCII 模式(通过 shift 组合实现 128 字符范围) |
add_check_digit | bool | None | 添加一个 modulo-43 校验位 |
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 在所有符号系统中拥有最多的配置选项。
| 属性 | 类型 | 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(控制符号尺寸) |
mask | int | 0–7(手动掩码模式选择) |
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 控制 EAN-13 条形码的校验位处理。
| 属性 | 类型 | Default | 描述 |
|---|---|---|---|
allow_check_digit_input | bool | None | 接受带有预先计算校验位的13位输入 |
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 的工作方式与 Ean13Options 完全相同,但适用于8位 EAN 条形码。提供 7 位有效负载数字(库会计算第 8 位),或者为8位输入设置 allow_check_digit_input=True。
from aspose_barcode_foss import Ean8Options
bc = barcode.ean8("1234567")
bc2 = barcode.ean8("12345670", encode=Ean8Options(allow_check_digit_input=True))UpcaOptions 和 UpceOptions
UpcaOptions 和 UpceOptions 遵循与 EAN 选项相同的 allow_check_digit_input 模式。UpceOptions 也具有 number_system 属性。
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 基础属性
所有选项类都从 EncodeOptions 继承这些属性:
| 属性 | 类型 | 描述 |
|---|---|---|
gs1_enabled | `bool | None` |
eci_assignment_number | `int | None` |
提示与最佳实践
- 除非有特定理由限制字符集——使用
Code128EncodeMode.AUTO,它会为任何输入生成最短的条形码。 - 对将在易受损表面上打印的 QR 码,设置
QrErrorCorrectionLevel.H。 - 默认让库计算校验位 —— 仅在从其他系统收到预先验证的数据时才设置
allow_check_digit_input=True。 - 对仅包含数字的 QR 数据使用
QrEncodeMode.NUMERIC,以最小化符号尺寸。
常见问题
| 问题 | 原因 | 修复 |
|---|---|---|
InvalidInputError 在 Code 39 上 | 输入包含小写字母 | 在 Code39Options 中设置 full_ascii=True |
| QR code 太大 | 版本设置过低,无法满足数据长度 | 删除显式的 version 或增大它 |
| 在 EAN-13 上的校验位错误 | 13 位输入缺少 allow_check_digit_input=True | 传入12位(自动)或将选项设置为 True |
SymbologyNotFoundError | 在 generate() 中的符号名称拼写错误 | 使用以下之一:code128,code39,ean13,ean8,qr,upca,upce |