介绍
微信开源了其二维码的解码功能,并贡献给 OpenCV 社区。其开源的 wechat_qrcode 项目被收录到 OpenCV contrib 项目中。从 OpenCV 4.5.2 版本开始,就可以直接使用。
该项目 github 地址:
https://github.com/opencv/opencv_contrib/tree/master/modules/wechat_qrcode
模型文件的地址:
https://github.com/WeChatCV/opencv_3rdparty
微信的扫码引擎,很早就支持了远距离二维码检测、自动调焦定位、多码检测识别等功能,它是基于 CNN 的二维码检测。
OpenCvSharp在 4.6.0.20220608 版本也加入了支持
效果:
?
测试环境:
vs2019
net framework4.7.2
opencvsharp4.8.0
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FIRC;
using OpenCvSharp;
namespace FIRC
{
public partial class Form1 : Form
{
Mat src = new Mat();
QRManager qm = new QRManager();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
src = Cv2.ImRead(openFileDialog.FileName);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
}
}
private void button2_Click(object sender, EventArgs e)
{
if(pictureBox1.Image==null)
{
return;
}
Mat[] rects;
string[] texts;
qm.Inference(src,out rects,out texts);
Console.WriteLine(string.Format("检测到了{0}个二维码",rects.Length));
var resultMat = qm.DrawImage(rects,texts,src);
pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
}
private void Form1_Load(object sender, EventArgs e)
{
qm.LoadWeights();
}
}
}
?代码优点:
封装成类直接几句即可调用,方便二次开发,同时为了照顾新手源码直接下载即可正常运行,注意要选x64 debug即可
视频演示:
源码下载地址: