1  VS 工程

// OpencvTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv.hpp>

using namespace cv;


//int main()
//{
// //   std::cout << "Hello World!\n";
//
//	//cv::Mat image = cv::imread("earth.png");
//	//cv::imshow("image", image);
//	//cv::waitKey();
//
//
//	VideoCapture cap(0);
//	if (cap.isOpened())
//	{
//		Mat capture;
//		while (true)
//		{
//			cap >> capture;
//			imshow("Camera", capture);
//			char cease = waitKey(1);
//
//			if (cease == 27 || cease == 32)
//			{
//				break;
//
//			}
//		}
//	}
//	else
//		std::cout << "Maybe no camera!" << std::endl;
//
//
//}

extern "C" __declspec(dllexport)
int addnum()
{
	int a = 10;
	int b = 100;
	int c = a + b;


	return c;
}


extern "C" __declspec(dllexport)
void showing()
{
	   std::cout << "Hello World!\n";

	   cv::Mat image = cv::imread("D:\\U3Dbuffers\\ActiateDll\\Assets\\DLL\\earth.png");
	   cv::imshow("image", image);
	   cv::waitKey();

}


extern "C" __declspec(dllexport)
void opencaera()
{

	VideoCapture cap(0);
	if (cap.isOpened())
	{
		Mat capture;
		while (true)
		{
			cap >> capture;
			imshow("Camera", capture);
			char cease = waitKey(1);

			if (cease == 27 || cease == 32)
			{
				break;

			}
		}
	}
	else
		std::cout << "Maybe no camera!" << std::endl;


}





// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

2  unity 调用dll

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices; //111111111111111111111111

public class OpenCamera : MonoBehaviour {



	[DllImport("OpencvTest")]
	static extern int addnum();



	[DllImport("OpencvTest")]
	static extern void opencaera();

	int c = 0;

	// Use this for initialization
	void Start () {
//		showing ();
		c = addnum();
		print (c);
		//print(addnum());
		opencaera();

		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

运行结果

VS通过opencv显示图片和打开相机的显示生成dll用unity调用_#include

opencv里面控制键盘的输入在这儿也可以

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class OpenCamera : MonoBehaviour {



	[DllImport("OpencvTest")]
	static extern int addnum();



	[DllImport("OpencvTest")]
	static extern void showing();

	int c = 0;

	// Use this for initialization
	void Start () {
//		showing ();
		c = addnum();
		print (c);
		//print(addnum());
		showing();

		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

 

VS通过opencv显示图片和打开相机的显示生成dll用unity调用_资源管理器_02