欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity調(diào)用C++?dll實(shí)現(xiàn)打開雙目相機(jī)

 更新時(shí)間:2022年05月09日 11:44:46   作者:天人合一peng  
這篇文章主要為大家詳細(xì)介紹了如何在Unity中調(diào)用C++?dll實(shí)現(xiàn)打開雙目相機(jī)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.vs中生成dll

對應(yīng)的生成dll的cpp如下 

#include<opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
 
#define EXPORT_API __declspec(dllexport)
 
VideoCapture my_camera;
int width = 640;
int height = 480;
 
extern "C" bool EXPORT_API openCamera()
{
	bool my_open = false;
	while (!my_camera.isOpened())
	{
		std::cout << "Cannot open the camera!" << std::endl;
		my_camera.open(0);//一個(gè)接口能同時(shí)打開兩個(gè)攝像頭
	}
	my_camera.set(CV_CAP_PROP_FRAME_WIDTH, width*2);
	my_camera.set(CV_CAP_PROP_FRAME_HEIGHT, height);
	if (my_camera.isOpened())
	{
		my_open = true;
	}
	return my_open;
}
 
extern "C" void   EXPORT_API recieveFrame(uchar* texturePtr)
{
 
	Mat my_frameBGR;
	Mat my_frameRBG;
	my_camera >> my_frameBGR;
	if (my_frameBGR.data)
	{
		cvtColor(my_frameBGR, my_frameRBG, CV_BGR2RGB);
		memcpy(texturePtr, my_frameRBG.data, my_frameRBG.cols*my_frameRBG.rows*my_frameRBG.channels()*sizeof(uchar));
	}
 
}
 
extern "C" void EXPORT_API closeCamera()
{
	if (my_camera.isOpened())
	{
		my_camera.release();
	}
}

2.unity中justatry腳本

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;用 c++中 dll 文件需要引入
 
public class justatry : MonoBehaviour {
 
	[DllImport("_dectecting")]
	public static extern bool openCamera();
 
	[DllImport("_dectecting")]
	public static extern bool recieveFrame(byte[] imageData);
 
	[DllImport("_dectecting")]
	public static extern bool 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 () {
		IsOpen = openCamera();
		if(IsOpen)
		{
			imageData = new byte[Length * Width * 3*2];
			tex = new Texture2D(Width*2, Length, TextureFormat.RGB24, false);
		}
	}
	
	// Update is called once per frame
	void Update () {
			if (IsOpen)
			{
			recieveFrame(imageData);
				tex.LoadRawTextureData(imageData);
				tex.Apply();
				GetComponent<Renderer>().material.mainTexture = tex;
 
 
			}
	}
	void CloseCamera()
	{
		if (IsOpen)
		{
		 closeCamera();
 
		}
	}
	public void OnApplicatoinQuit()
	{
		closeCamera();
	}
}
using System.Runtime.InteropServices;用 c++中 dll 文件需要引入

public class justatry : MonoBehaviour {

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

	[DllImport("_dectecting")]
	public static extern bool recieveFrame(byte[] imageData);

	[DllImport("_dectecting")]
	public static extern bool 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 () {
		IsOpen = openCamera();
		if(IsOpen)
		{
			imageData = new byte[Length * Width * 3*2];
			tex = new Texture2D(Width*2, Length, TextureFormat.RGB24, false);
		}
	}
	
	// Update is called once per frame
	void Update () {
			if (IsOpen)
			{
			recieveFrame(imageData);
				tex.LoadRawTextureData(imageData);
				tex.Apply();
				GetComponent<Renderer>().material.mainTexture = tex;


			}
	}
	void CloseCamera()
	{
		if (IsOpen)
		{
		 closeCamera();

		}
	}
	public void OnApplicatoinQuit()
	{
		closeCamera();
	}
}

注意,腳本要掛在plane上

3.在unity中調(diào)試

dll的輸出目錄是 unity項(xiàng)目工程名\Assets\Plugins

D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\Assets\Plugins

4.在vs中調(diào)試

4.1 把unity的工程生成對應(yīng)的exe

這是生成的unity exe對應(yīng)的生成目錄

對應(yīng)的目錄如

D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\test.exe

4.2 在vs項(xiàng)目的屬性中做如下設(shè)置

命令后用上面的路徑,注意是放在命令里不是命令參數(shù)里

在對應(yīng)的unity exe目錄中找到 unity工程名_Data下Plugins的目錄是

D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\test_Data\Plugins

4.3 在VS工程的屬性中的輸出目錄設(shè)置為上面的目錄

做完以上設(shè)置就直接可以在VS下調(diào)試了。

5.注意vs和unity的平臺x86/x64要對應(yīng)

5.1 vs x86/x64

5.2 unity 

如果按以上設(shè)置還是不對,提示找不到dll,則把dll的輸出放在與Plugins或Assets文件夾同一級嘗試。

如果你已經(jīng)把dll放在這里了,還是顯示找不到,則一定是你用vs生成dll的庫沒有在環(huán)境變量里,然后unity里面調(diào)用時(shí)找不到vs生成dll所依賴的一些庫??梢园岩恍┠阒赖膸熘苯臃旁趘s生成的dll一起,全放在unity工程里,應(yīng)該就好了。

以上就是Unity調(diào)用C++ dll實(shí)現(xiàn)打開雙目相機(jī)的詳細(xì)內(nèi)容,更多關(guān)于Unity雙目相機(jī)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論