簡體   English   中英

如何將條碼掃描器與 python 中的 tkinter GUI 應用程序集成?

[英]How to integrate barcode scanner with tkinter GUI application in python?

當我運行以下代碼時。 相機打開,我們可以讀取條形碼。 我需要的是相機 window 保留在我的 Tkinter GUI 應用程序的一側,而不是彈出。 這是代碼

from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
from datetime import datetime
import imutils
import time
import cv2
import winsound

frequency = 600  # Set Frequency To 2500 Hertz
duration = 800  # Set Duration To 1000 ms == 1 second

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="barcodesData.csv",
                help="path to output CSV file ")
args = vars(ap.parse_args())

print("Starting webcam")

vs = VideoStream(src=0).start()
time.sleep(2.0)
csvWrite = open(args["output"], "w")
found = set()
while True:
    frameData = vs.read()
    frameData = imutils.resize(frameData, width=600)
    barcodes = pyzbar.decode(frameData)
    for barcode in barcodes:
        (x, y, width, height) = barcode.rect
        cv2.rectangle(frameData, (x, y), (x + width, y + height), (0, 0, 255), 2)
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        textData = "{} ({})".format(barcodeData, barcodeType)
        cv2.putText(frameData, textData, (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        if barcodeData not in found:
            csvWrite.write("{},{}\n".format(datetime.today().strftime('%Y-%m-%d'),
                                            barcodeData))
            csvWrite.flush()
            found.add(barcodeData)
            winsound.Beep(frequency, duration)
    cv2.imshow("Barcode Scanner", frameData)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("e"):
        break

# close the output CSV file do a bit of cleanup
print("\nWait while we calculate cost...")
csvWrite.close()
cv2.destroyAllWindows()
vs.stop()

time.sleep(1.0)

再具體一點。 我正在制作一個計費軟件,我可以在其中讀取產品的條形碼並進行計費。 相機單獨的屏幕很煩人,所以如果相機一直在 canvas 的任何一側。 它會更快。

我現在才解決這個問題....我是GODZONE。

我所做的是在我的數據庫中將每個產品/項目的 ID 編碼到一個二維碼中,當需要特定項目時,我使用 CV2 來檢測和解碼二維碼

以下是代碼:

定義編碼_qr():

import qrcode

import random as r

item_code = r.randint(00000000,99999999)

data = item_code
qrfile = "qr_image_name_for_specified_item.png"
# generate qrcode
qrimage = qrcode.make(data)
# save image
fl = qrimage.save(qrfile)
print("Done generating qrcode      |>  GODZONE  <|")

def decode_qr():

import cv2
filename="qr_image_name_for_specified_item.png" 
# alternatively webcam cv2.VideoCapture(0)
# read image 
image = cv2.imread(filename)

`image = cv2.imread(文件名)

# initialize qrcode detector`  



detector = cv2.QRCodeDetector()
# detect and decode
info, v_array, binary_qrcode=detector.detectAndDecode(image)
# if null?
if v_array is None:
    print("No qrcode detected or probably some technical issues occurred")
else:
    print("QRCODE data      |>  GODZONE  <|")
    print(info)
    # below i am working with the import sqlite3 as sql3
    sqldb = sql3.connect("your_database.db")
    cur = sqldb.cursor()
    cur.execute("select * from table where ID=?, (info,))
    rows = cur.fetchall()
    for r in rows:
        print(r) # this will loop all the item details with the itemcode
       print(r[1]) # A SPECIFIC DETAIL WITH THE item_code

print("Godzone 說:我希望你覺得這些信息有用.. 我解決了與這個問題有關的問題。它對我有用!>> |> GODZONE <|")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM