|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Command-line barcode scanner using Dynamsoft Barcode Reader SDK |
| 4 | +""" |
| 5 | +import sys |
| 6 | +import os |
| 7 | +from dynamsoft_barcode_reader_bundle import * |
| 8 | + |
| 9 | + |
| 10 | +def scan_barcode(image_path): |
| 11 | + """ |
| 12 | + Scan barcode from a single image file |
| 13 | + |
| 14 | + Args: |
| 15 | + image_path: Path to the image file containing barcode |
| 16 | + """ |
| 17 | + # Check if file exists |
| 18 | + if not os.path.exists(image_path): |
| 19 | + print(f"Error: File '{image_path}' not found.") |
| 20 | + return |
| 21 | + |
| 22 | + # Initialize license |
| 23 | + # Using public trial license (requires network connection) |
| 24 | + errorCode, errorMsg = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9") |
| 25 | + if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 26 | + print(f"License initialization failed: ErrorCode: {errorCode}, ErrorString: {errorMsg}") |
| 27 | + return |
| 28 | + |
| 29 | + # Create CaptureVisionRouter instance |
| 30 | + cvr_instance = CaptureVisionRouter() |
| 31 | + |
| 32 | + # Capture barcode from image |
| 33 | + print(f"Scanning barcode from: {image_path}") |
| 34 | + result = cvr_instance.capture(image_path, EnumPresetTemplate.PT_READ_BARCODES.value) |
| 35 | + |
| 36 | + # Check for errors |
| 37 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 38 | + print(f"Error: {result.get_error_code()} - {result.get_error_string()}") |
| 39 | + return |
| 40 | + |
| 41 | + # Get barcode results |
| 42 | + barcode_result = result.get_decoded_barcodes_result() |
| 43 | + if barcode_result is None or len(barcode_result.get_items()) == 0: |
| 44 | + print("No barcode detected.") |
| 45 | + return |
| 46 | + |
| 47 | + # Display results |
| 48 | + items = barcode_result.get_items() |
| 49 | + print(f"\n✓ Decoded {len(items)} barcode(s):\n") |
| 50 | + for index, item in enumerate(items): |
| 51 | + print(f"Result {index + 1}:") |
| 52 | + print(f" Format: {item.get_format_string()}") |
| 53 | + print(f" Text: {item.get_text()}") |
| 54 | + print() |
| 55 | + |
| 56 | + |
| 57 | +def scan_directory(directory_path): |
| 58 | + """ |
| 59 | + Scan barcodes from all images in a directory |
| 60 | + |
| 61 | + Args: |
| 62 | + directory_path: Path to directory containing images |
| 63 | + """ |
| 64 | + # Check if directory exists |
| 65 | + if not os.path.isdir(directory_path): |
| 66 | + print(f"Error: Directory '{directory_path}' not found.") |
| 67 | + return |
| 68 | + |
| 69 | + # Initialize license |
| 70 | + errorCode, errorMsg = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9") |
| 71 | + if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 72 | + print(f"License initialization failed: ErrorCode: {errorCode}, ErrorString: {errorMsg}") |
| 73 | + return |
| 74 | + |
| 75 | + # Create CaptureVisionRouter instance |
| 76 | + cvr_instance = CaptureVisionRouter() |
| 77 | + |
| 78 | + # Set up DirectoryFetcher as input |
| 79 | + fetcher = DirectoryFetcher() |
| 80 | + fetcher.set_directory(directory_path) |
| 81 | + cvr_instance.set_input(fetcher) |
| 82 | + |
| 83 | + # Create result receiver |
| 84 | + class MyCapturedResultReceiver(CapturedResultReceiver): |
| 85 | + def __init__(self): |
| 86 | + super().__init__() |
| 87 | + |
| 88 | + def on_decoded_barcodes_received(self, result): |
| 89 | + tag = result.get_original_image_tag() |
| 90 | + if isinstance(tag, FileImageTag): |
| 91 | + print(f"\nFile: {tag.get_file_path()}") |
| 92 | + |
| 93 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 94 | + print(f"Error: {result.get_error_string()}") |
| 95 | + else: |
| 96 | + items = result.get_items() |
| 97 | + if len(items) > 0: |
| 98 | + print(f"Detected {len(items)} barcode(s):") |
| 99 | + for index, item in enumerate(items): |
| 100 | + print(f" Result {index + 1}:") |
| 101 | + print(f" Format: {item.get_format_string()}") |
| 102 | + print(f" Text: {item.get_text()}") |
| 103 | + else: |
| 104 | + print("No barcodes detected.") |
| 105 | + |
| 106 | + # Create image source state listener |
| 107 | + class MyImageSourceStateListener(ImageSourceStateListener): |
| 108 | + def __init__(self, cvr_instance): |
| 109 | + super().__init__() |
| 110 | + self.cvr_instance = cvr_instance |
| 111 | + |
| 112 | + def on_image_source_state_received(self, state): |
| 113 | + if state == EnumImageSourceState.ISS_EXHAUSTED: |
| 114 | + if self.cvr_instance is not None: |
| 115 | + self.cvr_instance.stop_capturing() |
| 116 | + |
| 117 | + # Register listeners |
| 118 | + receiver = MyCapturedResultReceiver() |
| 119 | + cvr_instance.add_result_receiver(receiver) |
| 120 | + listener = MyImageSourceStateListener(cvr_instance) |
| 121 | + cvr_instance.add_image_source_state_listener(listener) |
| 122 | + |
| 123 | + # Start capturing |
| 124 | + print(f"Scanning barcodes from directory: {directory_path}\n") |
| 125 | + errorCode, errorMsg = cvr_instance.start_capturing("", True) |
| 126 | + if errorCode != EnumErrorCode.EC_OK: |
| 127 | + print(f"Error: {errorMsg}") |
| 128 | + else: |
| 129 | + print("\n✓ Directory scan completed.") |
| 130 | + |
| 131 | + |
| 132 | +def print_usage(): |
| 133 | + """Print usage information""" |
| 134 | + print("Command-line Barcode Scanner using Dynamsoft Barcode Reader SDK") |
| 135 | + print("\nUsage:") |
| 136 | + print(" python barcode_scanner.py <image_path> # Scan a single image") |
| 137 | + print(" python barcode_scanner.py --dir <directory> # Scan all images in a directory") |
| 138 | + print("\nExamples:") |
| 139 | + print(" python barcode_scanner.py barcode.jpg") |
| 140 | + print(" python barcode_scanner.py --dir ./images") |
| 141 | + |
| 142 | + |
| 143 | +def main(): |
| 144 | + """Main entry point""" |
| 145 | + if len(sys.argv) < 2: |
| 146 | + print_usage() |
| 147 | + sys.exit(1) |
| 148 | + |
| 149 | + if sys.argv[1] == "--dir": |
| 150 | + if len(sys.argv) < 3: |
| 151 | + print("Error: Directory path required.") |
| 152 | + print_usage() |
| 153 | + sys.exit(1) |
| 154 | + scan_directory(sys.argv[2]) |
| 155 | + elif sys.argv[1] in ["-h", "--help"]: |
| 156 | + print_usage() |
| 157 | + else: |
| 158 | + scan_barcode(sys.argv[1]) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments