C# opencv识别二维码

新建桌面程序

在这里插入图片描述
在这里插入图片描述

安装opencvsharp

在这里插入图片描述

拖拽设计页面

在这里插入图片描述

选择图片识别代码

using OpenCvSharp;
using System.Text;

namespace QRcodeIdentity
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // 设置文件对话框的标题  
            openFileDialog.Title = "选择图片";
            // 设置文件类型过滤器  
            // 注意这里"PNG 图片 (*.png)|*.png|JPG 图片 (*.jpg)|*.jpg"的写法  
            // "|"用于分隔不同的过滤器,每个过滤器由“描述|文件扩展名”组成  
            openFileDialog.Filter = "PNG 图片 (*.png)|*.png|JPG 图片 (*.jpg)|*.jpg";
            // 设置默认文件类型过滤器为第一个(PNG 图片)  
            openFileDialog.FilterIndex = 1;
            // 显示文件对话框  
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.imgPathTxt.Text = openFileDialog.FileName;
            }
        }

        /// <summary>
        /// 识别图片中的二维码位置和内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void runBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.imgPathTxt.Text))
            {
                MessageBox.Show("请选择图片");
                return;
            }
            var src = Cv2.ImRead(this.imgPathTxt.Text);
            //图像灰度
            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();
            /*
             img 为输入图像,灰度或者彩色图像;
            points 是二维码ROI最小外接矩形顶点坐标;
            straightQrcode 输出的是二维码区域ROI图像信息 返回的二维码utf-8字符串
            返回值是已识别的二维码内容
             */
            string code = qRCodeDetector.DetectAndDecode(src, out point2Fs, mat);
            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);
            // 使用Cv2.ImWrite方法保存Mat对象为图片文件  
            var filePath = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.jpg";
            bool result = Cv2.ImWrite(filePath, src);
            StringBuilder str = new StringBuilder();
            str.AppendLine("识别成功\r\n");
            str.Append($"二维码内容为:{code}\r\n");
            var xLeft = (int)point2Fs[3].X;
            var yTop = (int)point2Fs[1].Y;
            var width = (int)point2Fs[2].X - (int)point2Fs[3].X;
            str.Append($"距离顶部{yTop},距离左侧{xLeft},宽度:{width}");
            this.resultTxt.Text = str.ToString();
        }
    }
}

在这里插入图片描述
在这里插入图片描述

参考

https://blog.csdn.net/qq_37835727/article/details/127809933
https://blog.csdn.net/weixin_63357306/article/details/132830563
https://blog.51cto.com/u_16099252/10915538
https://cloud.tencent.com/developer/article/2420183

  • 18
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值