c# 调用c++ opencv编译的微信二维码识别dll

#pragma once

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/wechat_qrcode.hpp>
#include <string>
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<cv::wechat_qrcode::WeChatQRCode> detector = nullptr;

class opencvwechats
{
};

.h文件

#include "opencvwechats.h"

int Init()
{
	try {
		detector = cv::makePtr<cv::wechat_qrcode::WeChatQRCode>(
			"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<cv::Mat> 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<float>(0, 0);
			stu[i].y1 = vPoints[i].at<float>(0, 1);
			stu[i].x2 = vPoints[i].at<float>(2, 0);
			stu[i].y2 = vPoints[i].at<float>(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文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值