-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgeneral_settings.py
More file actions
82 lines (72 loc) · 4.46 KB
/
general_settings.py
File metadata and controls
82 lines (72 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from dynamsoft_barcode_reader_bundle import *
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
if __name__ == "__main__":
try:
# 1. Initialize license.
# The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work.
# You can also request a 30-day trial license in the customer portal: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=python
err_code, err_str = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9")
if err_code != EnumErrorCode.EC_OK and err_code != EnumErrorCode.EC_LICENSE_WARNING:
print("License initialization failed: ErrorCode:", err_code, ", ErrorString:", err_str)
else:
# 2. Create an instance of CaptureVisionRouter.
cvr_instance = CaptureVisionRouter()
# 3. General settings (including barcode format, barcode count and scan region) through SimplifiedCaptureVisionSettings
# 3.1 Obtain current runtime settings of instance.
err_code, err_str, settings = cvr_instance.get_simplified_settings(EnumPresetTemplate.PT_READ_BARCODES)
# 3.2 Set the expected barcode format you want to read.
settings.barcode_settings.barcode_format_ids = EnumBarcodeFormat.BF_PDF417 | EnumBarcodeFormat.BF_QR_CODE
# 3.3 Set the expected barcode count you want to read.
settings.barcode_settings.expected_barcodes_count = 10
# 3.4 Set the grayscale transformation modes.
settings.barcode_settings.grayscale_transformation_modes[0] = EnumGrayscaleTransformationMode.GTM_AUTO
# 3.5 Set the ROI(region of interest) to speed up the barcode reading process.
# Note: DBR supports setting coordinates by pixels or percentages. The origin of the coordinate system is the upper left corner point.
settings.roi_measured_in_percentage = 1
points = settings.roi.points
points[0].x = 0
points[0].y = 0
points[1].x = 100
points[1].y = 0
points[2].x = 100
points[2].y = 100
points[3].x = 0
points[3].y = 100
# 3.6 Apply the new settings to the instance.
err_code, err_str = cvr_instance.update_settings(EnumPresetTemplate.PT_READ_BARCODES, settings)
if err_code != EnumErrorCode.EC_OK:
print("Update settings failed: ErrorCode:", err_code, ", ErrorString:", err_str)
# 4.Replace by your own image path
image_path = str(BASE_DIR.parent / "Images" / "GeneralBarcodes.png")
# 5.Decode barcodes from an image file.
result_array = cvr_instance.capture_multi_pages(image_path, EnumPresetTemplate.PT_READ_BARCODES)
# 6.Output the barcode text.
results = result_array.get_results()
if results is None or len(results) == 0:
print("No barcode detected.")
else:
for i, result in enumerate(results):
page_number = i + 1
tag = result.get_original_image_tag()
if isinstance(tag, FileImageTag):
page_number = tag.get_page_number() + 1
if result.get_error_code() == EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING:
print("Warning:", result.get_error_code(), result.get_error_string())
elif result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_code(), result.get_error_string())
barcode_result = result.get_decoded_barcodes_result()
if barcode_result is None or barcode_result.get_items() == 0:
print("No barcode found in page", page_number)
else:
items = barcode_result.get_items()
print("Page", page_number, "decoded", len(items), "barcodes.")
for index,item in enumerate(items):
print()
print("Result", str(page_number) + "-" + str(index + 1))
print("Barcode Format:", item.get_format_string())
print("Barcode Text:", item.get_text())
print()
except Exception as e:
print(e)