Introducere

Aspose.BarCode FOSS pentru Python generează coduri de bare în șapte simboluri și le redă în SVG și PNG. Această postare se concentrează pe pipeline-ul de redare — cum să controlezi formatul de ieșire, dimensiunile, culorile și textul lizibil pentru oameni utilizând RenderOptions, SvgRenderer și PngRenderer.

Biblioteca este disponibilă pe PyPI ca aspose-barcode-foss, lansată sub licența MIT fără dependențe native. Fiecare obiect de cod de bare expune metodele convenabile to_svg() și to_png(), plus o metodă de nivel inferior render() care acceptă orice implementare Renderer.


Caracteristici cheie

Ieșire SVG cu SvgRenderer

SvgRenderer produce markup SVG independent de rezoluție. Metoda Barcode.to_svg() este o scurtătură care utilizează SvgRenderer intern.

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

Ieșire PNG cu PngRenderer

PngRenderer produce imagini raster PNG. Controlează rezoluția cu proprietatea dpi pe RenderOptions. Metoda Barcode.to_png() este scurtătura convenabilă.

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)

Controlul scalării și DPI

RenderOptions oferă scale (multiplicator pentru dimensiunile modulelor) și dpi (puncte pe inch pentru ieșirea raster). Acestea lucrează împreună: scale afectează dimensiunea logică a barelor și spațiilor, în timp ce dpi controlează densitatea pixelilor în ieșirea 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)

Culori și fundal

Setează foreground_color și background_color ca șiruri de culoare CSS. Activează transparent_background pentru a omite umplerea fundalului (util pentru suprapunerea pe designuri existente).

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)

Controlul zonei de liniște

Proprietatea quiet_zone setează marginea goală în jurul codului de bare în unități de lățime a modulului. Majoritatea standardelor de coduri de bare necesită o zonă de liniște minimă pentru scanare fiabilă.

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)

Text lizibil pentru om

Setează show_text=True pentru a afișa datele codificate sub codul de bare. Proprietățile font_family și font_size controlează aspectul textului.

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)

Începe rapid

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)

Formate acceptate

FormatExtensieCitireScriere
SVG.svg
PNG.png

Notă: PdfRenderer există în API, dar generează NotImplementedError în versiunea curentă.


Open Source & Licențiere

Aspose.BarCode FOSS pentru Python este lansat sub Licența MIT. Codul sursă este găzduit pe GitHub. Poți folosi, modifica și redistribui biblioteca atât în proiecte open-source, cât și comerciale.


Începeți

Resurse conexe