How to Read Nonstandard 1D Barcode with Dynamsoft Barcode SDK
Start and stop characters are defined as part of linear barcode (1D barcode) standard. However, you may suffer from some nonstandard 1D barcode symbologies in which the start and stop characters are somehow changed. In this article, I will take Code39 as an example, demonstrating how to decode nonstandard 1D barcodes using Dynamsoft Barcode Reader SDK.
Development Environment
- Python 3.x
Python Barcode SDK Installation
Dynamsoft Barcode Reader SDK only supports Python 3.x.
pip install dbr
Nonstandard 1D Barcode Recognition
For comparison, I prepared three Code39 images with different start and stop characters.
Standard Code39 Symbology (*)
Nonstandard Code39 Symbology (+)
Nonstandard Code39 Symbology (-)
Here is the code snippet for decoding a standard 1D barcode image:
from dbr import *
license_key = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" # https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
reader = BarcodeReader()
reader.init_license(license_key)
try:
text_results = reader.decode_file(filename)
if text_results != None:
for text_result in text_results:
print('Barcode Format:')
print(text_result.barcode_format_string)
print('')
print('Barcode Text:')
print(text_result.barcode_text)
print('')
print('Localization Points:')
print(text_result.localization_result.localization_points)
print('------------------------------------------------')
print('')
except BarcodeReaderError as bre:
print(bre)
You need to get a free trial license before running the code.
If you run the code with nonstandard 1D barcode symbologies, there is no result. According to the online documentation, the nonstandard barcode is defined as an extended format in EnumBarcodeFormat_2.
Therefore, you need to make a little bit change to the code above:
print(text_result.barcode_format_string_2)
In addition, you have to set the start and stop characters related to the barcode type. A simple way is to load a JSON-formatted parameter template file:
{
"ImageParameter": {
"BarcodeFormatIds_2": [ "BF2_NONSTANDARD_BARCODE" ],
"FormatSpecificationNameArray": [ "FormatSpecification1" ],
"DeblurLevel": 9,
"Description": "",
"ExpectedBarcodesCount": 0,
"LocalizationModes": [
{
"Mode": "LM_CONNECTED_BLOCKS"
},
{
"Mode": "LM_SCAN_DIRECTLY",
"ScanStride": 0
},
{
"Mode": "LM_STATISTICS"
},
{
"Mode": "LM_LINES"
}
],
"Name": "Test",
"Timeout": 1000000
},
"FormatSpecification": {
"Name": "FormatSpecification1",
"BarcodeFormatIds_2": [ "BF2_NONSTANDARD_BARCODE" ],
"StandardFormat": "BF_CODE_39",
"HeadModuleRatio": "131113131",
"TailModuleRatio": "131113131"
},
"Version": "3.0"
}
For nonstandard barcodes, you just need to modify the following part:
"StandardFormat": "BF_CODE_39",
"HeadModuleRatio": "131113131",
"TailModuleRatio": "131113131"
- StandardFormat: the barcode symbology
- HeadModuleRatio: the start character
- TailModuleRatio: the stop character
By referencing the Code39 character table, “131113131” represents “+” and “131111313” represents “-“.
Now you can create a custom template file and load it in Python code:
json_file = None
if special_character == '+':
json_file = r"template_plus.json"
if special_character == '-':
json_file = r"template_minus.json"
if json_file == None:
return
error = reader.init_runtime_settings_with_file(json_file)
if error[0] != EnumErrorCode.DBR_OK:
print(error[1])
Run my Python code to read the standard and nonstandard Code39 barcodes: