PyZXing

1.1.1 · active · verified Thu Apr 16

PyZXing is a Python wrapper for the popular ZXing Java library, providing functionality primarily for decoding 1D and 2D barcodes (like QR codes) from image files or in-memory image bytes. It currently stands at version 1.1.1 and has an irregular release cadence, largely driven by updates to the underlying ZXing Java core.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the BarCodeReader and attempts to decode a barcode from a specified image file. Note that `pyzxing` calls an external Java process, requiring a JRE installation. Results are returned as a list of dictionaries.

import os
from pyzxing import BarCodeReader

# For demonstration, create a dummy image file if it doesn't exist
# In a real scenario, 'barcode.png' would be an actual image with a barcode.
dummy_image_path = 'barcode.png'
if not os.path.exists(dummy_image_path):
    print(f"Warning: '{dummy_image_path}' not found. Decoding might fail. Please provide a real barcode image.")
    # Example of how you might create a dummy (non-barcode) file to prevent FileNotFoundError
    with open(dummy_image_path, 'w') as f:
        f.write('This is not an image.')

try:
    reader = BarCodeReader()
    # Decode from a file path
    results = reader.decode(dummy_image_path)
    if results:
        for result in results:
            print(f"Decoded text: {result.get('parsed', 'N/A')}")
            print(f"Format: {result.get('format', 'N/A')}")
    else:
        print(f"No barcode found in {dummy_image_path}")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy file if created for demonstration
    if not os.path.exists(dummy_image_path):
        os.remove(dummy_image_path)

view raw JSON →