|
| 1 | +import os |
| 2 | +import json |
| 3 | +from dbr import DynamsoftBarcodeReader |
| 4 | +dbr = DynamsoftBarcodeReader() |
| 5 | +# print(dir(dbr)) |
| 6 | + |
| 7 | +import cv2 |
| 8 | + |
| 9 | +import sys |
| 10 | +sys.path.append('../') |
| 11 | +# import config |
| 12 | + |
| 13 | +def InitLicense(license): |
| 14 | + dbr.InitLicense(license) |
| 15 | + |
| 16 | +def DecodeFile(fileName, templateName = ""): |
| 17 | + try: |
| 18 | + |
| 19 | + # results is a dictionary object which includes TextResults and IntermediateResults. |
| 20 | + results = dbr.DecodeFile(fileName, templateName) |
| 21 | + # textResults is a list object, the following program will output each whole text result. |
| 22 | + # if you just want some individual results in textResult, you can get all keys in text result and get the value by the key. |
| 23 | + textResults = results["TextResults"] |
| 24 | + intermediateResults = results["IntermediateResults"] |
| 25 | + for textResult in textResults: |
| 26 | + # print(textResult["BarcodeFormat"]) |
| 27 | + print(textResult["BarcodeFormatString"]) |
| 28 | + print(textResult["BarcodeText"]) |
| 29 | + # print(textResult["BarcodeBytes"]) |
| 30 | + # # LocalizationResult is a dictionary object, you can use the same method as textResult to get the key-value. |
| 31 | + # print(textResult["LocalizationResult"]) |
| 32 | + # # DetailedResult is a dictionary object, you can use the same method as textResult to get the key-value. |
| 33 | + # print(textResult["DetailedResult"]) |
| 34 | + # # ExtendedResults is a list object , and each item of it is a dictionary object. |
| 35 | + # extendedResults = textResult["ExtendedResults"] |
| 36 | + # for extendedResult in extendedResults: |
| 37 | + # print(extendedResult) |
| 38 | + # intermediateResults is a list object, the following program will output each whole intermediate result. |
| 39 | + # if you just want some individual results in intermediateResult, you can get all keys in intermediateResult and get the value by the key. |
| 40 | + # for intermediateResult in intermediateResults: |
| 41 | + # print(intermediateResult) |
| 42 | + # print(intermediateResult.keys()) |
| 43 | + except Exception as err: |
| 44 | + print(err) |
| 45 | + |
| 46 | +def DecodeBuffer(image, templateName = ""): |
| 47 | + results = dbr.DecodeBuffer(image, image.shape[0], image.shape[1], image.strides[0], dbr.IPF_RGB_888, templateName) |
| 48 | + textResults = results["TextResults"] |
| 49 | + |
| 50 | + for textResult in textResults: |
| 51 | + print("barcode format: " + textResult["BarcodeFormatString"]) |
| 52 | + print("barcode text: " + textResult["BarcodeText"]) |
| 53 | + localizationResult = textResult["LocalizationResult"] |
| 54 | + x1 = localizationResult["X1"] |
| 55 | + y1 = localizationResult["Y1"] |
| 56 | + x2 = localizationResult["X2"] |
| 57 | + y2 = localizationResult["Y2"] |
| 58 | + x3 = localizationResult["X3"] |
| 59 | + y3 = localizationResult["Y3"] |
| 60 | + x4 = localizationResult["X4"] |
| 61 | + y4 = localizationResult["Y4"] |
| 62 | + localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)] |
| 63 | + print(localizationPoints) |
| 64 | + |
| 65 | + # cv2.waitKey(0) |
| 66 | + |
| 67 | +def DecodeFileStream(imagePath, templateName = ""): |
| 68 | + with open(imagePath, "rb") as fread: |
| 69 | + total = fread.read() |
| 70 | + results = dbr.DecodeFileStream(bytearray(total), len(total)) |
| 71 | + textResults = results["TextResults"] |
| 72 | + for textResult in textResults: |
| 73 | + print("barcode format: " + textResult["BarcodeFormatString"]) |
| 74 | + print("barcode text: " + textResult["BarcodeText"]) |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + print("OpenCV version: " + cv2.__version__) |
| 78 | + import sys |
| 79 | + barcode_image = "" |
| 80 | + if sys.version_info < (3, 0): |
| 81 | + barcode_image = raw_input("Enter the barcode file: ") |
| 82 | + else: |
| 83 | + barcode_image = input("Enter the barcode file: ") |
| 84 | + |
| 85 | + if not os.path.isfile(barcode_image): |
| 86 | + print("It is not a valid file.") |
| 87 | + else: |
| 88 | + InitLicense("Input your license") |
| 89 | + # Get default barcode params |
| 90 | + params = dbr.GetRuntimeSettings() |
| 91 | + params["BarcodeFormatIds"] = dbr.BF_ONED | dbr.BF_QR_CODE |
| 92 | + # Set parameters |
| 93 | + ret = dbr.UpdataRuntimeSettings(params) |
| 94 | + |
| 95 | + DecodeFile(barcode_image) |
| 96 | + image = cv2.imread(barcode_image) |
| 97 | + DecodeBuffer(image) |
| 98 | + print("Over") |
0 commit comments