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

相关推荐
hetao1733837几秒前
2026-05-25~06-11 hetao1733837 的刷题记录
c++·算法
洛水水5 分钟前
【力扣100题】82.有效的括号
c++·算法·leetcode
超哥--7 分钟前
B站视频内容智能分析系统(六):Text-to-SQL 结构化查询
数据库·sql·音视频
byte轻骑兵11 分钟前
蓝牙CAS通用音频服务:解锁多设备音频协同的底层标准
网络·音视频·cas·le audio·低功耗音频
析稿AI写作19 分钟前
AI视频创作实战:用飙算工具箱实现图转视频与文字成片,个人开发者的多模态效率方案
人工智能·音视频
不昀20 分钟前
VOOHU沃虎:使用音频变压器时常见的接地和屏蔽注意事项有哪些?
网络·音视频·以太网·网络通信·电子元器件
AI服务老曹27 分钟前
解耦安防黑盒:基于 Docker 的国标 GB28181 与 RTSP 统一接入 AI 视频管理平台架构设计(附源码交付与边缘计算实践)
人工智能·docker·音视频
初中就开始混世的大魔王35 分钟前
7 Fast DDS-持久化服务
c++·人工智能·中间件·自动驾驶·信息与通信
luoyayun36140 分钟前
Qt/QML 音频频谱图与频谱瀑布图实现:从 PCM 到频域可视化
qt·音视频·频谱图·频谱瀑布图
爱吃生蚝的于勒42 分钟前
QT开发第三章——常用控件
linux·服务器·开发语言·前端·javascript·c++·qt