经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity调用C++?dll实现打开双目相机
来源:jb51  时间:2022/5/9 14:22:30  对本文有异议

1.vs中生成dll

对应的生成dll的cpp如下 

  1. #include<opencv2/opencv.hpp>
  2. #include "opencv2/core/core.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. using namespace cv;
  5. #define EXPORT_API __declspec(dllexport)
  6. VideoCapture my_camera;
  7. int width = 640;
  8. int height = 480;
  9. extern "C" bool EXPORT_API openCamera()
  10. {
  11. bool my_open = false;
  12. while (!my_camera.isOpened())
  13. {
  14. std::cout << "Cannot open the camera!" << std::endl;
  15. my_camera.open(0);//一个接口能同时打开两个摄像头
  16. }
  17. my_camera.set(CV_CAP_PROP_FRAME_WIDTH, width*2);
  18. my_camera.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  19. if (my_camera.isOpened())
  20. {
  21. my_open = true;
  22. }
  23. return my_open;
  24. }
  25. extern "C" void EXPORT_API recieveFrame(uchar* texturePtr)
  26. {
  27. Mat my_frameBGR;
  28. Mat my_frameRBG;
  29. my_camera >> my_frameBGR;
  30. if (my_frameBGR.data)
  31. {
  32. cvtColor(my_frameBGR, my_frameRBG, CV_BGR2RGB);
  33. memcpy(texturePtr, my_frameRBG.data, my_frameRBG.cols*my_frameRBG.rows*my_frameRBG.channels()*sizeof(uchar));
  34. }
  35. }
  36. extern "C" void EXPORT_API closeCamera()
  37. {
  38. if (my_camera.isOpened())
  39. {
  40. my_camera.release();
  41. }
  42. }

2.unity中justatry脚本

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Runtime.InteropServices;用 c++中 dll 文件需要引入
  6. public class justatry : MonoBehaviour {
  7. [DllImport("_dectecting")]
  8. public static extern bool openCamera();
  9. [DllImport("_dectecting")]
  10. public static extern bool recieveFrame(byte[] imageData);
  11. [DllImport("_dectecting")]
  12. public static extern bool closeCamera();
  13. public bool IsOpen = false;
  14. public byte[] imageData;
  15. public Texture2D tex;
  16. public int Width = 640;
  17. public int Length = 480;
  18. // Use this for initialization
  19. void Start () {
  20. IsOpen = openCamera();
  21. if(IsOpen)
  22. {
  23. imageData = new byte[Length * Width * 3*2];
  24. tex = new Texture2D(Width*2, Length, TextureFormat.RGB24, false);
  25. }
  26. }
  27. // Update is called once per frame
  28. void Update () {
  29. if (IsOpen)
  30. {
  31. recieveFrame(imageData);
  32. tex.LoadRawTextureData(imageData);
  33. tex.Apply();
  34. GetComponent<Renderer>().material.mainTexture = tex;
  35. }
  36. }
  37. void CloseCamera()
  38. {
  39. if (IsOpen)
  40. {
  41. closeCamera();
  42. }
  43. }
  44. public void OnApplicatoinQuit()
  45. {
  46. closeCamera();
  47. }
  48. }
  49. using System.Runtime.InteropServices;用 c++中 dll 文件需要引入
  50.  
  51. public class justatry : MonoBehaviour {
  52.  
  53. [DllImport("_dectecting")]
  54. public static extern bool openCamera();
  55.  
  56. [DllImport("_dectecting")]
  57. public static extern bool recieveFrame(byte[] imageData);
  58.  
  59. [DllImport("_dectecting")]
  60. public static extern bool closeCamera();
  61. public bool IsOpen = false;
  62. public byte[] imageData;
  63. public Texture2D tex;
  64. public int Width = 640;
  65. public int Length = 480;
  66. // Use this for initialization
  67. void Start () {
  68. IsOpen = openCamera();
  69. if(IsOpen)
  70. {
  71. imageData = new byte[Length * Width * 3*2];
  72. tex = new Texture2D(Width*2, Length, TextureFormat.RGB24, false);
  73. }
  74. }
  75. // Update is called once per frame
  76. void Update () {
  77. if (IsOpen)
  78. {
  79. recieveFrame(imageData);
  80. tex.LoadRawTextureData(imageData);
  81. tex.Apply();
  82. GetComponent<Renderer>().material.mainTexture = tex;
  83.  
  84.  
  85. }
  86. }
  87. void CloseCamera()
  88. {
  89. if (IsOpen)
  90. {
  91. closeCamera();
  92.  
  93. }
  94. }
  95. public void OnApplicatoinQuit()
  96. {
  97. closeCamera();
  98. }
  99. }

注意,脚本要挂在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工程里,应该就好了。

以上就是Unity调用C++ dll实现打开双目相机的详细内容,更多关于Unity双目相机的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号