如何建立適用於 Windows、Linux 和 macOS 的 Python 條碼掃描器
條碼掃描已成為從零售、物流到醫療保健等各行業的必備工具。在桌面平台上,它可以快速捕獲和處理訊息,無需手動輸入數據,從而節省時間並減少錯誤。在本教學中,我們將透過建置適用於Windows、Linux 的Python 條碼掃描器,繼續探索Dynamsoft Capture Vision SDK的功能和macOS。
macOS 上的 Python 條碼掃描器演示
先決條件
Dynamsoft Capture Vision 試用授權:取得 Dynamsoft Capture Vision SDK 的 30 天試用授權金鑰。
-
Python 套件:使用以下指令安裝所需的 Python 套件:
pip install dynamsoft-capture-vision-bundle opencv-python
登入後複製登入後複製這些包有什麼用?
- dynamsoft-capture-vision-bundle 是一個適用於 Python 的 Dynamsoft Capture Vision SDK。
- opencv-python 會擷取相機幀並顯示處理後的影像結果。
從靜態影像中讀取條碼
由於 Dynamsoft Capture Vision SDK 是一個整合了各種映像處理任務的統一框架,因此我們可以透過將 PresetTemplate 名稱傳遞給 capture() 方法來輕鬆切換映像處理模式。
Dynamsoft Capture Vision SDK 內建模板
以下程式碼片段顯示了 Dynamsoft Capture Vision SDK 中的內建 PresetTemplate 枚舉:
class EnumPresetTemplate(Enum): PT_DEFAULT = _DynamsoftCaptureVisionRouter.getPT_DEFAULT() PT_READ_BARCODES = _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES() PT_RECOGNIZE_TEXT_LINES = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_TEXT_LINES() PT_DETECT_DOCUMENT_BOUNDARIES = ( _DynamsoftCaptureVisionRouter.getPT_DETECT_DOCUMENT_BOUNDARIES() ) PT_DETECT_AND_NORMALIZE_DOCUMENT = ( _DynamsoftCaptureVisionRouter.getPT_DETECT_AND_NORMALIZE_DOCUMENT() ) PT_NORMALIZE_DOCUMENT = _DynamsoftCaptureVisionRouter.getPT_NORMALIZE_DOCUMENT() PT_READ_BARCODES_SPEED_FIRST = ( _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_SPEED_FIRST() ) PT_READ_BARCODES_READ_RATE_FIRST = ( _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_READ_RATE_FIRST() ) PT_READ_SINGLE_BARCODE = _DynamsoftCaptureVisionRouter.getPT_READ_SINGLE_BARCODE() PT_RECOGNIZE_NUMBERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS() PT_RECOGNIZE_LETTERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_LETTERS() PT_RECOGNIZE_NUMBERS_AND_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_LETTERS() ) PT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS() ) PT_RECOGNIZE_UPPERCASE_LETTERS = ( _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_UPPERCASE_LETTERS() )
PT_DEFAULT 範本支援多種任務,包括文件偵測、機讀區辨識和條碼偵測。若要專門最佳化條碼偵測的效能,請將範本設定為 EnumPresetTemplate.PT_READ_BARCODES.value。
用於條碼檢測的Python程式碼
參考先前的文件偵測與機讀區辨識範例,可以使用以下程式碼從靜態影像讀取條碼:
import sys from dynamsoft_capture_vision_bundle import * import os import cv2 import numpy as np from utils import * if __name__ == '__main__': print("**********************************************************") print("Welcome to Dynamsoft Capture Vision - Barcode Sample") print("**********************************************************") error_code, error_message = LicenseManager.init_license( "LICENSE-KEY") if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED: print("License initialization failed: ErrorCode:", error_code, ", ErrorString:", error_message) else: cvr_instance = CaptureVisionRouter() while (True): image_path = input( ">> Input your image full path:\n" ">> 'Enter' for sample image or 'Q'/'q' to quit\n" ).strip('\'"') if image_path.lower() == "q": sys.exit(0) if image_path == "": image_path = "../../../images/multi.png" if not os.path.exists(image_path): print("The image path does not exist.") continue result = cvr_instance.capture( image_path, EnumPresetTemplate.PT_READ_BARCODES.value) if result.get_error_code() != EnumErrorCode.EC_OK: print("Error:", result.get_error_code(), result.get_error_string()) else: cv_image = cv2.imread(image_path) items = result.get_items() print('Found {} barcodes.'.format(len(items))) for item in items: format_type = item.get_format() text = item.get_text() print("Barcode Format:", format_type) print("Barcode Text:", text) location = item.get_location() x1 = location.points[0].x y1 = location.points[0].y x2 = location.points[1].x y2 = location.points[1].y x3 = location.points[2].x y3 = location.points[2].y x4 = location.points[3].x y4 = location.points[3].y del location cv2.drawContours( cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) cv2.putText(cv_image, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow( "Original Image with Detected Barcodes", cv_image) cv2.waitKey(0) cv2.destroyAllWindows() input("Press Enter to quit...")
注意:將 LICENSE-KEY 替換為您的有效許可證金鑰。
使用多條碼影像測試 Python 條碼閱讀器
從單一影像解碼多個條碼是零售和物流中的常見用例。下圖包含多個不同格式的條碼:
使用網路攝影機進行即時多條碼偵測
當從映像檔讀取條碼時,我們在主執行緒中呼叫 capture() 方法。然而,為了處理來自網路攝影機的即時視訊串流,需要採用不同的方法來避免阻塞主線程。 Dynamsoft Capture Vision SDK 提供了一種內建機制,用於處理即時視訊幀並在本機 C 工作執行緒上非同步處理它們。若要實現此目的,請擴充 ImageSourceAdapter 和 CapturedResultReceiver 類別來分別處理影像資料和擷取的結果,然後呼叫 start_capturing() 方法開始處理視訊串流。
pip install dynamsoft-capture-vision-bundle opencv-python
說明
- FrameFetcher 類別實作 ImageSourceAdapter 接口,將幀資料送入內建緩衝區。
- MyCapturedResultReceiver 類別實作了 CapturedResultReceiver 介面。 on_captured_result_received 方法在本機 C 工作執行緒上執行,並將 CapturedResult 物件傳送至主執行緒,並將它們儲存在執行緒安全性佇列中以供進一步使用。
- CapturedResult 包含多個 CapturedResultItem 物件。 CRIT_BARCODE 類型表示已識別的條碼資料。
在 macOS 上測試 Python 條碼掃描儀
原始碼
https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/10.x
以上是如何建立適用於 Windows、Linux 和 macOS 的 Python 條碼掃描器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
