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

相关推荐
豆浩宇几秒前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
WG_179 分钟前
C++多态
开发语言·c++·面试
Charles Ray2 小时前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
重生之我在20年代敲代码2 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
华清远见IT开放实验室4 小时前
【项目案例】物联网比较好的10+练手项目推荐,附项目文档/源码/视频
物联网·音视频
迷迭所归处7 小时前
C++ —— 关于vector
开发语言·c++·算法
CV工程师小林8 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice9 小时前
2024.9.19
c++
天玑y9 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
姜太公钓鲸2339 小时前
c++ static(详解)
开发语言·c++