unity3d 打开相机将视频传给C++ dll

Unity C#

cameraDll.cs

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class cameraDll : MonoBehaviour
{


    [DllImport("run_on_camera_sequence")]
    //static extern void ByteToMat(System.IntPtr imageData, int imageW, int imageH, int nFlag);
    static extern void ByteToMat(byte[] imagedata, int imageW, int imageH, int nFlag);


    // Start is called before the first frame update



    int width = 640;
    int height = 480;

    private WebCamTexture webCamTexture;


    void Start()
    {

        webCamTexture = new WebCamTexture();

        如果有后置摄像头,调用后置摄像头  
        //for (int i = 0; i < WebCamTexture.devices.Length; i++)
        //{

        //    print(WebCamTexture.devices[i].name);

        //    if (WebCamTexture.devices[i].name == "Creative GestureCam")
        //    {
        //        webcamTexture.deviceName = WebCamTexture.devices[i].name;

        //        break;
        //    }
        //}
        webCamTexture.deviceName = WebCamTexture.devices[0].name;
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = webCamTexture;
        webCamTexture.Play();



    }


    // Update is called once per frame
    void Update()
    {

        ByteToMat(ConvertWebCamTextureToByteArray(), 480, 640, 3);

    }

    //获取像素
    public byte[] ConvertWebCamTextureToByteArray()
    {

        Color[] pixels = new Color[width * height];

        pixels = webCamTexture.GetPixels();
        byte[] bytes = new byte[width * height * 4];
        for (int i = 0; i < pixels.Length; i++)
        {
            Color color = pixels[i];
            bytes[i * 4] = (byte)(color.r * 255);
            bytes[i * 4 + 1] = (byte)(color.g * 255);
            bytes[i * 4 + 2] = (byte)(color.b * 255);
            bytes[i * 4 + 3] = (byte)(color.a * 255);
        }

        return bytes;
    }


}

C++

cpp 复制代码
cv::Mat outImg;
extern "C" __declspec(dllexport)
//解析从u3d中传过来的字节格式的图像数据为Mat
void  ByteToMat(byte* pImg, int nH, int nW, int nFlag)//nH,nW为BYTE*类型图像的高和宽,nFlag为通道数
{
	if (pImg == nullptr)
	{
		return ;
	}


	 outImg = cv::Mat(nH, nW, CV_8UC4, pImg);
	cv::cvtColor(outImg, outImg, cv::COLOR_RGB2BGR);
	cv::flip(outImg, outImg, 1);
	 cv::rotate(outImg, outImg, cv::ROTATE_180);

	cv::imshow("u3dvideo", outImg);
	cv::waitKey(1);
}

初阶上nFlag没用,可以不要。

相关推荐
runafterhit9 小时前
显示调研专题-OLED行业技术趋势分析报告
音视频
灰灰勇闯IT10 小时前
DVPP 视频预处理:YOLO 视频检测的瓶颈与解法
yolo·音视频
做人求其滴10 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
见叶之秋10 小时前
C++基础入门指南
开发语言·c++
计算机安禾10 小时前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法
玖釉-10 小时前
C++ 中的循环语句详解:while、do...while、for、嵌套循环与循环控制
开发语言·c++·算法
Luna-player11 小时前
音频服务未运行,未安装音频设备,Windows 无法启动 Windows Audio 服务,错误 0x80070005:拒绝访问,本计算机无法播放音频
音视频
欧米欧11 小时前
C++进阶数据结构之搜索二叉树
开发语言·数据结构·c++
青小莫11 小时前
C++之vector讲解
c++·stl
中小企业实战军师刘孙亮12 小时前
小微企业生存发展指南:从求稳到扩张的实战策略-佛山鼎策创局破局增长咨询
架构·产品运营·音视频·制造·业界资讯