如何在python中将条形码扫描仪与tkinter GUI应用程序集成?

2024-05-19 02:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码时。摄像头打开,我们可以读取条形码。我需要的是相机窗口保持在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)

具体来说。我正在制作一个计费软件,在那里我可以读取产品的条形码并制作账单。相机单独的屏幕很烦人,所以如果相机一直在画布的任何一侧。那会更快


Tags: fromimportoutputdatetimetimeargscv2barcode

热门问题