Unity调用dll打开多个相机

//为了让dll导出函数,需要在每一个需要被导出的函数前面加上标识符:

__declspec(dllexport)

//在编译链接时,C++会按照自己的规则篡改函数的名称,这一过程称为“名字改编”。这会导致不同的编译器、不同的语言下调用dll发生问题。因此我们希望动态链接库文件在编译时,导出函数的名称不要发生变化。 
为了实现这一目的,可以在定义导出函数时加上限定符

extern “C”

 (1) TryTry.cpp中添加如下语句:

#include<opencv2/opencv.hpp>
using namespace cv;

//..\..\UnityActivate\VSActivateUnity.exe


#define EXPORT_API __declspec(dllexport)

VideoCapture my_cameraRight;
VideoCapture my_cameraLeft;
//Mat image, imageR, imageL;
int m_width = 640;
int m_height = 480;


extern "C" bool  EXPORT_API openCamera()
{
	bool my_open = false;
	while (!my_cameraRight.isOpened() && !my_cameraLeft.isOpened())
	{
		//std::cout << "Cannot open the camera!" << std::endl;
		my_cameraRight.open(0);
		my_cameraLeft.open(1);

	}



	if (my_cameraRight.isOpened() && my_cameraLeft.isOpened())
	{

		my_cameraRight.set(CV_CAP_PROP_FRAME_WIDTH, m_width);
		my_cameraRight.set(CV_CAP_PROP_FRAME_HEIGHT, m_height);

		my_cameraLeft.set(CV_CAP_PROP_FRAME_WIDTH, m_width);
		my_cameraLeft.set(CV_CAP_PROP_FRAME_HEIGHT, m_height);

		my_open = true;
	}
	return my_open;
}


extern "C" void   EXPORT_API recieveFrameRight(uchar* texturePtrRight)
{

	Mat my_frameBGR_Right;
	Mat my_frameRBG_Right;

	my_cameraRight >> my_frameBGR_Right;

	if (my_frameBGR_Right.data )
	{
		cvtColor(my_frameBGR_Right, my_frameRBG_Right, CV_BGR2RGB);
		memcpy(texturePtrRight, my_frameRBG_Right.data, my_frameRBG_Right.cols*my_frameRBG_Right.rows*my_frameRBG_Right.channels()*sizeof(uchar));

	}

}


extern "C" void   EXPORT_API recieveFrameLeft( uchar* texturePtrLeft)
{

	Mat my_frameBGR_Left;
	Mat  my_frameRBG_Left;

	my_cameraLeft >> my_frameBGR_Left;

	if ( my_frameBGR_Left.data)
	{
		cvtColor(my_frameBGR_Left, my_frameRBG_Left, CV_BGR2RGB);
		memcpy(texturePtrLeft, my_frameRBG_Left.data, my_frameRBG_Left.cols*my_frameRBG_Left.rows*my_frameRBG_Left.channels()*sizeof(uchar));
	}

}

extern "C" void EXPORT_API closeCamera()
{
	if (my_cameraRight.isOpened() && my_cameraLeft.isOpened())
	{
		my_cameraRight.release();
		my_cameraLeft.release();
	}
}

(2)在Unity中编写TryTryDllLeft和TryTryDllRight两个C#脚本如下,分别挂在PlaneLeft、PlaneRight两个GameObject上

①TryTryDllLeft

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;//用c++中dll文件需要引入  

public class TryTryDllLeft : MonoBehaviour
{

    [DllImport("OpenDoubleCamera")]
    public static extern bool openCamera();

    [DllImport("OpenDoubleCamera")]
    public static extern void recieveFrameRight(byte[] imageData);

    [DllImport("OpenDoubleCamera")]
    public static extern void closeCamera();

    public bool IsOpen = false;
    public byte[] imageData;
    public Texture2D tex;
    public int Width = 640;
    public int Length = 480;

    // Use this for initialization
    void Start()
    {
        //open one camera
        IsOpen = openCamera();
        if (IsOpen)
        {
            imageData = new byte[Length * Width * 3];
            tex = new Texture2D(Width, Length, TextureFormat.RGB24, false);
        }

    }

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

        //update one camera
        if (IsOpen)
        {
            recieveFrameRight(imageData);
            tex.LoadRawTextureData(imageData);
            tex.Apply();
            GetComponent<Renderer>().material.mainTexture = tex;

        }
    }


    void OnGUI()
    {
        if (!IsOpen)
        {
            //string myword = "打开相机失败!";
            string myword = "camera pause for a while!";
            GUI.TextArea(new Rect(200, 100, 200, 50), myword, new GUIStyle());
        }
        //to get the button
        GUI.color = Color.yellow;
        GUI.backgroundColor = Color.red;
        if (GUI.Button(new Rect(40, 50, 60, 30), "continue"))
        {
            IsOpen = true;
        }

        GUI.color = Color.white;
        GUI.backgroundColor = Color.green;
        if (GUI.Button(new Rect(40, 100, 60, 30), "pause"))
        {
            IsOpen = false;
        }

        GUI.color = Color.white;
        GUI.backgroundColor = Color.blue;
        if (GUI.Button(new Rect(40, 150, 60, 30), "close"))
        {
            closeCamera();
        }
    }
}

②TryTryDllRight

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;//用c++中dll文件需要引入  

public class TryTryDllRight : MonoBehaviour
{

    [DllImport("OpenDoubleCamera")]
    public static extern bool openCamera();

    [DllImport("OpenDoubleCamera")]
    public static extern void recieveFrameLeft(byte[] imageData);

    [DllImport("OpenDoubleCamera")]
    public static extern void closeCamera();

    public bool IsOpen = false;
    public byte[] imageData;
    public Texture2D tex;
    public int Width = 640;
    public int Length = 480;

    // Use this for initialization
    void Start()
    {
        //open one camera
        IsOpen = openCamera();
        if (IsOpen)
        {
            imageData = new byte[Length * Width * 3];
            tex = new Texture2D(Width, Length, TextureFormat.RGB24, false);
        }

    }

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

        //update one camera
        if (IsOpen)
        {
            recieveFrameLeft(imageData);
            tex.LoadRawTextureData(imageData);
            tex.Apply();
            GetComponent<Renderer>().material.mainTexture = tex;

        }
    }


    void OnGUI()
    {
        if (!IsOpen)
        {
            //string myword = "打开相机失败!";
            string myword = "camera pause for a while!";
            GUI.TextArea(new Rect(900, 100, 200, 50), myword, new GUIStyle());
        }
        //to get the button
        GUI.color = Color.yellow;
        GUI.backgroundColor = Color.red;
        if (GUI.Button(new Rect(1200, 50, 60, 30), "continue"))
        {
            IsOpen = true;
        }

        GUI.color = Color.white;
        GUI.backgroundColor = Color.green;
        if (GUI.Button(new Rect(1200, 100, 60, 30), "pause"))
        {
            IsOpen = false;
        }

        GUI.color = Color.white;
        GUI.backgroundColor = Color.blue;
        if (GUI.Button(new Rect(1200, 150, 60, 30), "close"))
        {
            closeCamera();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/KillMeHealMe/article/details/83754227