gpt4 book ai didi

python - 字符串和整数之间的转换。 Python

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:57 24 4
gpt4 key购买 nike

我正在构建一个条形码扫描仪。扫描仪按预期工作。然后我决定将该值更改为 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: unsupported operand types(s) for +: 'int' and '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()

最佳答案

更改此行(您的行中的问题是 ord return 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'

附注不要忘记将代码中的 key 转换为 int

关于python - 字符串和整数之间的转换。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60201418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com