|
| 1 | +# Decoding Nonstandard 1D Barcodes in Python with Dynamsoft Barcode SDK |
| 2 | +This sample demonstrates how to use the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/overview/) to decode nonstandard 1D barcodes. |
| 3 | + |
| 4 | +## Environment |
| 5 | +Python 3.x |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | +- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) |
| 9 | +- Install the Dynamsoft Barcode Reader SDK for Python: |
| 10 | + |
| 11 | + ```bash |
| 12 | + pip install dynamsoft-capture-vision-bundle |
| 13 | + ``` |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +1. Set the license key in `test.py`: |
| 18 | + |
| 19 | + ```python |
| 20 | + error_code, error_message = LicenseManager.init_license( |
| 21 | + "LICENSE-KEY") |
| 22 | + ``` |
| 23 | + |
| 24 | +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): |
| 25 | + |
| 26 | + ```json |
| 27 | + "StandardFormat": "BF_CODE_39", |
| 28 | + "HeadModuleRatio": "131111313", |
| 29 | + "TailModuleRatio": "131111313" |
| 30 | + ``` |
| 31 | + |
| 32 | +3. After configuring the template file, use the following code to read the barcode: |
| 33 | + |
| 34 | + ```python |
| 35 | + error_code, error_message = LicenseManager.init_license("LICENSE-KEY") |
| 36 | + if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 37 | + print("License initialization failed: ErrorCode:", |
| 38 | + error_code, ", ErrorString:", error_message) |
| 39 | + else: |
| 40 | + cvr_instance = CaptureVisionRouter() |
| 41 | + |
| 42 | + cvr_instance.init_settings_from_file('template_plus.json') # Or template_minus.json |
| 43 | +
|
| 44 | + result = cvr_instance.capture( |
| 45 | + cv_image, "") # The empty string means using the default template |
| 46 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 47 | + print("Error:", result.get_error_code(), |
| 48 | + result.get_error_string()) |
| 49 | + else: |
| 50 | + |
| 51 | + items = result.get_items() |
| 52 | + print('Found {} barcodes.'.format(len(items))) |
| 53 | + for item in items: |
| 54 | + format_type = item.get_format_string() |
| 55 | + text = item.get_text() |
| 56 | + print("Barcode Format:", format_type) |
| 57 | + print("Barcode Text:", text) |
| 58 | +
|
| 59 | + location = item.get_location() |
| 60 | + x1 = location.points[0].x |
| 61 | + y1 = location.points[0].y |
| 62 | + x2 = location.points[1].x |
| 63 | + y2 = location.points[1].y |
| 64 | + x3 = location.points[2].x |
| 65 | + y3 = location.points[2].y |
| 66 | + x4 = location.points[3].x |
| 67 | + y4 = location.points[3].y |
| 68 | + print("Location Points:") |
| 69 | + print("({}, {})".format(x1, y1)) |
| 70 | + print("({}, {})".format(x2, y2)) |
| 71 | + print("({}, {})".format(x3, y3)) |
| 72 | + print("({}, {})".format(x4, y4)) |
| 73 | + print("-------------------------------------------------") |
| 74 | +
|
| 75 | + pts = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], dtype=np.int32).reshape((-1, 1, 2)) |
| 76 | + cv2.drawContours( |
| 77 | + cv_image, [pts], 0, (0, 255, 0), 2) |
| 78 | +
|
| 79 | + cv2.putText(cv_image, text, (x1, y1 - 10), |
| 80 | + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) |
| 81 | +
|
| 82 | + cv2.imshow( |
| 83 | + "Original Image with Detected Barcodes", cv_image) |
| 84 | + cv2.waitKey(0) |
| 85 | + cv2.destroyAllWindows() |
| 86 | + |
| 87 | + ``` |
| 88 | +
|
| 89 | +4. Run the script: |
| 90 | +
|
| 91 | + ```bash |
| 92 | + python test.py |
| 93 | + ``` |
| 94 | +
|
| 95 | +  |
| 96 | +
|
| 97 | +## Blog |
| 98 | +[How to Read Nonstandard 1D Barcode with Dynamsoft Barcode SDK](https://www.dynamsoft.com/codepool/read-nonstandard-1d-barcode-barcode-sdk.html) |
0 commit comments