Giới thiệu

Aspose.BarCode FOSS cho Python tạo mã vạch trong bảy biểu tượng và kết xuất chúng sang SVG và PNG. Bài viết này tập trung vào quy trình kết xuất — cách kiểm soát định dạng đầu ra, kích thước, màu sắc và văn bản có thể đọc được bằng con người bằng cách sử dụng RenderOptions, SvgRenderer, và PngRenderer.

Thư viện có sẵn trên PyPI dưới tên aspose-barcode-foss, được phát hành dưới giấy phép MIT mà không có phụ thuộc gốc. Mỗi đối tượng mã vạch cung cấp các phương thức tiện lợi to_svg()to_png(), cộng thêm một phương thức render() cấp thấp hơn chấp nhận bất kỳ triển khai Renderer nào.


Các tính năng chính

Kết xuất SVG với SvgRenderer

SvgRenderer tạo ra mã SVG không phụ thuộc vào độ phân giải. Phương thức Barcode.to_svg() là một lối tắt sử dụng SvgRenderer bên trong.

from aspose_barcode_foss import BarcodeService, RenderOptions
from aspose_barcode_foss.rendering import SvgRenderer

service = BarcodeService()
barcode = service.generate("code128", "RENDER-TEST-001")

# Direct convenience method
svg_string = barcode.to_svg(RenderOptions(scale=2.0))

# Explicit renderer usage
renderer = SvgRenderer()
result = barcode.render(renderer, RenderOptions(scale=2.0, show_text=True))

Kết xuất PNG với PngRenderer

PngRenderer tạo ra các hình ảnh PNG raster. Kiểm soát độ phân giải bằng thuộc tính dpi trên RenderOptions. Phương pháp Barcode.to_png() là phím tắt tiện lợi.

from aspose_barcode_foss import BarcodeService, RenderOptions
from aspose_barcode_foss.rendering import PngRenderer

service = BarcodeService()
barcode = service.generate("ean13", "590123412345")

# Convenience method — returns bytes
png_bytes = barcode.to_png(RenderOptions(dpi=300, scale=3.0))

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

Kiểm soát Tỷ lệ và DPI

RenderOptions cung cấp scale (bộ nhân cho kích thước mô-đun) và dpi (điểm trên mỗi inch cho đầu ra raster). Chúng hoạt động cùng nhau: scale ảnh hưởng đến kích thước logic của các thanh và khoảng trống, trong khi dpi kiểm soát mật độ pixel trong đầu ra PNG.

from aspose_barcode_foss import RenderOptions

# High-resolution print label
print_options = RenderOptions(scale=4.0, dpi=600)

# Screen display
screen_options = RenderOptions(scale=1.0, dpi=96)

Màu sắc và Nền

Đặt foreground_colorbackground_color dưới dạng chuỗi màu CSS. Bật transparent_background để bỏ qua việc tô nền (hữu ích khi chồng lên các thiết kế hiện có).

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()
barcode = service.generate("qrcode", "https://example.com")

options = RenderOptions(
    foreground_color="#003366",
    background_color="#FFFFFF",
    scale=3.0,
)
svg = barcode.to_svg(options)

Kiểm soát Vùng yên tĩnh

Thuộc tính quiet_zone đặt lề trống quanh mã vạch tính bằng đơn vị chiều rộng mô-đun. Hầu hết các tiêu chuẩn mã vạch yêu cầu một vùng yên tĩnh tối thiểu để quét một cách đáng tin cậy.

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()
barcode = service.generate("code39", "QUIET-ZONE")

# Wider quiet zone for print use
options = RenderOptions(quiet_zone=10.0, scale=2.0)
svg = barcode.to_svg(options)

Văn bản có thể đọc được

Đặt show_text=True để hiển thị dữ liệu đã mã hoá phía dưới mã vạch. Các thuộc tính font_familyfont_size kiểm soát cách hiển thị văn bản.

from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()
barcode = service.generate("upca", "01234567890")

options = RenderOptions(
    show_text=True,
    font_family="monospace",
    font_size=12.0,
    scale=2.5,
)
svg = barcode.to_svg(options)

Bắt đầu nhanh

asposefoss/barcode is not yet published — build from source until it ships. See the project README for build instructions.
from aspose_barcode_foss import BarcodeService, RenderOptions

service = BarcodeService()

# Generate a Code 128 barcode
barcode = service.generate("code128", "HELLO-2026")

# Render to SVG with custom options
options = RenderOptions(
    scale=2.0,
    show_text=True,
    foreground_color="#000000",
    background_color="#FFFFFF",
    quiet_zone=4.0,
)

svg_output = barcode.to_svg(options)
with open("hello.svg", "w") as f:
    f.write(svg_output)

# Render to PNG at 300 DPI
png_bytes = barcode.to_png(RenderOptions(dpi=300, scale=3.0))
with open("hello.png", "wb") as f:
    f.write(png_bytes)

Định dạng được hỗ trợ

Định dạngPhần mở rộngĐọcGhi
SVG.svg
PNG.png

Lưu ý: PdfRenderer tồn tại trong API nhưng gây ra NotImplementedError trong phiên bản hiện tại.


Mã nguồn mở & Giấy phép

Aspose.BarCode FOSS cho Python được phát hành dưới giấy phép MIT. Mã nguồn được lưu trữ trên GitHub. Bạn có thể sử dụng, sửa đổi và phân phối lại thư viện trong cả dự án mã nguồn mở và thương mại.


Bắt đầu

Tài nguyên liên quan