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 DynamsoftBarcodeReaderdbr = 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.
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)
To update the parameter setting:
- Convert the parameter JSON string to a JSON object:
import jsonjson_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:
The full code:
Source Code
Originally published at https://www.codepool.biz on October 9, 2019.