Unity调用C++ dll实现打开双目相机
作者:天人合一peng 时间:2022-05-28 13:19:51
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
如果按以上设置还是不对,提示找不到dll,则把dll的输出放在与Plugins或Assets文件夹同一级尝试。
如果你已经把dll放在这里了,还是显示找不到,则一定是你用vs生成dll的库没有在环境变量里,然后unity里面调用时找不到vs生成dll所依赖的一些库。可以把一些你知道的库直接放在vs生成的dll一起,全放在unity工程里,应该就好了。
来源:https://blog.csdn.net/moonlightpeng/article/details/79017204
标签:Unity,C++,dll,双目相机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
OpenCV实现直线拟合
2023-06-22 15:22:37
![](https://img.aspxhome.com/file/2023/4/92494_0s.jpg)
SpringMVC上传图片与访问
2023-12-06 17:54:31
Java使用substring()截取(提取)子字符串
2023-09-29 17:02:04
![](https://img.aspxhome.com/file/2023/0/62670_0s.jpg)
实例讲解JAVA设计模式之备忘录模式
2023-08-29 16:31:19
RestTemplate Get请求实现bean参数传递详解
2022-03-14 22:20:27
![](https://img.aspxhome.com/file/2023/5/76325_0s.png)
java自定义ClassLoader加载指定的class文件操作
2022-03-16 16:19:07
![](https://img.aspxhome.com/file/2023/8/60848_0s.jpg)
Spring实战之ResourceLoader接口资源加载用法示例
2023-11-20 12:16:49
Springboot如何操作redis数据
2022-05-30 21:36:41
java Spring的启动原理详解
2022-09-02 04:39:59
![](https://img.aspxhome.com/file/2023/4/66244_0s.jpg)
深入剖析Java中的各种异常处理方式
2022-05-29 20:20:24
ThreadLocal使用案例_动力节点Java学院整理
2021-06-08 09:57:15
Spring整合Junit的使用详解
2022-11-20 18:33:17
Java泛型机制与反射原理相关知识总结
2023-11-11 06:02:15
![](https://img.aspxhome.com/file/2023/4/58914_0s.jpg)
基于swing实现窗体拖拽和拉伸
2023-11-12 22:32:40
![](https://img.aspxhome.com/file/2023/2/69082_0s.gif)
最简单易懂的java数组排序方法整理
2023-01-03 18:56:41
Android ViewDragHelper使用介绍
2023-08-01 04:18:59
![](https://img.aspxhome.com/file/2023/8/85458_0s.gif)
Intellij搭建springmvc常见问题解决方案
2023-07-23 12:53:29
![](https://img.aspxhome.com/file/2023/9/57419_0s.png)
详解如何将Spring Boot应用跑在Docker容器中
2023-04-25 08:08:58
![](https://img.aspxhome.com/file/2023/8/77158_0s.jpg)
5个主流的Java开源IDE工具详解
2021-10-13 06:06:50
![](https://img.aspxhome.com/file/2023/6/63306_0s.jpg)
SpringBoot整合Zookeeper详细教程
2022-07-24 11:33:09
![](https://img.aspxhome.com/file/2023/2/63392_0s.png)