NEWS

[C#] Sử dụng scan đọc qrcode bằng Python trong winform

[C#] Sử dụng scan đọc qrcode bằng Python trong winform
Đăng bởi: Thảo Meo - Lượt xem: 6287 20:28:14, 22/10/2021C#   In bài viết

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:

2021-10-22_155533

Ở 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!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Sử dụng scan đọc qrcode bằng Python trong winform
Đăng bởi: Thảo Meo - Lượt xem: 6287 20:28:14, 22/10/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.