#pragma once
#include
#include
#include
#include
using namespace std;
using namespace cv;
struct Result
{
char result[1024];
float x1;
float y1;
float x2;
float y2;
};
extern "C" _declspec(dllexport) const bool Decoder(const char*,Result*);
cv::Ptr detector = nullptr;
class opencvwechats
{
};
.h文件
#include "opencvwechats.h"
int Init()
{
try {
detector = cv::makePtr(
"models\\detect.prototxt",
"models\\detect.caffemodel",
"models\\sr.prototxt",
"models\\sr.caffemodel");
return 0;
}
catch (const std::exception& e) {
std::cout <<
"\n---------------------------------------------------------------\n"
"Failed to initialize WeChatQRCode.\n"
"Please, download 'detector.*' and 'sr.*' from\n"
"https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
"and put them into the current directory.\n"
"---------------------------------------------------------------\n";
std::cout << e.what() << std::endl;
return -1;
}
}
const bool Decoder(const char* imgpath, Result* stu)
{
if (!imgpath)
{
return false;
}
cv::Mat img = cv::imread(imgpath);
if (!img.data)
{
return false;
}
if (!detector)
{
if (Init() < 0)
{
return false;
}
}
std::vector vPoints;
auto res = detector->detectAndDecode(img, vPoints);
if (res.size() > 0)
{
for (int i = 0; i < res.size(); i++)
{
memcpy(stu[i].result, res[i].c_str(), res[i].length());
stu[i].x1 = vPoints[i].at(0, 0);
stu[i].y1 = vPoints[i].at(0, 1);
stu[i].x2 = vPoints[i].at(2, 0);
stu[i].y2 = vPoints[i].at(2, 1);
}
return true;
}
else
{
return false;
}
}
.cpp文件
c++源码
c++编译opencv微信二维码识别dll。-C++文档类资源-CSDN文库
首先自行编译opencv。
win10下编译OpenCV的微信二维码库给Dotnet使用_yinqinggong的博客-CSDN博客
编译好的opencv库
opencv4.5.5带微信二维码识别的编译好的库,c++直接调用。-C++文档类资源-CSDN文库
生成的dll。用c# 调用。
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;
namespace WinFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
public struct Result
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]//4
public string result;
public float x1;
public float y1;
public float x2;
public float y2;
}
public static Bitmap DrawRect(Bitmap bmp,string text, float x1,float y1,float x2,float y2)
{
Graphics gg = Graphics.FromImage(bmp);
Pen p = new Pen(Brushes.Red);
gg.DrawRectangle(p, x1, y1, x2-x1, y2-y1);
Font drawFont = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Millimeter);
SolidBrush drawBush = new SolidBrush(Color.Red);
gg.DrawString(text, drawFont, drawBush, x1, y2);
gg.Dispose();
return bmp;
}
[DllImport("Project1.dll", CallingConvention = CallingConvention.Cdecl)]
extern static bool Decoder(IntPtr path,[Out] Result[] re);
private void button1_Click(object sender, EventArgs e)
{
try
{
pictureBox1.Image = null;
Stopwatch sw = new Stopwatch();
sw.Start();
IntPtr imgpath = Marshal.StringToHGlobalAnsi(Environment.CurrentDirectory +"\"+"dm.bmp");
Result[] re = new Result[20];
bool result = Decoder(imgpath, re);
if (!result) { MessageBox.Show("没有二维码!"); }
Bitmap bitmap= (Bitmap)Image.FromFile("dm.bmp");
pictureBox1.Image = bitmap;
sw.Stop();
StringBuilder sb=new StringBuilder();
sb.Append("耗时"+sw.ElapsedMilliseconds.ToString() + "ms\r\n" );
foreach (var i in re)
{
if(i.x2 !=0)
sb.Append("内容:" +i.result+" 位置:"+i.x1+", "+i.y1+", "+i.x2+", "+i.y2+"\r\n");
DrawRect(bitmap,i.result,i.x1,i.y1,i.x2,i.y2);
}
MessageBox.Show(sb.ToString() );//15ms i5 10400
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
c# 调用。输出识别结果,识别位置。
c# 源码
c#调用c++dll调用opencv-wechat微信识别二维码。-C#文档类资源-CSDN文库
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)