مقدمه

Aspose.BarCode متن‌باز برای Python بارکدها را در هفت نمادگذاری تولید می‌کند و آن‌ها را به SVG و PNG رندر می‌نماید. این پست بر روی خط لوله رندر تمرکز دارد — چگونگی کنترل فرمت خروجی، ابعاد، رنگ‌ها و متن قابل‌خواندن برای انسان با استفاده از RenderOptions، SvgRenderer و PngRenderer.

کتابخانه بر روی PyPI به عنوان aspose-barcode-foss در دسترس است، تحت مجوز MIT منتشر شده و بدون وابستگی‌های بومی می‌باشد. هر شیء بارکد to_svg() و to_png() را به عنوان متدهای راحتی ارائه می‌دهد، به‌علاوه یک متد سطح پایین render() که هر پیاده‌سازی Renderer را می‌پذیرد.


ویژگی‌های کلیدی

خروجی SVG با SvgRenderer

SvgRenderer علامت‌گذاری SVG مستقل از وضوح را تولید می‌کند. متد Barcode.to_svg() یک میانبر است که به‌صورت داخلی از SvgRenderer استفاده می‌کند.

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))

خروجی PNG با PngRenderer

PngRenderer تصاویر PNG رستر تولید می‌کند. رزولوشن را با ویژگی dpi روی RenderOptions کنترل کنید. روش Barcode.to_png() یک میانبر راحت است.

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)

کنترل مقیاس و DPI

RenderOptions scale (ضرب‌کننده برای ابعاد ماژول) و dpi (دات در اینچ برای خروجی رستر) را فراهم می‌کند. این‌ها با هم کار می‌کنند: scale بر اندازه منطقی خطوط و فضاها تأثیر می‌گذارد، در حالی که dpi چگالی پیکسل در خروجی 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)

رنگ‌ها و پس‌زمینه

foreground_color و background_color را به‌عنوان رشته‌های رنگی CSS تنظیم کنید. فعال‌سازی transparent_background باعث حذف پر کردن پس‌زمینه می‌شود (برای قرار دادن روی طرح‌های موجود مفید است).

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)

کنترل ناحیهٔ ساکت

ویژگی quiet_zone حاشیهٔ خالی اطراف بارکد را بر حسب واحدهای عرض ماژول تنظیم می‌کند. اکثر استانداردهای بارکد برای اسکن قابل اعتماد، نیاز به ناحیهٔ ساکت حداقل دارند.

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)

متن قابل خواندن برای انسان

مقدار show_text=True را تنظیم کنید تا داده‌های رمزگذاری‌شده زیر بارکد نمایش داده شود. ویژگی‌های font_family و font_size ظاهر متن را کنترل می‌کنند.

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)

شروع سریع

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)

فرمت‌های پشتیبانی‌شده

قالبپسوندخواندننوشتن
SVG.svg
PNG.png

توجه: PdfRenderer در API وجود دارد اما در نسخهٔ فعلی NotImplementedError را ایجاد می‌کند.


منبع باز و مجوزدهی

Aspose.BarCode FOSS برای Python تحت مجوز MIT منتشر شده است. کد منبع در GitHub میزبانی می‌شود. شما می‌توانید کتابخانه را هم در پروژه‌های منبع باز و هم تجاری استفاده، تغییر و توزیع کنید.


شروع کار

منابع مرتبط