การทํางานกับตัวเลือก Encode

การทํางานกับตัวเลือก Encode

ทุกสัญลักษณ์บาร์โค้ดใน Aspose.BarCode FOSS for Python มีคลาสตัวเลือกที่มอบหมายเพื่อควบคุมพฤติกรรมการรหัส. คลาซทางเลือกทั้งหมดได้รับจาก EncodeOptions, ซึ่งให้บริการ gs1_enabled และ eci_assignment_number คุณสมบัติที่แบ่งปันผ่านสัญลักษณ์.


Code128Options

Code128Options การควบคุมที่รหัส 128 ตัวอักษรตั้งตัวโค้ดจะเลือก. encode_mode อสังหาริมทรัพย์ยอมรับการ Code128EncodeMode หมายเลขค่า.

ModeDescription
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 หรือไม่.

PropertyTypeDefaultDescription
full_asciiboolNoneเปิดช่อง ASCII เต็ม (ระยะ 128 ตัวอักษรผ่านคู่ shift)
add_check_digitboolNoneเพิ่มตัวเลขตรวจสอบ 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 มีพื้นผิวที่ตั้งค่ามากที่สุดในหมู่ทั้งหมดของสัญลักษณ์.

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 การควบคุมการจัดการตัวเลขเช็คสําหรับบาร์โค้ด EAN-13.

PropertyTypeDefaultDescription
allow_check_digit_inputboolNoneรับการเข้า 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:

PropertyTypeDescription
gs1_enabled`boolNone`
eci_assignment_number`intNone`

เคล็ดลับและปฏิบัติที่ดีที่สุด

  • การใช้งาน Code128EncodeMode.AUTO เว้นแต่คุณมีเหตุผลเฉพาะเจาะจงที่จะจํากัดชุดตัวอักษร มันจะผลิตบาร์โค้ดที่สั้นที่สุดสําหรับการเข้าใด ๆ.
  • เซ็ต QrErrorCorrectionLevel.H สําหรับรหัส QR ที่จะพิมพ์บนพื้นที่ที่มีความเสี่ยงต่อการเสียหาย.
  • ให้ห้องสมุดคํานวณตัวเลขเช็คโดยตั้งค่าตามพื้นฐาน เก็บไว้เท่านั้น allow_check_digit_input=True เมื่อได้รับข้อมูลที่ผ่านการยืนยันก่อนจากระบบอื่น.
  • การใช้งาน QrEncodeMode.NUMERIC สําหรับข้อมูล QR ที่มีแต่ตัวเลข เพื่อลดขนาดสัญลักษณ์ให้เป็นอย่างน้อยที่สุด.

ปัญหาทั่วไป

IssueCauseแก้ไข
InvalidInputError ในโค้ด 39การเข้ามีตัวอักษรเล็กๆเซ็ต full_ascii=True ใน Code39Options
รหัส QR กว่าขนาดใหญ่มากวัดเวอร์ชั่นต่ําเกินไปสําหรับความยาวของข้อมูลลบคําแปลอย่างชัดเจน version หรือเพิ่มมันขึ้น
เลขตรวจสอบผิดที่ EAN-1313-digit input without allow_check_digit_input=Trueแจก 12 หลัก (อัตโนมัติ) หรือตั้งตัวเลือกเป็น True
SymbologyNotFoundErrorชื่อสัญลักษณ์ที่ผิดคําใน generate()ใช้หนึ่งใน: code128, code39, ean13, ean8, qr, upca, upce

See Also

 ภาษาไทย