c# opencv QR code recognition

1. Test interface

2. Core code

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestProject
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread bath_th = null;
        Mat frame = null;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //videocapture后面参数为0表示获取电脑本机摄像头的内容
                //如果输入rtsp地址就是获取网络摄像头的拍摄内容,rtsp地址可由摄像头厂家提供
                //已大华为例 rtsp://用户名:密码@ip:port
                if (frame != null)
                {
                    MessageBox.Show("正在采集");
                    return;
                }
                 frame =new Mat();
                var capture = new VideoCapture(0);

                int sleeptime = (int)Math.Round(1000 / capture.Fps);
                bath_th = new Thread(
               new ThreadStart(delegate
               {
                  
                   while (true)
                   {
                       try
                       {
                           capture.Read(frame);
                           if (frame.Empty())
                           {
                               break;
                           }
                           Bitmap image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame);
                           this.Invoke((EventHandler)delegate
                                {
                                    if (pictureEdit1.Image != null)
                                    {
                                        pictureEdit1.Image.Dispose();
                                    }
                                    pictureEdit1.Image = image;
                                });
                           Cv2.WaitKey(sleeptime);
                       }
                       catch { }
                   }
               }));
                bath_th.Start();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.simpleButton2.Enabled = false;
            String  qr_code = DetectQRCode(frame);
            if (!qr_code.Equals(string.Empty))
            {
                this.Invoke((EventHandler)delegate {
                    this.textEdit1.Text = qr_code;
                });
                //MessageBox.Show(qr_code);
            }
            Thread.Sleep(1000);
            this.simpleButton2.Enabled = true;
        }
        public string DetectQRCode(Mat src)
        {

            //Mat src = Cv2.ImRead(@"D:\Project\禾川PLC\code.png");
            //图像灰度
            Mat gray = new Mat();
            Cv2.CvtColor(src, gray, ColorConversionCodes.BGRA2GRAY);
            //Cv2.ImShow("gray", gray);
            //二值化
            Mat threshold = new Mat();
            //Cv2.AdaptiveThreshold(gray, threshold, 255.0, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 13, 2);
            Cv2.Threshold(gray, threshold, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            //Cv2.ImShow("threshold", threshold);
            //绘制轮廓

            //截取二维码有效区域
            //识别二维码
            QRCodeDetector qRCodeDetector = new QRCodeDetector();
            Point2f[] point2Fs;
            //Cv2.ImShow("gray", gray);
            Mat mat = new Mat();
            string code = qRCodeDetector.DetectAndDecode(src, out point2Fs, mat);
            //Point[][] contours;
            //HierarchyIndex[] hierarchies;
            //Cv2.FindContours(src, out contours, out hierarchies, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
            //RNG rng = new RNG(12345);
            //for (int i = 0; i < point2Fs.Length; i++)
            //{
            //  Cv2.DrawContours(src, point2Fs, i, new Scalar(rng.Uniform(0, 255), rng.Uniform(0, 255), rng.Uniform(0, 255)), 6, LineTypes.Link4);

            //}
           // Cv2.Rectangle(src, new OpenCvSharp.Point((int)point2Fs[3].X, (int)point2Fs[1].Y), new OpenCvSharp.Point((int)point2Fs[2].X, (int)point2Fs[2].Y), new Scalar(0, 255, 255), 2);
           // Cv2.ImShow("src", src);
            return code;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.textEdit1.Text = "";
        }
    }
}

 Author V: jbossjf

Guess you like

Origin blog.csdn.net/jbossjf/article/details/131262923