我正在构建条形码扫描仪。扫描仪工作正常。然后,我决定将值更改为ascii 5偏移量,以提供额外的数据保护。这也按预期工作。为了进一步提高安全性,我希望添加用户输入的密码。我在添加密码之前的原始代码如下。...
barcodeData = barcode.data.decode("ascii")
barcodeData = "".join(chr(ord(c) +5) for c in barcodeData
然后我决定在第一行中添加用户输入
userkey = input()
key=float(userkey)
然后替换
barcodeData = "".join(chr(ord(c) +5) for c in barcodeData
与
barcodeData = "".join(chr(ord(c) +'key') for c in barcodeData
这会引发错误
TypeError:+不支持的操作数类型:“ int”和“ str”
我希望系统在所有输入上运行,但仅在用户输入数字5
时显示正确的输出
预先感谢
# import the necessary packages
from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
import imutils
import time
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="barcodes.csv",
help="path to output CSV file containing barcodes")
args = vars(ap.parse_args())
#start video stream and allow warming of camera
print("While camera is warming up, please enter the numerical password.")
vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
userkey=input()
key=str(userkey)
# open the output CSV file for writing and initialize the set of
# QR barcodes found thus far
csv = open(args["output"], "w")
found = set()
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it to
# have a maximum width of 600 pixels
frame = vs.read()
frame = imutils.resize(frame, width=600)
# find the QR Codes in the frame and decode each of the barcodes
barcodes = pyzbar.decode(frame)
# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw
# the bounding box surrounding the barcode on the image
(x, y, w, h) = barcode.rect
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3)
# the barcode data is a bytes object so if we want to draw it
# on our output image we need to convert it to a string first
barcodeData = barcode.data.decode("ascii")
#Chnage the decoded ascii string by a value of 5 charcters
barcodeData = "".join(chr(ord(c) + 'key') for c in barcodeData)
# draw the barcode data and barcode type on the image
text = "{}".format(barcodeData)
cv2.putText(frame, text, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# if the barcode text is currently not in our CSV file, write
# the timestamp + barcode to disk and update the set
if barcodeData not in found:
csv.write("{},{}\n".format(datetime.datetime.now(),
barcodeData))
csv.flush()
found.add(barcodeData)
# show the output frame
cv2.imshow("QR Code Secret Message Scanner", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# close the output CSV file nad perform cleanup
csv.close()
cv2.destroyAllWindows()
vs.stop()
答案 0 :(得分:0)
v1 v2 v3 v4 v5
0 a ab abc abcd abcde
1 abcd abc ab NaN abcd
2 a abc ac cde abc
3 cde c NaN cd cde
返回的不是字符串,而是int的数据类型,如果要转换它,则必须通过以下操作将其类型转换为字符串:
key
同样,要转换为int的字符串是:
key = str(userkey)
答案 1 :(得分:0)
更改此行(您的行中ord返回int和key具有单引号的问题,我认为这是一个错误)
barcodeData = "".join(chr(ord(c) + 'key') for c in barcodeData)
到下一个:
barcodeData = "".join(chr(ord(c) + key) for c in barcodeData)
一个例子:
key = 5 # your offset
barcode = 'AB1234VD'
barcode = "".join(chr(ord(c) + key) for c in barcode)
print(barcode) # 'FG6789[I'
P.S。不要忘记将代码中的key
转换为int