Unity3D C#调用C++ dll并且实现两者之间的图像传输
2019-04-15 22:13
513 查看
Unity3D官方下载地址
opencv环境配置教程
Dependency Walker下载地址
OpenCV for Unity package提取码:a8h5
参考书籍:Windows核心编程第五版【第19、20章】提取码:u6ps
Unity圣典
vs生成dll
将下图中的两处改为dll编译就会生成dll,改回exe则为应用程序
process.cpp
#include <opencv2\opencv.hpp> #define DLL_API extern "C" __declspec (dllexport) DLL_API void transferImage(int rgbInt[], int width, int height, int channel); void transferImage(int rgbInt[], int width, int height, int channel) { cv::Mat image(height, width, CV_MAKETYPE(CV_8U, channel)); uchar* ptr = image.data; for (int i = 0; i < width*height*channel; i++) { *ptr++ = (uchar)rgbInt[i]; } cv::imshow("image", image); cv::waitKey(10); // start process image image = processImage(image); // end process image ptr = image.data; for (int i = 0; i < width*height*channel; i++) { rgbInt[i] = int(*ptr++); } }
生成process.dll,拷贝到unity工程中【yourProjectPath/Assets/Plugins】
如果没有Plugins文件夹,则自己创建
ProcessImage.cs
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; using OpenCVForUnity; public class ProcessImage: MonoBehaviour { [DllImport("process")] public extern static void transferImage(int[] rgbInt, int width, int height, int channel); public int width = 640; public int height = 480; public VideoCapture capture = new VideoCapture(); private int[] rgbInt; private Mat frame; private Mat gray; public int cntFrame = 0; // Use this for initialization void Start () { capture.open(0); rgbInt = new int[width * height * 3]; frame = new Mat(480, 640, CvType.CV_8UC4); gray = new Mat(); } // Update is called once per frame void Update () { capture.read(frame); Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY); frame.get(0, 0, rgbByte); for (int i = 0; i < width * height*3; i++) { rgbInt[i] = (int)rgbByte[i]; } transferImage(rgbInt, width, height, 3); for (int i = 0; i < width * height * 3; i++) { rgbByte[i] = (byte)rgbInt[i]; } byte[] bb = new byte[3]; int cnt = 0; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { bb[2] = rgbByte[cnt]; cnt += 1; bb[1] = rgbByte[cnt]; cnt += 1; bb[0] = rgbByte[cnt]; cnt += 1; frame.put(i, j, bb); } } Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false, false); Utils.matToTexture2D(frame, texture); gameObject.GetComponent<Renderer>().material.mainTexture = texture; cntFrame++; } }
如果unity运行时找不到process.dll,可能是process.dll调用了其他dll,可以用Dependency Walker查看,将缺少的dll的路径添加到系统环境变量中,unity就可以找到dll了
相关文章推荐
- C#调用C++ Dll 并且实现联调
- C++调用C#的DLL实现方法
- C#调用C++的Dll(dll,DLL)类型之间转换笔记
- C# 调用C++ CLR dll类库时,实现从 string 到 sbyte* 的转换
- Unity3d 调用 Luajit C++ DLL 实现调用 Lua脚本
- C++调用C#的DLL程序实现方法
- [Unity3D]C# 调用C++ DLL
- 在VS2015中用C++创建DLL并用C#调用且同时实现对DLL的调试
- unity3D C#调用C++代码(通过DLL链接库方式)
- C#调用DLL实现图像文字识别(OCR)
- C# 调用C++写的dll的实现方法
- C#调用DLL实现图像文字识别(OCR)
- C++调用C#的DLL实现方法
- Unity3D中使用C#调用C++编写的DLL
- C#调用C++dll方法,char*类型之间的传递
- 实现验证码图像文字的识别(C#调用DLL)
- C#调用DLL实现图像文字识别(OCR)
- 如何实现C++调用C#的dll方法教程
- 实现验证码图像文字的识别(C#调用DLL)
- C++调用C#生成的DLL实现及注意事项