|
| 1 | +# -*-coding:utf-8-*- |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import json |
| 6 | +import time |
| 7 | +import glob |
| 8 | +from dbr import * |
| 9 | + |
| 10 | +reader = BarcodeReader() |
| 11 | + |
| 12 | +Barcode_Format_Dict = { |
| 13 | + '1': (EnumBarcodeFormat.BF_ALL, EnumBarcodeFormat_2.BF2_POSTALCODE | EnumBarcodeFormat_2.BF2_DOTCODE), |
| 14 | + '2': (EnumBarcodeFormat.BF_ONED, EnumBarcodeFormat_2.BF2_NULL), |
| 15 | + '3': (EnumBarcodeFormat.BF_QR_CODE, EnumBarcodeFormat_2.BF2_NULL), |
| 16 | + '4': (EnumBarcodeFormat.BF_CODE_39, EnumBarcodeFormat_2.BF2_NULL), |
| 17 | + '5': (EnumBarcodeFormat.BF_CODE_128, EnumBarcodeFormat_2.BF2_NULL), |
| 18 | + '6': (EnumBarcodeFormat.BF_CODE_93, EnumBarcodeFormat_2.BF2_NULL), |
| 19 | + '7': (EnumBarcodeFormat.BF_CODABAR, EnumBarcodeFormat_2.BF2_NULL), |
| 20 | + '8': (EnumBarcodeFormat.BF_ITF, EnumBarcodeFormat_2.BF2_NULL), |
| 21 | + '9': (EnumBarcodeFormat.BF_INDUSTRIAL_25, EnumBarcodeFormat_2.BF2_NULL), |
| 22 | + '10': (EnumBarcodeFormat.BF_EAN_13, EnumBarcodeFormat_2.BF2_NULL), |
| 23 | + '11': (EnumBarcodeFormat.BF_EAN_8, EnumBarcodeFormat_2.BF2_NULL), |
| 24 | + '12': (EnumBarcodeFormat.BF_UPC_A, EnumBarcodeFormat_2.BF2_NULL), |
| 25 | + '13': (EnumBarcodeFormat.BF_UPC_E, EnumBarcodeFormat_2.BF2_NULL), |
| 26 | + '14': (EnumBarcodeFormat.BF_PDF417, EnumBarcodeFormat_2.BF2_NULL), |
| 27 | + '15': (EnumBarcodeFormat.BF_DATAMATRIX, EnumBarcodeFormat_2.BF2_NULL), |
| 28 | + '16': (EnumBarcodeFormat.BF_AZTEC, EnumBarcodeFormat_2.BF2_NULL), |
| 29 | + '17': (EnumBarcodeFormat.BF_CODE_39_EXTENDED, EnumBarcodeFormat_2.BF2_NULL), |
| 30 | + '18': (EnumBarcodeFormat.BF_MAXICODE, EnumBarcodeFormat_2.BF2_NULL), |
| 31 | + '19': (EnumBarcodeFormat.BF_GS1_DATABAR, EnumBarcodeFormat_2.BF2_NULL), |
| 32 | + '20': (EnumBarcodeFormat.BF_PATCHCODE, EnumBarcodeFormat_2.BF2_NULL), |
| 33 | + '21': (EnumBarcodeFormat.BF_GS1_COMPOSITE, EnumBarcodeFormat_2.BF2_NULL), |
| 34 | + '22': (EnumBarcodeFormat.BF_NULL, EnumBarcodeFormat_2.BF2_POSTALCODE), |
| 35 | + '23': (EnumBarcodeFormat.BF_NULL, EnumBarcodeFormat_2.BF2_DOTCODE) |
| 36 | +} |
| 37 | + |
| 38 | +Template_Settings = { |
| 39 | + '1': '{"ImageParameter":{' |
| 40 | + '"Name":"BestCoverage",' |
| 41 | + '"DeblurLevel":9,' |
| 42 | + '"ExpectedBarcodesCount":512,' |
| 43 | + '"ScaleDownThreshold":100000,' |
| 44 | + '"LocalizationModes":[' |
| 45 | + '{"Mode":"LM_CONNECTED_BLOCKS"},' |
| 46 | + '{"Mode":"LM_SCAN_DIRECTLY"},' |
| 47 | + '{"Mode":"LM_STATISTICS"},' |
| 48 | + '{"Mode":"LM_LINES"},' |
| 49 | + '{"Mode":"LM_STATISTICS_MARKS"}],' |
| 50 | + '"GrayscaleTransformationModes":[' |
| 51 | + '{"Mode":"GTM_ORIGINAL"},' |
| 52 | + '{"Mode":"GTM_INVERTED"}]' |
| 53 | + '}' |
| 54 | + '}', |
| 55 | + '2': '{"ImageParameter":{' |
| 56 | + '"Name":"BestSpeed",' |
| 57 | + '"DeblurLevel":3,' |
| 58 | + '"ExpectedBarcodesCount":512,' |
| 59 | + '"LocalizationModes":[' |
| 60 | + '{"Mode":"LM_SCAN_DIRECTLY"}],' |
| 61 | + '"TextFilterModes":[' |
| 62 | + '{"MinImageDimension":262144,"Mode":"TFM_GENERAL_CONTOUR"}]' |
| 63 | + '}' |
| 64 | + '}', |
| 65 | + '3': '{"ImageParameter":{' |
| 66 | + '"Name":"Balance",' |
| 67 | + '"DeblurLevel":5,' |
| 68 | + '"ExpectedBarcodesCount":512,' |
| 69 | + '"LocalizationModes":[' |
| 70 | + '{"Mode":"LM_CONNECTED_BLOCKS"},' |
| 71 | + '{"Mode":"LM_STATISTICS"}]' |
| 72 | + '}' |
| 73 | + '}' |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +def init_runtime_settings(): |
| 78 | + while True: |
| 79 | + print() |
| 80 | + print("Step 2: Choose a template settings : ") |
| 81 | + print("\t 1: Best Coverage Settings") |
| 82 | + print("\t 2: Best Speed Settings") |
| 83 | + print("\t 3: Balance Settings") |
| 84 | + item = input() |
| 85 | + |
| 86 | + if str(item) == 'q' or str(item) == 'Q': |
| 87 | + print('Bye, looking forward to your next use.') |
| 88 | + exit() |
| 89 | + |
| 90 | + if str(item) not in Template_Settings.keys(): |
| 91 | + print('Please choose a valid number.') |
| 92 | + continue |
| 93 | + else: |
| 94 | + reader.init_runtime_settings_with_string(Template_Settings[item]) |
| 95 | + break |
| 96 | + |
| 97 | + |
| 98 | +def set_barcode_format(): |
| 99 | + while True: |
| 100 | + print() |
| 101 | + print("Step 3: Choose a number for the format(s) of your barcode image: ") |
| 102 | + print("\t 1: All") |
| 103 | + print("\t 2: OneD") |
| 104 | + print("\t 3: QR Code") |
| 105 | + print("\t 4: Code 39") |
| 106 | + print("\t 5: Code 128") |
| 107 | + print("\t 6: Code 93") |
| 108 | + print("\t 7: Codabar") |
| 109 | + print("\t 8: Interleaved 2 of 5") |
| 110 | + print("\t 9: Industrial 2 of 5") |
| 111 | + print("\t 10: EAN-13") |
| 112 | + print("\t 11: EAN-8") |
| 113 | + print("\t 12: UPC-A") |
| 114 | + print("\t 13: UPC-E") |
| 115 | + print("\t 14: PDF417") |
| 116 | + print("\t 15: DATAMATRIX") |
| 117 | + print("\t 16: AZTEC") |
| 118 | + print("\t 17: Code 39 Extended") |
| 119 | + print("\t 18: Maxicode") |
| 120 | + print("\t 19: GS1 Databar") |
| 121 | + print("\t 20: PatchCode") |
| 122 | + print("\t 21: GS1 Composite") |
| 123 | + print("\t 22: Postal Code") |
| 124 | + print("\t 23: DotCode") |
| 125 | + item = input() |
| 126 | + |
| 127 | + if str(item) == 'q' or str(item) == 'Q': |
| 128 | + print('Bye, looking forward to your next use.') |
| 129 | + exit() |
| 130 | + |
| 131 | + if str(item) not in Barcode_Format_Dict.keys(): |
| 132 | + print('Please choose a valid number.') |
| 133 | + continue |
| 134 | + else: |
| 135 | + settings = reader.get_runtime_settings() |
| 136 | + settings.barcode_format_ids = Barcode_Format_Dict[item][0] |
| 137 | + settings.barcode_format_ids_2 = Barcode_Format_Dict[item][1] |
| 138 | + reader.update_runtime_settings(settings) |
| 139 | + break |
| 140 | + |
| 141 | + |
| 142 | +def decode_file(path): |
| 143 | + try: |
| 144 | + start_time = time.time() |
| 145 | + text_results = reader.decode_file(path) |
| 146 | + end_time = time.time() |
| 147 | + |
| 148 | + spend_time = (end_time - start_time)*1000 |
| 149 | + |
| 150 | + if text_results is not None: |
| 151 | + print('Total barcode(s) found : {0}. Total time spent: {1} ms.'.format(len(text_results), spend_time)) |
| 152 | + i = 1 |
| 153 | + for text_result in text_results: |
| 154 | + print("-------------") |
| 155 | + print('Barcode {0}'.format(i)) |
| 156 | + print("Barcode Format :") |
| 157 | + print(text_result.barcode_format_string) |
| 158 | + print("Barcode Text :") |
| 159 | + print(text_result.barcode_text) |
| 160 | + print("Barcode Bytes :") |
| 161 | + print(text_result.barcode_bytes) |
| 162 | + print("Localization Points : ") |
| 163 | + print(text_result.localization_result.localization_points) |
| 164 | + print("-------------") |
| 165 | + i = i + 1 |
| 166 | + else: |
| 167 | + print("-------------") |
| 168 | + print("No barcode found. Total time spent: {0} ms.".format(spend_time)) |
| 169 | + print("-------------") |
| 170 | + except BarcodeReaderError as bre: |
| 171 | + print(bre) |
| 172 | + |
| 173 | + |
| 174 | +def decode_files(path): |
| 175 | + for idx, img in enumerate(glob.glob(os.path.join(path, "*.*"))): |
| 176 | + print('Image', idx + 1) |
| 177 | + print(img) |
| 178 | + print(40 * '#') |
| 179 | + decode_file(img) |
| 180 | + print(40 * '#') |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == '__main__': |
| 184 | + |
| 185 | + license_key = "Input your own license" |
| 186 | + reader.init_license(license_key) |
| 187 | + # reader.init_license_from_server(license_server, license_key) |
| 188 | + # license_content = reader.output_license_to_string() |
| 189 | + # reader.init_license_from_license_content(license_key, license_content) |
| 190 | + |
| 191 | + print("*************************************************") |
| 192 | + print("Welcome to Dynamsoft Barcode Reader Demo") |
| 193 | + print("*************************************************") |
| 194 | + print("Hints: Please input 'Q'or 'q' to quit the application.") |
| 195 | + print() |
| 196 | + while True: |
| 197 | + path = input("Step 1: Input your image path or folder path:") |
| 198 | + if path == 'q' or path == 'Q': |
| 199 | + print('Bye, looking forward to your next use.') |
| 200 | + exit() |
| 201 | + if not os.path.exists(path): |
| 202 | + print("The picture or folder path doesn't exist , please input a valid path.") |
| 203 | + continue |
| 204 | + |
| 205 | + init_runtime_settings() |
| 206 | + |
| 207 | + set_barcode_format() |
| 208 | + |
| 209 | + if os.path.isdir(path): |
| 210 | + decode_files(path) |
| 211 | + elif os.path.isfile(path): |
| 212 | + decode_file(path) |
| 213 | + else: |
| 214 | + print("The path is invalid , please input a valid path.") |
| 215 | + continue |
0 commit comments