- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
[C#] Sử dụng scan đọc qrcode bằng Python trong winform
Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách đọc mã vạch QR code từ camera bằng Python và trả kết quả vào Richtextbox C#
, Winform.
[C#] How to Scan QRcode From Camera Python to Winform
Dưới đây, là giao diện demo ứng dụng của mình:
Ở giao diện trên các bạn, thấy từ app ứng dụng C#, winform mình sẽ gọi ứng dụng đọc qrcode bằng Python lên.
Và sau đó, kết quả scan được trả về như hình bên dưới.
Source code Scan QR Code Python:
import cv2
import numpy as np
from pyzbar.pyzbar import Decoded, decode
import keyboard
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
success, img = cap.read()
for barcode in decode(img):
# print(barcode.data)
myData = barcode.data.decode('utf-8')
print(myData, flush=True)
pts = np.array([barcode.polygon], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], True, (255,0,255), 5)
pts2 = barcode.rect
cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 165,0), 2)
cv2.imshow('Scan QR Code - https://laptrinhvb.net', img)
cv2.waitKey(1)
if keyboard.is_pressed('q'):
break
Các bạn cài đặt 3 thư viên dưới đây để chạy project, trên python bằng cách sử dụng lệnh pip
# using pip install three library below
# pip install opencv-python
# pip install pyzbar
# pip install keyboard
Chi tiết bạn có thể xem video hướng dẫn cài đặt thư viện:
Full source code C# scan qrcode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScanQRcodeWithPython
{
public partial class Form1 : Form
{
private object syncGate = new object();
private Process process;
private StringBuilder output = new StringBuilder();
private bool outputChanged;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
lock (syncGate)
{
if (process != null) return;
}
output.Clear();
outputChanged = false;
richTextBox1.Text = "";
process = new Process();
process.StartInfo.FileName = @"C:\Users\nguye\AppData\Local\Programs\Python\Python39\python.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = Application.StartupPath + @"\scanqrcode.py";
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += OnOutputDataReceived;
process.Exited += OnProcessExited;
process.Start();
process.BeginOutputReadLine();
}
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
lock (syncGate)
{
if (sender != process) return;
output.AppendLine(e.Data);
if (outputChanged) return;
outputChanged = true;
BeginInvoke(new Action(OnOutputChanged));
}
}
private void OnOutputChanged()
{
lock (syncGate)
{
richTextBox1.Text = output.ToString();
outputChanged = false;
}
}
private void OnProcessExited(object sender, EventArgs e)
{
lock (syncGate)
{
if (sender != process) return;
process.Dispose();
process = null;
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://laptrinhvb.net");
}
}
}
Thanks for watching!