首先在文件选择对话框,选择二维码文件。代码:
def _on_select_qrcode_btn_click(self):
file_name = filedialog.askopenfilename()
if len(file_name) == 0:
return
self.selected_qr_image = Image.open(file_name)
self.selected_qr_photoImage = ImageTk.PhotoImage(self.selected_qr_image)
self.selected_qr_image_label.config(image=self.selected_qr_photoImage)
self.convert_qrcode_btn.config(state=tk.NORMAL)
然后点击按钮开始解析二维码。代码:
decode_list = pyzbar.decode(self.selected_qr_image)
解析出来的数据为list对象。如果图片上有多个二维码,则图像与list无法对应。所以,按照二维码从左到右的图像位置排序。
decode_list_sorted = sorted(decode_list, key=lambda decode: decode.rect.left)
把解析出来的数据显示在UI上。代码:
def _on_convert_qrcode_btn_click(self):
self.output_qr_info_text.delete(1.0, tk.END)
decode_list = pyzbar.decode(self.selected_qr_image)
decode_list_sorted = sorted(decode_list, key=lambda decode: decode.rect.left)
for i in range(len(decode_list_sorted)):
self.output_qr_info_text.insert("end", "#" + str(i+1)+"\n")
self.output_qr_info_text.insert("end", decode_list_sorted[i].data.decode())
self.output_qr_info_text.insert("end", "\n")