Unity調(diào)用C++?dll實(shí)現(xiàn)打開雙目相機(jī)
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)文章
DataTable的AcceptChanges()和RejectChanges()方法介紹并實(shí)現(xiàn)DataGridView
這篇文章介紹了DataTable的AcceptChanges()和RejectChanges()方法并實(shí)現(xiàn)DataGridView數(shù)據(jù)增、刪、改,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02C#實(shí)現(xiàn)簡單成績管理系統(tǒng)的完整步驟
這篇文章主要給大家介紹了關(guān)于C#實(shí)現(xiàn)簡單成績管理系統(tǒng)的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11c#異步讀取數(shù)據(jù)庫與異步更新ui的代碼實(shí)現(xiàn)
這篇文章主要介紹了c#從數(shù)據(jù)庫里取得數(shù)據(jù)并異步更新ui的方法,大家參考使用吧2013-12-12WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法
這篇文章主要介紹了WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法,涉及ListBox屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2016-09-09C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解
這篇文章主要為大家詳細(xì)介紹了C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08