Reading barcodes with vision extraction
Use Vision extraction to read barcodes as part of document layout analysis. This workflow extracts text, tables, reading order, and machine-readable codes in one pass.
Use this guide when a document processing pipeline needs barcode values together with surrounding document context, such as page location or nearby text.
Download sampleSupported barcode types
Vision barcode recognition supports common 1D and 2D symbologies, including:
- Code 128, Code 39, EAN, UPC, and other 1D formats
- QR codes and Micro QR codes
- PDF417
- DataMatrix
- Aztec
- MaxiCode
Recognition quality depends on image resolution, barcode size, contrast, rotation, and damage. Use a direct barcode reader for workflows that only need barcode values from a single image.
Complete implementation
Import the classes used by the sample:
from nutrient_sdk import Documentfrom nutrient_sdk import NutrientExceptionfrom nutrient_sdk import Visionfrom nutrient_sdk import VisionEngineOpen the document, select the ICR engine, and extract the document layout as JSON:
def main(): try: with Document.open("input_barcode_1d.png") as document: document.settings.vision_settings.engine = VisionEngine.ICR
vision = Vision.set(document) content_json = vision.extract_content()
with open("output.json", "w", encoding="utf-8") as f: f.write(content_json)
print("Successfully extracted document layout and barcode data to output.json") except NutrientException as e: print(f"Error: {e}")
if __name__ == "__main__": main()extract_content() returns JSON that includes detected barcode elements when the input contains supported barcodes. Each barcode element contains the decoded value and symbology information, together with its location in the document layout.
Markdown output
Markdown export surfaces barcode values as text in reading order. It doesn’t preserve the full coordinate data available in JSON.
Use JSON when downstream code needs page numbers, bounding boxes, decoded values, or symbology metadata. For the full layout JSON structure, refer to the JSON data extraction guide.
Error handling
Vision API raises VisionException when extraction fails. VisionException derives from NutrientException, so catching NutrientException covers Vision-specific failures. Common causes include unreadable input files, unsupported image data, missing Vision resources, insufficient memory, or low-quality barcode images.
In production code:
- Catch
NutrientException. - Log the input file and Vision engine used for extraction.
- Route low-quality scans to manual review or retry with a higher-resolution source image.
Summary
This workflow extracts barcode values through the Vision document layout pipeline. Use it when barcode values need to remain connected to the document structure, page location, and surrounding text.