Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / Python
Article

The New Power of Dynamsoft Barcode SDK: Decoding Direct Part Marking (DPM) Codes

23 Oct 2019CPOL 4.1K   1  
In this article, we discuss the unique challenges DPM codes present, and how Dynamsoft Barcode Reader overcomes these challenges with just a few lines of code.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Direct Part Marking (DPM) is a process to mark equipment with product information permanently. The marked information is generally encoded in barcodes, such as Data Matrix and QR Code. Since version 7.2, Dynamsoft Barcode Reader SDK has added support for decoding DPM DataMatrix codes.

In this article, we discuss the unique challenges DPM codes present, and how Dynamsoft Barcode Reader overcomes these challenges with just a few lines of code.

Where DPM is Used

DPM is efficient for tracking parts through their lifecycle, especially in harsh environments. It is commonly adopted in the following industries:

  • Electronic manufacturing
  • Automotive
  • Aerospace
  • Telecommunications
  • Healthcare equipment and devices

Image 1

The Challenges of Decoding DPM Codes

The general methods of marking barcodes on parts include laser, dot peen, and electrochemical etch, which increase the algorithm complexity.

There are four possible scenarios:

  1. Color and Contrast – the foreground and background colors are too similar to distinguish
  2. Dots – if the dots are not distributed evenly, it is easy to cause decoding errors
  3. Surface Imperfections – part surfaces may be damaged through their lifecycle, which makes barcodes unreadable
  4. Lighting – part images may be shot in a dark environment lacking lighting, which is essential for boosting the contrast between foreground and background (e.g., warehouse)

Decoding DPM Barcodes in C/C++

Dynamsoft Barcode Reader SDK is built in C/C++. The default settings do not support DPM decoding.

Step 1. Download the trial SDK

You can download the trial here: Dynamsoft Barcode Reader v7.2.

Step 2. Instantiate the barcode reader object

CBarcodeReader reader;

Step 3. Update the parameters

According to the online documentation, there are two parameters related to the DPM mode:

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

To support DPM, update the parameters after instantiating the barcode reader object:

char sError[512];
PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
reader.GetRuntimeSettings(runtimeSettings);
runtimeSettings->furtherModes.dpmCodeReadingModes[0] = DPMCRM_GENERAL;runtimeSettings->localizationModes[0] = LM_STATISTICS_MARKS;
reader.UpdateRuntimeSettings(runtimeSettings, sError, 512);

Step 4. Get a trial license key

Note: a valid license key is required. You can get a free trial license here by registering a new Dynamsoft account. Set a valid license before compiling the project with CMake:

reader.InitLicense("LICENSE-KEY");

Get the full C++ code:

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

Decoding DPM Barcodes in Python

Based on the C/C++ APIs, we can create an open-source project with wrapper code for CPython, which allows developers to rapidly build a barcode reader app with a few lines of Python code.

Step 1. Build Python Barcode Module

Get the source code of Python barcode extension and follow the below steps to build and install the Python module for Windows, Linux, and macOS platforms.

Windows

Create a system environment variable for locating Visual Studio 2015 common tools:

SET VS90COMNTOOLS=%VS140COMNTOOLS%

Update the library paths in src/setup.py:

dbr_lib_dir = r'<Dynamsoft\Barcode Reader 7.2>\Components\C_C++\Lib'
dbr_dll = r'<Dynamsoft\Barcode Reader 7.2>\Components\C_C++\Redist\x64'

Build and install the module:

cd src
python2/3 setup.py build install

Linux and macOS

Export the library path you used:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<YOUR-LIBRARY-PATH>

Build and install the module:

cd src
python2/3 setup.py build install

Step 2. Create the BarcodeReader Object

To decode barcodes from an image file, the code snippet is as follows:

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])

Here is an example image.

Image 2

Step 3. Update the Algorithm Parameters to Enable DPM

Dynamsoft Barcode Reader SDK supports the JSON-formatted parameter template as the input. You can output the default parameters and find relevant properties for update in Python:

params = dbr.getParameters()
print(params)

Image 3

To set the property values:

  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 the barcode algorithm:
    params = json.dumps(json_obj)
    ret = dbr.setParameters(params)

Step 4. Run the code

We can successfully decode the Direct Part Marking DataMatrix code:

Image 4

The full Python 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])

https://github.com/dynamsoft-dbr/python/blob/7.x/examples/command-line/test.py

Building a GUI App with OpenCV

A GUI app is more straightforward than the console app for showing the barcode localization. Pass an OpenCV image buffer to the dbr.decodeBuffer() method and render the barcode localization results with cv2.imshow():

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

import cv2
image = cv2.imread('dpm.jpg', 1)
results = dbr.decodeBuffer(image, dbr.BF_ALL, templateName)

thickness = 2
color = (0,255,0)
for result in results:
    print("barcode format: " + result[0])
    print("barcode value: " + result[1])
    x1 = result[2]
    y1 = result[3]
    x2 = result[4]
    y2 = result[5]
    x3 = result[6]
    y3 = result[7]
    x4 = result[8]
    y4 = result[9]

    cv2.line(image, (x1, y1), (x2, y2), color, thickness)
    cv2.line(image, (x2, y2), (x3, y3), color, thickness)
    cv2.line(image, (x3, y3), (x4, y4), color, thickness)
    cv2.line(image, (x4, y4), (x1, y1), color, thickness)

cv2.imshow("Localization", image)
cv2.waitKey(0)

Now the barcode decoding app looks much better:

Image 5

Try the Online Demo

If you do not want to write code, please feel free to try the online barcode demo.

Image 6

Related Articles

Dynamsoft Barcode Reader v7.2 Added Support for Direct Part Marking (DPM)

Technical Support

If you have any questions about Dynamsoft Barcode Reader SDK, please feel free to contact support@dynamsoft.com.

Release History

v7.2, 09/24/2019

NEW

  • Added more barcode formats:
    1. GS1 Databar (Omnidirectional, Truncated,
      Stacked, Stacked Omnidirectional, Limited,
      Expanded, Expanded Stacked)
    2. PatchCode
    3. Maxicode (mode 2-5)
    4. Micro PDF417
    5. Micro QR
    6. GS1 COMPOSITE (a combination of OneD
      and PDF417/Micro PDF 417)
    7. Non-standard Barcode
  • Added the capability of reading DPM code. It can be enabled by turning on the struct PublicRuntimeSettings->furtherMode-> DPMCodeReadingModes and adding LM_STATISTICS_MARKS to the PublicRuntimeSettings->localizationModes.
  • Licensing is required to obtain the intermediate results, except the original image in the intermediate results.
  • Added a parameter, clarity in the struct ExtendedResult, to show the clarity of the decoded-barcode zone.
  • For .NET, added a method, GetModeArgument(), to get the value of the argument for the mode parameters.
  • For C/C++, added a method, DBR_GetModeArgument()/GetModeArgument(), to get the argument value of the mode parameters.
  • For Java, added a method, getModeArgument(), to get the argument value of the mode parameters.

IMPROVED

  • Improved the decoding speed for PDF417.
  • Improved the capability of decoding QR and Data Matrix with cylinder-like deformation.

FIXED

  • Fixed minor bugs

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer Dynamsoft
Canada Canada
Xiao Ling is A technical content writer at Dynamsoft, the leading company of document capture and image processing SDKs.
He is in charge of managing Dynamsoft blog site codepool.biz and Dynamsoft GitHub community.

Comments and Discussions

 
-- There are no messages in this forum --