Quickstart
Quickstart — Aspose.BarCode FOSS for Python
This guide walks through generating barcodes using the three most common symbologies: Code 128, QR Code, and EAN-13. Each example demonstrates the high-level API, per-symbology encode options, and rendering controls.
Generate a Code 128 Barcode
Code 128 is a variable-length 1D symbology used in logistics and shipping. The code128() helper accepts a data string and optional Code128Options:
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()
with open("code128.svg", "w") as f:
f.write(svg)Code128EncodeMode.CODE_C is optimized for digit-only data — it encodes pairs of digits as single code points, producing a shorter barcode.
Generate a QR Code
QR Code is a 2D symbology supporting error correction and multiple encoding modes. QrOptions controls error correction level, encoding mode, version, and mask pattern:
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,
),
)
with open("qr.png", "wb") as f:
f.write(bc.to_png())Error correction levels: L (7% recovery), M (15%), Q (25%), H (30%).
Generate an EAN-13 Barcode
EAN-13 is a fixed-length 1D symbology used in retail. Supply 12 payload digits — the library computes the 13th check digit automatically:
import aspose_barcode_foss as barcode
bc = barcode.ean13("590123412345")
svg = bc.to_svg()
with open("ean13.svg", "w") as f:
f.write(svg)To supply your own pre-computed check digit (13 digits total), set allow_check_digit_input=True:
from aspose_barcode_foss import Ean13Options
bc = barcode.ean13(
"5901234123457",
encode=Ean13Options(allow_check_digit_input=True),
)Customize Rendering
RenderOptions applies to all symbologies and controls the visual output. Pass it to to_svg() or to_png():
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",
))
with open("upca.png", "wb") as f:
f.write(png)Available properties: scale, dpi, module_width, module_height, quiet_zone, foreground_color, background_color, transparent_background, show_text, font_family, font_size.
Use the Generic generate() API
The generate() function accepts a symbology name string instead of calling a dedicated helper:
import aspose_barcode_foss as barcode
bc = barcode.generate("code128", "ABC123")
svg = bc.to_svg()Valid symbology names: "code128", "code39", "ean13", "ean8", "qr", "upca", "upce".
Error Handling
All validation happens before encoding. If invalid data or incompatible options are provided, the library raises a typed exception:
import aspose_barcode_foss as barcode
from aspose_barcode_foss import InvalidInputError
try:
bc = barcode.ean13("ABC") # EAN-13 requires digits
except InvalidInputError as e:
print(f"Invalid input: {e}")Exception hierarchy:
| Exception | When raised |
|---|---|
InvalidInputError | Data contains characters not supported by the symbology |
EncodingError | Encoding fails (e.g., data exceeds symbology capacity) |
SymbologyNotFoundError | Unrecognized symbology name passed to generate() |
UnsupportedCapabilityError | Requested feature not available for this symbology |
All exception classes inherit from BarcodeError.
Supported Symbologies
| Symbology | Type | Options Class | Key Properties |
|---|---|---|---|
| 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 |
Output formats: SVG and PNG.
Next Steps
- Developer Guide: Per-symbology encode options, rendering controls, and architecture
- Features: Complete feature overview with code examples
- KB Articles: How-to guides and FAQ