How to Use Tesseract OCR as an Assist for Barcode Scan

When scanning barcodes in production (warehouse, logistics, healthcare, retail), image quality issues like blur, motion, glare, low contrast, or physical damage can cause even strong barcode algorithms to fail. Fortunately, most linear (1D) barcodes also include human-readable text. That text becomes a powerful assist layer: if the barcode fails, OCR (Optical Character Recognition) can still recover the payload — or help validate borderline reads.

This tutorial shows how to pair two complementary technologies:

  • Dynamsoft Barcode Reader – robust, high-speed, localization‑aware barcode decoding.
  • Tesseract OCR – open-source OCR used here as a fallback / verification tool for damaged or low-quality images that cannot be decoded by barcode SDK.

Instead of treating OCR as a replacement (it is slower and less structured), we treat it as a confidence booster and resilience mechanism.

Prerequisites

  • Get a free trial (30 days) for Dynamsoft Barcode Reader
  • Python 3.8+
  • Install Tesseract OCR:
    • Windows: Install UB Mannheim build; ensure C:\\Program Files\\Tesseract-OCR is on PATH
    • macOS:

        brew install tesseract
      
    • Linux:
        sudo apt update
        sudo apt install tesseract-ocr -y
        sudo apt install libtesseract-dev -y
      
  • Python dependencies:

      pip install dynamsoft-capture-vision-bundle pytesseract pillow
    

Sample Images

The project includes two test images:

  • codabar.jpg - A clear Codabar barcode image

    codabar

  • damaged.png - A damaged barcode image to demonstrate OCR fallback

    damaged

Reading a Barcode and Validating With OCR

Below is the core script app.py. It:

  1. Initializes the Dynamsoft license.
  2. Repeatedly accepts an image path.
  3. Attempts barcode decoding.
  4. Always performs OCR digit extraction.
  5. Prints both results so you can compare and validate.
from PIL import Image
import pytesseract
import os
import sys
from dynamsoft_capture_vision_bundle import LicenseManager, EnumErrorCode, CaptureVisionRouter, EnumPresetTemplate
def main():

    print("**********************************************************")
    print("Welcome to Dynamsoft Barcode Reader")
    print("**********************************************************")

    error_code, error_message = LicenseManager.init_license(
        "LICENSE-KEY")
    if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
        print("License initialization failed: ErrorCode:",
              error_code, ", ErrorString:", error_message)
    else:
        cvr_instance = CaptureVisionRouter()
        while (True):
            image_path = input(
                ">> Input your image full path:\n"
                ">> 'Enter' for sample image or 'Q'/'q' to quit\n"
            ).strip('\'"')

            if image_path.lower() == "q":
                sys.exit(0)

            if image_path == "":
                image_path = "codabar.jpg"

            if not os.path.exists(image_path):
                print("The image path does not exist.")
                continue
            result = cvr_instance.capture(
                image_path, EnumPresetTemplate.PT_READ_BARCODES.value)
            if result.get_error_code() != EnumErrorCode.EC_OK:
                print("Error:", result.get_error_code(),
                      result.get_error_string())
            else:
            
                items = result.get_items()
                print('Found {} barcodes.'.format(len(items)))
                for item in items:
                    format_type = item.get_format_string()
                    text = item.get_text()
                    print("Barcode Format:", format_type)
                    print("Barcode Text:", text)

                    location = item.get_location()
                    x1 = location.points[0].x
                    y1 = location.points[0].y
                    x2 = location.points[1].x
                    y2 = location.points[1].y
                    x3 = location.points[2].x
                    y3 = location.points[2].y
                    x4 = location.points[3].x
                    y4 = location.points[3].y
                    print("Location Points:")
                    print("({}, {})".format(x1, y1))
                    print("({}, {})".format(x2, y2))
                    print("({}, {})".format(x3, y3))
                    print("({}, {})".format(x4, y4))
                    print("-------------------------------------------------")


            ## Inovke Tesseract OCR
            result = pytesseract.image_to_string(Image.open(image_path))
            digits = ''
            for i in result:
                if ord(i) >= 48 and ord(i) <= 57:
                    digits += i

            print(f'OCR Result: {digits}')

if __name__ == "__main__":
    main()

You must replace LICENSE-KEY with your own.

Running the Barcode/OCR Application with Sample Images

  • codabar.jpg

    Read barcodes with Dynamsoft Barcode Reader

  • damaged.png

    OCR for damaged

Source Code

https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/tesseract