การทํางานกับตัวเลือก Encode
การทํางานกับตัวเลือก Encode
ทุกสัญลักษณ์บาร์โค้ดใน Aspose.BarCode FOSS for Python มีคลาสตัวเลือกที่มอบหมายเพื่อควบคุมพฤติกรรมการรหัส. คลาซทางเลือกทั้งหมดได้รับจาก EncodeOptions, ซึ่งให้บริการ gs1_enabled และ eci_assignment_number คุณสมบัติที่แบ่งปันผ่านสัญลักษณ์.
Code128Options
Code128Options การควบคุมที่รหัส 128 ตัวอักษรตั้งตัวโค้ดจะเลือก. encode_mode อสังหาริมทรัพย์ยอมรับการ Code128EncodeMode หมายเลขค่า.
| Mode | Description |
|---|---|
AUTO | เลือกการโค๊ดที่คับที่สุดโดยอัตโนมัติ |
CODE_A | ตัวอักษรตัวใหญ่, เลข และตัวควบคุม |
CODE_B | ตัวใหญ่, ตัวเล็ก และตัวเลข |
CODE_C | คู่ตัวเลข ปรับปรุงให้ดีสําหรับข้อมูลที่มีจํานวนเท่านั้น |
CODE_AB | การเปลี่ยนระหว่างโค้ด A และโค๊ด B |
CODE_AC | การเปลี่ยนระหว่างโค้ด A และโค๊ด C |
CODE_BC | การเปลี่ยนระหว่างโค้ด B และโค๊ด 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 ตัวอักษรหรือการขยาย Full ASCII หรือไม่.
| Property | Type | Default | Description |
|---|---|---|---|
full_ascii | bool | None | เปิดช่อง ASCII เต็ม (ระยะ 128 ตัวอักษรผ่านคู่ shift) |
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 มีพื้นผิวที่ตั้งค่ามากที่สุดในหมู่ทั้งหมดของสัญลักษณ์.
| 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 การควบคุมการจัดการตัวเลขเช็คสําหรับบาร์โค้ด EAN-13.
| Property | Type | Default | Description |
|---|---|---|---|
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 แต่สําหรับบาร์โค้ด EAN 8 หลัก. การจัดหา 7 ตัวเลขของภาระ (ห้องสมุดคํานวณที่ 8) หรือตั้ง allow_check_digit_input=True สําหรับการเข้า 8 หลัก.
from aspose_barcode_foss import Ean8Options
bc = barcode.ean8("1234567")
bc2 = barcode.ean8("12345670", encode=Ean8Options(allow_check_digit_input=True))อัพคาออปชั่น และ อูเซอพชัน
UpcaOptions และ UpceOptions ตามแบบเดียวกัน allow_check_digit_input รูปแบบเป็น EAN ตัวเลือก. 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:
| Property | Type | Description |
|---|---|---|
gs1_enabled | `bool | None` |
eci_assignment_number | `int | None` |
เคล็ดลับและปฏิบัติที่ดีที่สุด
- การใช้งาน
Code128EncodeMode.AUTOเว้นแต่คุณมีเหตุผลเฉพาะเจาะจงที่จะจํากัดชุดตัวอักษร มันจะผลิตบาร์โค้ดที่สั้นที่สุดสําหรับการเข้าใด ๆ. - เซ็ต
QrErrorCorrectionLevel.Hสําหรับรหัส QR ที่จะพิมพ์บนพื้นที่ที่มีความเสี่ยงต่อการเสียหาย. - ให้ห้องสมุดคํานวณตัวเลขเช็คโดยตั้งค่าตามพื้นฐาน เก็บไว้เท่านั้น
allow_check_digit_input=Trueเมื่อได้รับข้อมูลที่ผ่านการยืนยันก่อนจากระบบอื่น. - การใช้งาน
QrEncodeMode.NUMERICสําหรับข้อมูล QR ที่มีแต่ตัวเลข เพื่อลดขนาดสัญลักษณ์ให้เป็นอย่างน้อยที่สุด.
ปัญหาทั่วไป
| Issue | Cause | แก้ไข |
|---|---|---|
InvalidInputError ในโค้ด 39 | การเข้ามีตัวอักษรเล็กๆ | เซ็ต full_ascii=True ใน Code39Options |
| รหัส QR กว่าขนาดใหญ่มาก | วัดเวอร์ชั่นต่ําเกินไปสําหรับความยาวของข้อมูล | ลบคําแปลอย่างชัดเจน version หรือเพิ่มมันขึ้น |
| เลขตรวจสอบผิดที่ EAN-13 | 13-digit input without allow_check_digit_input=True | แจก 12 หลัก (อัตโนมัติ) หรือตั้งตัวเลือกเป็น True |
SymbologyNotFoundError | ชื่อสัญลักษณ์ที่ผิดคําใน generate() | ใช้หนึ่งใน: code128, code39, ean13, ean8, qr, upca, upce |