MENU
Table of contents

Thanks for downloading Dynamsoft Barcode Reader Package!

Your download will start shortly. If your download does not begin, click here to retry.

User Guide for Python SDK

System Requirements

  • Operating systems:
    • Windows x64
    • Linux (x64, ARM32, ARM64)
    • macOS (10.15+)
  • Supported Python Versions: Python 2.7 (for versions below DBR 7.2.2.3), Python 3.5 (for versions below DBR 7.5), Python 3.6, Python 3.7, Python 3.8

Installation

pip install dbr

Getting Started: HelloWorld

  1. Create a text document. Let’s rename it to BarcodeReadDemo_python.py.
  2. Import dbr package in BarcodeReadDemo_python.py.

     from dbr import *
    

    Before this, please make sure dbr python package has been installed successful. You can start terminal or command prompt to run command “pip show dbr” to show dbr python package information.

  3. Update the main code in BarcodeReadDemo_python.py.

    license_key = "<your license key here>"
    image = r"<your image file full path>"
    
    reader = BarcodeReader()
    
    reader.init_license(license_key)
    
    try:
        text_results = reader.decode_file(image)
    
        if text_results != None:
            for text_result in text_results:
                print("Barcode Format :")
                print(text_result.barcode_format_string)
                print("Barcode Text :")
                print(text_result.barcode_text)
                print("Localization Points : ")
                print(text_result.localization_result.localization_points)
                print("-------------")
    except BarcodeReaderError as bre:
        print(bre)
    

    Please update <your image file full path> and <your license key here> in the code accordingly.

  4. Run BarcodeReadDemo_python.py.

Decoding Methods

The SDK provides multiple decoding methods that support reading barcodes from different sources, including static images, video stream, files in memory, base64 string, etc. Here is a list of all decoding methods:

You can find more samples in more programming languages at Code Gallery.

Barcode Reading Settings

Calling the decoding methods directly will use the default scanning modes and it will satisfy most of the needs. The SDK also allows you to adjust the scanning settings to optimize the scanning performance for different usage scenarios.

There are two ways to change the barcode reading settings - using the PublicRuntimeSettings Class or template. For new developers, We recommend you to start with the PublicRuntimeSettings Class; For those who are experienced with the SDK, you may use a template which is more flexible and easier to update.

Use PublicRuntimeSettings Class to Change Settings

Here are some common scanning settings you might find helpful:

For more scanning settings guide, check out the How To section.

Specify Barcode Type to Read

By default, the SDK will read all the supported barcode formats except Postal Codes and Dotcode from the image. (See Product Overview for the full supported barcode list.)

If your full license only covers some barcode formats, you can use BarcodeFormatIds and BarcodeFormatIds_2 to specify the barcode format(s). Check out BarcodeFormat and BarcodeFormat_2.

For example, to enable only 1D barcode reading, you can use the following code:

license_key = "<your license key here>"
image = r"<your image file full path>"

reader = BarcodeReader()

reader.init_license(license_key)

settings = reader.get_runtime_settings()
settings.barcode_format_ids = EnumBarcodeFormat.BF_ONED

try:
    reader.update_runtime_settings(settings)
    text_results = reader.decode_file(image)

    if text_results != None:
        for text_result in text_results:
            print("Barcode Format :")
            print(text_result.barcode_format_string)
            print("Barcode Text :")
            print(text_result.barcode_text)
            print("Localization Points : ")
            print(text_result.localization_result.localization_points)
            print("-------------")
except BarcodeReaderError as bre:
    print(bre)

Specify maximum barcode count

By default, the SDK will read as many barcodes as it can. To increase the recognition efficiency, you can use expectedBarcodesCount to specify the maximum number of barcodes to recognize according to your scenario.

   license_key = "<your license key here>"
   image = r"<your image file full path>"

   reader = BarcodeReader()

   reader.init_license(license_key)

   settings = reader.get_runtime_settings()
   settings.expected_barcodes_count = 1

   try:
      reader.update_runtime_settings(settings)
      text_results = reader.decode_file(image)

      if text_results != None:
         for text_result in text_results:
               print("Barcode Format :")
               print(text_result.barcode_format_string)
               print("Barcode Text :")
               print(text_result.barcode_text)
               print("Localization Points : ")
               print(text_result.localization_result.localization_points)
               print("-------------")
   except BarcodeReaderError as bre:
      print(bre)

Specify a scan region

By default, the barcode reader will search the whole image for barcodes. This can lead to poor performance especially when dealing with high-resolution images. You can speed up the recognition process by restricting the scanning region.

To specify a region, you will need to define an area. The following code shows how to create a template string and define the region.

   license_key = "<your license key here>"
   image = r"<your image file full path>"

   reader = BarcodeReader()

   reader.init_license(license_key)

   settings = reader.get_runtime_settings()
   settings.region_bottom  = 100
   settings.region_left    = 0
   settings.region_right   = 50
   settings.region_top     = 0
   settings.region_measured_by_percentage   = 1

   try:
      reader.update_runtime_settings(settings)
      text_results = reader.decode_file(image)

      if text_results != None:
         for text_result in text_results:
               print("Barcode Format :")
               print(text_result.barcode_format_string)
               print("Barcode Text :")
               print(text_result.barcode_text)
               print("Localization Points : ")
               print(text_result.localization_result.localization_points)
               print("-------------")
   except BarcodeReaderError as bre:
      print(bre)

Use A Template to Change Settings

Besides the option of using the PublicRuntimeSettings struct, the SDK also provides init_runtime_settings_with_string and init_runtime_settings_with_file APIs that enable you to use a template to control all the runtime settings. With a template, instead of writing many codes to modify the settings, you can manage all the runtime settings in a JSON file/string.

   license_key = "<your license key here>"
   image = r"<your image file full path>"
   json_file = r"<your template file path>"

   reader = BarcodeReader()

   reader.init_license(license_key)

   error = reader.init_runtime_settings_with_file(json_file)

   if error[0] != EnumErrorCode.DBR_OK:
      print(error[1])

   try:
      reader.update_runtime_settings(settings)
      text_results = reader.decode_file(image)

      if text_results != None:
         for text_result in text_results:
               print("Barcode Format :")
               print(text_result.barcode_format_string)
               print("Barcode Text :")
               print(text_result.barcode_text)
               print("Localization Points : ")
               print(text_result.localization_result.localization_points)
               print("-------------")
   except BarcodeReaderError as bre:
      print(bre)

Below is a template for your reference. To learn more about the APIs, you can check out PublicRuntimeSettings class.

{
   "ImageParameter" : {
      "BarcodeFormatIds" : [ "BF_ALL" ],
      "BinarizationModes" : [
         {
            "BlockSizeX" : 0,
            "BlockSizeY" : 0,
            "EnableFillBinaryVacancy" : 1,
            "ImagePreprocessingModesIndex" : -1,
            "Mode" : "BM_LOCAL_BLOCK",
            "ThreshValueCoefficient" : 10
         }
      ],
      "Description" : "",
      "ExpectedBarcodesCount" : 0,
      "GrayscaleTransformationModes" : [
         {
            "Mode" : "GTM_ORIGINAL"
         }
      ],
      "ImagePreprocessingModes" : [
         {
            "Mode" : "IPM_GENERAL"
         }
      ],
      "IntermediateResultSavingMode" : {
         "Mode" : "IRSM_MEMORY"
      },
      "IntermediateResultTypes" : [ "IRT_NO_RESULT" ],
      "MaxAlgorithmThreadCount" : 4,
      "Name" : "runtimesettings",
      "PDFRasterDPI" : 300,
      "Pages" : "",
      "RegionDefinitionNameArray" : null,
      "RegionPredetectionModes" : [
         {
            "Mode" : "RPM_GENERAL"
         }
      ],
      "ResultCoordinateType" : "RCT_PIXEL",
      "ScaleDownThreshold" : 2300,
      "TerminatePhase" : "TP_BARCODE_RECOGNIZED",
      "TextFilterModes" : [
         {
            "MinImageDimension" : 65536,
            "Mode" : "TFM_GENERAL_CONTOUR",
            "Sensitivity" : 0
         }
      ],
      "TextResultOrderModes" : [
         {
            "Mode" : "TROM_CONFIDENCE"
         },
         {
            "Mode" : "TROM_POSITION"
         },
         {
            "Mode" : "TROM_FORMAT"
         }
      ],
      "TextureDetectionModes" : [
         {
            "Mode" : "TDM_GENERAL_WIDTH_CONCENTRATION",
            "Sensitivity" : 5
         }
      ],
      "Timeout" : 10000
   },
   "Version" : "3.0"
}

This page is compatible for:

Version 7.5

Is this page helpful?

YesYes NoNo

In this article:

latest version

    • Latest Version
    • version 7.6
    • version 7.5
    Change +
    © 2003–2020 Dynamsoft. All rights reserved.
    Privacy Statement / Site Map / Home / Purchase / Support