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没用,可以不要。

相关推荐
蒸蒸yyyyzwd2 小时前
cpp 选手秋招学习笔记 day33
c++·笔记·求职招聘
hansang_IR8 小时前
【题解】P4460 [CQOI2018] 解锁屏幕
c++·算法
高升说8 小时前
多相机同步怎么选:硬件同步与软件同步的原理、对比与选型流程
人工智能·数码相机
ChampaignWolf9 小时前
在 SAP S/4HANA 内部基于 ICF Handler 从零实现 ABAP MCP Server:架构、代码与踩坑实录
c++·架构·sap·abap·mcp
视频技术分享9 小时前
B端音视频SDK:从底层组件到企业业务能力的蜕变
音视频
尘客-追梦10 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt
明志数科11 小时前
从300克Ego头环看第一人称数据采集趋势:设备轻量化之后,场景端壁垒在哪
数码相机·学习
有点。11 小时前
*C++哈夫曼树与哈夫曼编码
java·c++·servlet
啦啦啦啦啦zzzz11 小时前
利用RAII机制进行日志的采集
linux·服务器·c++·网络编程·c++20
nLif12 小时前
基于GTK的简易MFC对话框兼容层 -- MfcToGTK
linux·c++·mfc·gtk