מבוא
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)
התחלה מהירה
git clone https://github.com/aspose-barcode-foss/Aspose.BarCode-FOSS-for-Python.git
cd Aspose.BarCode-FOSS-for-Python
pip install -e .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. אתה יכול להשתמש, לשנות ולהפיץ את הספרייה גם בפרויקטים קוד פתוח וגם בפרויקטים מסחריים.