Decoding Direct Part Marking (DPM) Barcode

Direct Part Marking (DPM) is a process to mark equipment with some information, such as barcodes, permanently. The typical DPM barcode symbologies include DataMatrix and QR code. Since version 7.2, Dynamsoft Barcode Reader SDK has been capable of decoding DPM barcodes. In this article, I will share how to create a simple python barcode reader to read the DPM DataMatrix code.

How to Use Python to Decode Direct Part Marking DataMatrix

Get the source code of Python barcode extension built with Dynamsoft Barcode Reader C/C++ SDK.

Follow the README.MD file to build and install the Python extension for Windows, Linux, or macOS.

To decode barcodes from an image file, we can use the following code:

from dbr import DynamsoftBarcodeReader
dbr = DynamsoftBarcodeReader()
dbr.initLicense('LICENSE-KEY')
results = dbr.decodeFile(fileName, dbr.BF_ALL)
for result in results:
    print('barcode format: ' + result[0])
    print('barcode value: ' + result[1])

Note: you can get a free trial license key by signing up a Dynamsoft account.

Here is a DPM DataMatrix image.

dpm datamatrix

If we run the code above to decode the DPM image, it will fail to get the barcode results. The reason is the default setting does not support DPM decoding. You have to adjust parameters against the DPM case.

According to the online documentation, we can turn on the DPM mode by using the following C code:

runtimeSettings.furtherModes.dpmCodeReadingModes[0] = DPMCRM_GENERAL;
runtimeSettings.localizationModes[0] = LM_STATISTICS_MARKS;

Dynamsoft Barcode Reader SDK supports inputting the JSON-formatted parameter template. We can first peek the default parameters and find relevant properties:

params = dbr.getParameters()
print(params)

DPM mode parameters

To update the parameter setting:

  1. Convert the parameter JSON string to a JSON object:
    import json
    json_obj = json.loads(params)
    
  2. Update the DPM-relevant property values:
    templateName = json_obj['ImageParameter']['Name']
    json_obj['ImageParameter']['DPMCodeReadingModes'][0]['Mode'] = 'DPMCRM_GENERAL'
    json_obj['ImageParameter']['LocalizationModes'][0]['Mode'] = 'LM_STATISTICS_MARKS'
    
  3. Convert the JSON object to string and update the parameters for optimizing barcode algorithm:
    params = json.dumps(json_obj)
    ret = dbr.setParameters(params)
    

Now we can successfully decode the Direct Part Marking DataMatrix code:

Direct Part Marking DataMatrix

The full code:

from dbr import DynamsoftBarcodeReader
dbr = DynamsoftBarcodeReader()
dbr.initLicense('LICENSE-KEY')

params = dbr.getParameters()
print(params)

import json
json_obj = json.loads(params)
# Update JSON object
templateName = json_obj['ImageParameter']['Name']
# DPM
json_obj['ImageParameter']['DPMCodeReadingModes'][0]['Mode'] = 'DPMCRM_GENERAL'
json_obj['ImageParameter']['LocalizationModes'][0]['Mode'] = 'LM_STATISTICS_MARKS'
# Convert JSON object to string
params = json.dumps(json_obj)
# Set parameters
ret = dbr.setParameters(params)

results = dbr.decodeFile('dpm.jpg', dbr.BF_ALL)
for result in results:
    print('barcode format: ' + result[0])
    print('barcode value: ' + result[1])

Source Code

https://github.com/dynamsoft-dbr/python