-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a nonstandard 1D barcode detection example
- Loading branch information
Showing
8 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Xiao Ling | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Decoding Nonstandard 1D Barcodes in Python with Dynamsoft Barcode SDK | ||
This sample demonstrates how to use the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/overview/) to decode nonstandard 1D barcodes. | ||
|
||
## Environment | ||
Python 3.x | ||
|
||
## Prerequisites | ||
- Obtain a [Dynamsoft Barcode Reader trial license](ttps://www.dynamsoft.com/customer/license/trialLicense) | ||
- Install the Dynamsoft Barcode Reader SDK for Python: | ||
|
||
```bash | ||
pip install dbr | ||
``` | ||
|
||
## Getting Started | ||
|
||
1. Set the license key in `test.py`: | ||
|
||
```python | ||
license_key = "LICENSE-KEY" | ||
``` | ||
|
||
2. Define the barcode format and specify the start/stop characters. Below is an example configuration for [Code39](https://en.wikipedia.org/wiki/Code_39): | ||
|
||
```json | ||
"StandardFormat": "BF_CODE_39", | ||
"HeadModuleRatio": "131111313", | ||
"TailModuleRatio": "131111313" | ||
``` | ||
|
||
3. After configuring the template file, use the following code to read the barcode: | ||
|
||
```python | ||
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: | ||
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_2) | ||
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) | ||
``` | ||
4. Run the script: | ||
```bash | ||
python test.py | ||
``` | ||
![python nonstandard 1D barcode detection](https://www.dynamsoft.com/codepool/img/2020/06/nonstandard-1d-barcode-recognition.png) | ||
## Blog | ||
[How to Read Nonstandard 1D Barcode with Dynamsoft Barcode SDK](https://www.dynamsoft.com/codepool/read-nonstandard-1d-barcode-barcode-sdk.html) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions
35
examples/official/9.x/nonstandard_1D_barcode/template_minus.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"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": "131111313", | ||
"TailModuleRatio": "131111313" | ||
}, | ||
"Version": "3.0" | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
examples/official/9.x/nonstandard_1D_barcode/template_plus.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"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" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from dbr import * | ||
|
||
license_key = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" | ||
BarcodeReader.init_license(license_key) | ||
reader = BarcodeReader() | ||
|
||
|
||
def decode(filename, is_standard, special_character=None): | ||
try: | ||
text_results = reader.decode_file(filename) | ||
if text_results != None: | ||
for text_result in text_results: | ||
print('Barcode Format:') | ||
if is_standard: | ||
print(text_result.barcode_format_string) | ||
else: | ||
print(text_result.barcode_format_string_2 + | ||
'. Start/Stop character is ' + special_character + '.') | ||
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) | ||
|
||
|
||
def decode_non_standard(special_character, filename): | ||
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]) | ||
|
||
decode(filename, False, special_character) | ||
|
||
|
||
def decode_standard(filename): | ||
decode(filename, True) | ||
|
||
|
||
def main(): | ||
decode_standard('code39_standard.PNG') | ||
decode_non_standard('+', 'code39_plus.PNG') | ||
decode_non_standard('-', 'code39_minus.PNG') | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |