はじめに
Aspose.BarCode FOSS for Python は、サポートされている 7 つのバーコード形式すべてにわたるエンコード動作を制御するシンボルごとのオプションクラスを提供します。各オプションクラスは EncodeOptions から継承し、そのシンボル固有のプロパティを追加します — Code 128 エンコードモード、Code 39 ASCII 選択、QR エラー訂正レベル、そして EAN/UPC チェックデジット処理。
この記事では各オプションクラス、そのプロパティ、および動作するコード例について説明します。クラスはトップレベルの aspose_barcode_foss パッケージから利用可能です。
主な機能
Code 128 エンコードモード
Code128Options はエンコーダが使用する Code 128 文字セットを制御します。encode_mode プロパティは Code128EncodeMode 列挙値を受け取ります:AUTO、CODE_A、CODE_B、CODE_C、CODE_AB、CODE_AC、または CODE_BC。AUTO は最もコンパクトなエンコードを自動的に選択します。CODE_C は数字のみのデータに最適化されており(数字のペアが単一のコードポイントとしてエンコードされます)。
import aspose_barcode_foss as barcode
from aspose_barcode_foss import Code128Options, Code128EncodeMode
bc = barcode.code128(
"1234567890",
encode=Code128Options(encode_mode=Code128EncodeMode.CODE_C),
)
svg = bc.to_svg()
Code 128 はまた、EncodeOptions から継承された gs1_enabled および eci_assignment_number プロパティをサポートし、GS1 と ECI メタデータに対応します。
Code 39 エンコーディング
Code39Options はエンコーダがベース43文字セットを使用するか、Full ASCII拡張を使用するかを制御します。full_ascii=True を設定してFull ASCIIモードを有効にすると、シフトペアを使用して128文字のASCII全範囲をエンコードします。オプションの add_check_digit プロパティは modulo-43 チェックデジットを付加します。
import aspose_barcode_foss as barcode
from aspose_barcode_foss import Code39Options
bc = barcode.code39(
"Hello",
encode=Code39Options(full_ascii=True, add_check_digit=True),
)
svg = bc.to_svg()
QR Code 設定
QrOptions はサポートされているシンボロジーの中で最も豊富な構成セットを提供します:
error_correction_level:QrErrorCorrectionLevel.L(7%),.M(15%),.Q(25%),.H(30%)encoding_mode:QrEncodeMode.AUTO,.NUMERIC,.ALPHANUMERIC,.BYTE,.KANJIversion: QR バージョン (1–40)、シンボルサイズを制御mask: マスクパターン (0–7) を手動で選択
import aspose_barcode_foss as barcode
from aspose_barcode_foss import QrOptions, QrErrorCorrectionLevel, QrEncodeMode
bc = barcode.qr(
"https://example.com/product/12345",
encode=QrOptions(
error_correction_level=QrErrorCorrectionLevel.Q,
encoding_mode=QrEncodeMode.BYTE,
),
)
png_bytes = bc.to_png()
EAN と UPC のチェックデジット
Ean13Options、Ean8Options、UpcaOptions、および UpceOptions はそれぞれ allow_check_digit_input プロパティを持ちます。デフォルトでは、ライブラリはデータからチェックデジットを自動的に計算します。データにすでにチェックデジットが含まれている場合は allow_check_digit_input=True を設定してください。
import aspose_barcode_foss as barcode
from aspose_barcode_foss import Ean13Options
# Automatic check digit (12 digits in, library appends the 13th)
bc = barcode.ean13("590123412345")
svg = bc.to_svg()
# Pre-computed check digit (13 digits, library validates)
bc2 = barcode.ean13(
"5901234123457",
encode=Ean13Options(allow_check_digit_input=True),
)
レンダリングオプション
RenderOptions はすべてのシンボロジーに適用され、ビジュアル出力を制御します。to_svg() または to_png() に渡してください:
| プロパティ | 型 | 説明 |
|---|---|---|
scale | float | 均一スケール倍率 |
dpi | int | PNG出力の解像度 |
module_width | float | 1つのバーコードモジュールの幅 |
module_height | float | 1つのバーコードモジュールの高さ |
quiet_zone | float | バーコードの周囲の余白 |
foreground_color | str | バーの16進カラーコード(例: "#000000") |
background_color | str | 背景の16進カラーコード |
transparent_background | bool | PNG の透過背景 |
show_text | bool | バーコードの下に人が読めるテキストを表示 |
font_family | str | 人が読めるテキストのフォント |
font_size | float | 人が読みやすいテキストの Font size |
import aspose_barcode_foss as barcode
from aspose_barcode_foss import RenderOptions
bc = barcode.upca("01234567890")
png = bc.to_png(options=RenderOptions(
scale=3.0,
dpi=300,
foreground_color="#1a1a1a",
background_color="#ffffff",
show_text=True,
font_family="Courier New",
))
エラーハンドリング
すべてのエンコードオプションの検証は解析時に行われます。無効なデータや互換性のないオプションが提供された場合、エンコードが開始される前にライブラリは型付き例外をスローします:
InvalidInputError— データにシンボロジーでサポートされていない文字が含まれていますEncodingError— エンコードに失敗しました(例:データが容量を超える)SymbologyNotFoundError—generate()に渡された認識できないシンボロジー名UnsupportedCapabilityError— このシンボロジーでは要求された機能は利用できません
すべての例外クラスは BarcodeError を継承します。
クイックスタート
git clone https://github.com/aspose-barcode-foss/Aspose.BarCode-FOSS-for-Python.git
cd Aspose.BarCode-FOSS-for-Python
pip install -e .さまざまなエンコードオプションでバーコードを生成し、保存します:
import aspose_barcode_foss as barcode
from aspose_barcode_foss import (
Code128Options, Code128EncodeMode,
QrOptions, QrErrorCorrectionLevel,
RenderOptions,
)
# Code 128 in CODE_C mode (compact digit encoding)
c128 = barcode.code128(
"9876543210",
encode=Code128Options(encode_mode=Code128EncodeMode.CODE_C),
)
with open("code128.svg", "w") as f:
f.write(c128.to_svg())
# QR with high error correction
qr = barcode.qr(
"https://example.com",
encode=QrOptions(error_correction_level=QrErrorCorrectionLevel.H),
)
with open("qr.png", "wb") as f:
f.write(qr.to_png(options=RenderOptions(scale=4.0)))
# EAN-13 with custom colors
ean = barcode.ean13("590123412345")
with open("ean13.svg", "w") as f:
f.write(ean.to_svg(options=RenderOptions(
foreground_color="#003366",
show_text=True,
)))
サポートされているフォーマット
| Symbology | 型 | Options クラス | キー プロパティ |
|---|---|---|---|
| Code 128 | 1D | Code128Options | encode_mode |
| Code 39 | 1D | Code39Options | full_ascii, add_check_digit |
| EAN-13 | 1D | Ean13Options | allow_check_digit_input |
| EAN-8 | 1D | Ean8Options | allow_check_digit_input |
| QR Code | 2D | QrOptions | error_correction_level, encoding_mode, version, mask |
| UPC-A | 1D | UpcaOptions | allow_check_digit_input |
| UPC-E | 1D | UpceOptions | allow_check_digit_input |
出力フォーマット:
| 形式 | 拡張機能 | Export |
|---|---|---|
| SVG | .svg | ✓ |
| PNG | .png | ✓ |
オープンソース & ライセンス
Aspose.BarCode Python 用の FOSS は MIT ライセンスです。ソースは GitHub で入手可能です; ソースからビルドしてください。ネイティブ依存関係はなく、テレメトリもなく、使用制限もありません。