条形码生成用例

条形码生成用例

Aspose.BarCode FOSS for Python 支持七种符号集:Code 128、Code 39、EAN-13、EAN-8、QR Code、UPC-A和UPC-E。本文展示了每种的实际场景。

库存管理

Code 128 是库存标签的常用选择,因为它可以编码包括特殊字符在内的字母数字数据。每个物品都会在标签上打印唯一标识符。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# Generate a Code 128 barcode for an inventory SKU
barcode = service.generate("code128", "SKU-2026-A1234")
svg_content = barcode.to_svg(RenderOptions(scale=2.0, show_text=True))

with open("inventory_label.svg", "w") as f:
    f.write(svg_content)

运输标签

Code 128 和 Code 39 在运输和物流中被广泛使用。Code 39 更为简洁(大写字母、数字和少量符号),用于旧系统。Code 128 支持更广泛的字符集。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# Code 39 for a tracking number
barcode = service.generate("code39", "SHIP2026071234")
png_bytes = barcode.to_png(RenderOptions(dpi=300, show_text=True))

with open("shipping_label.png", "wb") as f:
    f.write(png_bytes)

零售销售点

EAN-13 和 UPC-A 是零售产品的标准符号集。EAN-13 在国际上使用;UPC-A 是12位的北美变体。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# EAN-13 for a product barcode (12 digits; check digit auto-computed)
barcode = service.generate("ean13", "590123412345")
svg_content = barcode.to_svg(RenderOptions(scale=3.0, show_text=True))

# UPC-A for a North American product
barcode_upc = service.generate("upca", "01234567890")
svg_upc = barcode_upc.to_svg(RenderOptions(scale=3.0, show_text=True))

文档追踪

Code 128 在文档追踪系统中表现良好,系统为每个文档分配唯一的字母数字标识符。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# Generate barcodes for a batch of documents
doc_ids = ["DOC-2026-001", "DOC-2026-002", "DOC-2026-003"]
for doc_id in doc_ids:
    barcode = service.generate("code128", doc_id)
    svg = barcode.to_svg(RenderOptions(scale=1.5, show_text=True))
    filename = f"{doc_id.lower().replace('-', '_')}.svg"
    with open(filename, "w") as f:
        f.write(svg)

使用 QR 代码进行 URL 和联系人编码

QR Code 对任意文本数据进行编码,包括 URL、联系信息和 Wi-Fi 凭证。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# QR Code for a URL
barcode = service.generate("qrcode", "https://products.aspose.org/barcode/")
png_bytes = barcode.to_png(RenderOptions(scale=4.0))

with open("url_qrcode.png", "wb") as f:
    f.write(png_bytes)

使用 EAN-8 和 UPC-E 的小件标签

EAN-8 和 UPC-E 是针对空间受限物品的紧凑符号系统。EAN-8 使用 7 或 8 位数字;UPC-E 是 UPC-A 的压缩版本。

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# EAN-8 for a small product (7 digits; check digit auto-computed)
barcode = service.generate("ean8", "1234567")
svg = barcode.to_svg(RenderOptions(scale=2.0, show_text=True))

常见问题

我应该选择哪种符号系统?

  • 字母数字数据(任何字符): Code 128
  • 仅大写字母,遗留系统: Code 39
  • 零售产品(国际): EAN-13 或 EAN-8
  • 零售产品(北美): UPC-A 或 UPC-E
  • URL、文本、二进制数据: QR Code

我可以批量生成条形码吗?

是的。只需创建一次 BarcodeService 实例,然后在循环中调用 generate()。每次调用都会返回一个 Barcode 对象,您可以使用 to_svg() 或 to_png() 独立渲染它。

有哪些可用的输出格式?

该库通过 to_svg() 渲染为 SVG,通过 to_png() 渲染为 PNG。PDF 渲染尚未实现。

另请参阅

 中文