はじめに
Aspose.BarCode の Python 用FOSSは、7つのシンボルでバーコードを生成し、SVGとPNGにレンダリングします。本稿では、レンダリングパイプラインに焦点を当て、RenderOptions、SvgRenderer、PngRenderer を使用して出力形式、サイズ、色、そして人間可読テキストを制御する方法を解説します。
このライブラリは PyPI で aspose-barcode-foss として利用可能で、MITライセンスの下でリリースされており、ネイティブ依存はありません。すべてのバーコードオブジェクトは to_svg() と to_png() の便利メソッドを提供し、さらに任意の Renderer 実装を受け入れる低レベルの render() メソッドも備えています。
主な機能
SvgRenderer を使用したSVG出力
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))
PngRenderer を使用したPNG出力
PngRenderer はラスタ PNG 画像を生成します。解像度は RenderOptions の dpi プロパティで制御します。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(ラスタ出力の 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 の Python 用 FOSS は MIT License の下でリリースされています。ソースコードは GitHub にホストされています。ライブラリはオープンソースプロジェクトでも商用プロジェクトでも使用、変更、再配布できます。