unity调用C++ dll打开双目相机
C++ 调用 打开 Unity dll 相机 双目
2023-09-27 14:25:49 时间
1, vs中生成dll
对应的生成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);//一个接口能同时打开两个摄像头
}
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中调试则dll的输出目录是 unity项目工程名\Assets\Plugins
D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\Assets\Plugins
4,在vs中调试则
4.1 把unity的工程生成对应的exe
这是生成的unity exe对应的生成目录
对应的目录如
D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\test.exe
4.2 在vs项目的属性中做如下设置
命令后用上面的路径,注意是放在命令里不是命令参数里
在对应的unity exe目录中找到 unity工程名_Data下Plugins的目录是
D:\Michael Wang\SC\2018\1\openDoublecamera\UnityTry\test_Data\Plugins
4.3 在VS工程的属性中的输出目录设置为上面的目录
做完以上设置就直接可以在VS下调试了。
5 注意vs和unity的平台x86/x64要对应
5.1 vs x86/x64
5.2 unity
5.3, 如果按以上设置还是不对,提示找不到dll,则把dll的输出放在与Plugins或Assets文件夹同一级尝试。
如果你已经把dll放在这里了,还是显示找不到,则一定是你用vs生成dll的库没有在环境变量里,然后unity里面调用时找不到vs生成dll所依赖的一些库。可以把一些你知道的库直接放在vs生成的dll一起,全放在unity工程里,应该就好了。
相关文章
- C++的拷贝构造函数的四种调用
- C++ char[] 转 DWORD、DWORD - string 互转
- C/C++知识要点2——STL中Vector、Map、Set容器的实现原理
- c# 传递Null的string值导致的调用C++的dll报错 Attempted to read or write protected memory.
- 在VS2015中用C++创建DLL并用C#调用且同时实现对DLL的调试
- (C++C#类型互转工具)使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
- python 调用C++ DLL,传递int,char,char*,数组和多维数组
- 【C++】多态:dynamic_cast
- c#调用c++的dll
- C/C++ http协议发送字段,文件,单个和多张图片
- 调用C++dll
- c++中非静态函数不能用类名调用,为什么CWnd的成员函数GetDC()可以直接调用啊?
- C++在dll中获取自身路径(非exe调用路径)
- C++ 通讯录管理系统
- 《C和C++代码精粹》——2.14 封装和不完全类型
- 基于C++ MCF 实现(图形界面)通讯录管理系统【100010625】
- C与C++之间相互调用
- C/C++ 字符串分割: strtok 与 strsep 函数说明
- C++ | (struct)结构体变量作为函数参数调用的方法小结
- C/C++调用java---JNI常用函数
- 【C++快速上手】二十一、初始化列表与赋值 学习笔记(待更)
- C++继承中析构函数 构造函数的调用顺序以及虚析构函数
- C++实现ftp客户端
- configure: error: C++ preprocessor "/lib/cpp" fails sanity check