مقدمة
Aspose.BarCode FOSS لـ 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. يمكنك استخدام المكتبة، تعديلها، وإعادة توزيعها في كل من المشاريع المفتوحة المصدر والمشاريع التجارية.