【问题标题】:How to integrate barcode scanner with tkinter GUI application in python?如何在 python 中将条码扫描器与 tkinter GUI 应用程序集成?
【发布时间】:2021-02-10 20:01:39
【问题描述】:

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

具体一点。我正在制作一个计费软件,我可以在其中读取产品的条形码并进行计费。相机单独的屏幕很烦人,所以如果相机一直在画布的任何一侧。这样会更快。

【问题讨论】:

  • 所以,你已经解释了你想要什么,然后提供了很多代码,但是你没有解释你遇到了什么问题。看来您还没有在 TKinter 中实现任何东西。您是否被困在其中的某个特定部分?如果是这样,您应该详细说明。如果您只是要求我们告诉或向您展示如何从头开始在 TKinter 中完成这一切,我认为这个问题有点过于宽泛了。
  • 您需要将OpenCV 图像转换为Pillow(或PIL)图像并使用tkinter Label 而不是cv2.imshow() 显示图像。搜索 SO,你可以找到几个例子。

标签: python user-interface tkinter barcode-scanner


【解决方案1】:

我将数据库中每个产品/项目的 ID 编码为 QR 码。当正在寻找该特定项目时,我使用 CV2 来检测和解码 QR 码。

代码如下:

def encode_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")


def decode_qr():

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

    # 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")
        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

【讨论】:

  • 请阅读How to Answer。对我们大喊大叫是不合适的。反复称自己为“Godzone”是不恰当的。请正确格式化您的代码并清楚地解释它如何回答问题。与解释如何修改问题中的代码相比,全新代码的用处要小得多。大喊大叫你有多棒是被停职的好方法。
  • 此代码对提问的用户没有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多