• Unity实现摄像头录像功能


    Unity实现摄像头录像功能

    前言

    在之前的很多展馆展示的项目中,甲方有很多要求实现用摄像头录像的功能。使用Unity实现调用USB摄像头画面的功能非常容易实现,但是实现录屏的功能有一些困难,我使用了几种方法都没有实现出想要的效果,后来我在网上找到一款叫做AVProMovieCapture的插件,实现了录屏的良好效果,同时也实现了使用Unity实现摄像头录像的效果,具体实现方法如下所示:

    实现步骤

    1.在项目中导入AVProMovieCapture插件,如下图所示:
    在这里插入图片描述
    2.在场景中新建plane物体,设置如下图所示:
    在这里插入图片描述3.在场景中拖入ScreenGameObject物体,如下图所示:
    在这里插入图片描述
    4.在场景中新建WebCapture物体,在该物体上挂载WebCapture.cs脚本,脚本代码如下图所示:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace RenderHeads.Media.AVProMovieCapture.Demos
    {
        public class WebCapture : MonoBehaviour
        {
            private class Instance
            {
                public string name;
                public WebCamTexture texture;
                public CaptureFromTexture capture;
                public CaptureGUI gui;
            }
    
            [SerializeField]
            private GUISkin _skin;
    
            //[SerializeField]
            //private GameObject _prefab;
    
            [SerializeField]
            private int _webcamResolutionWidth = 1920;
    
            [SerializeField]
            private int _webcamResolutionHeight = 1080;
    
            [SerializeField]
            private int _webcamFrameRate = 30;
    
            //State
            private Instance[] _instances;
            private int _selectedWebcamIndex;
            //显示视频的面板
            public MeshRenderer plane;
            //调用录像的脚本物体
            public CaptureGUI captureObject;
    
            private void Start()
            {
                //Create instance data per webcam
                int numCams = WebCamTexture.devices.Length;
                _instances = new Instance[numCams];
                for (int i = 0;i < numCams;i++)
                {
                    //GameObject go = (GameObject)GameObject.Instantiate(_prefab);
    
                    Instance instance = new Instance();
                    instance.name = WebCamTexture.devices[i].name;
                    //instance.capture = go.GetComponent();
                    instance.capture = captureObject.gameObject.GetComponent<CaptureFromTexture>();
                    instance.capture._autoFilenamePrefix = "Demo4Webcam-" + i;
                    //instance.gui = go.GetComponent();
                    instance.gui = captureObject.gameObject.GetComponent<CaptureGUI>();
                    instance.gui._showUI = true;
    
                    _instances[i] = instance;
                }
    
                if (numCams > 0)
                {
                    Change(0);
                }
                StartCoroutine(OpenCamera());
                //captureObject = GameObject.Find("ScreenGameObject(Clone)").GetComponent();
            }
            /// 
            /// 开启摄像头
            /// 
            /// 
            IEnumerator OpenCamera()
            {
                yield return new WaitForSeconds(0.5f);
                beginCamera();
                yield return new WaitForSeconds(0.5f);
                captureObject.ToStartCapture();
            }
    
            private void StartWebcam(Instance instance)
            {
                instance.texture = new WebCamTexture(instance.name,_webcamResolutionWidth,_webcamResolutionHeight,_webcamFrameRate);
                instance.texture.Play();
                if (instance.texture.isPlaying)
                {
                    instance.capture.SetSourceTexture(instance.texture);
                    plane.material.mainTexture = instance.texture;
                }
                else
                {
                    StopWebcam(instance);
                }
            }
    
            private void StopWebcam(Instance instance)
            {
                if (instance.texture != null)
                {
                    if (instance.capture != null && instance.capture.IsCapturing())
                    {
                        instance.capture.SetSourceTexture(null);
                        instance.capture.StopCapture();
                    }
                    instance.texture.Stop();
                    Destroy(instance.texture);
                    instance.texture = null;
                }
            }
    
            private void OnDestroy()
            {
                for (int i = 0;i < _instances.Length;i++)
                {
                    StopWebcam(_instances[i]);
                }
            }
    
            private void Change(int index)
            {
                _selectedWebcamIndex = index;
                for (int j = 0;j < _instances.Length;j++)
                {
                    _instances[j].gui._showUI = (j == _selectedWebcamIndex);
                }
            }
            /// 
            /// 开启摄像头
            /// 
            public void beginCamera()
            {
                for (int i = 0;i<_instances.Length;i++)
                {
                    Instance webcam = _instances[i];
                    StartWebcam(webcam);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    5.运行场景,发现已经调用了摄像头,如下图所示:
    在这里插入图片描述
    6.虽然调用了摄像头,但是不知道是否已经进行了录像,查找到工程下的movie文件夹,发现已经录入了视频,从而实现了使用usb摄像头录像的功能,如下图所示:
    在这里插入图片描述
    7.实现录像功能,有的需求还需要获取到这些视频并且展示出来,这个也在我之前的项目实现了,具体怎么实现不再赘述了,在这里将核心代码分享在这里:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    
    
    public class Load : MonoBehaviour
    {
        public List<string> filePaths;
        public static string[][] pic;
        private List<string> LAN;
        private string movieUrl;
        //遍历的视频数量
        public static int movieNumber = 0;
    
        private void Start()
        {
            movieUrl = ConfigTest.dic["录像路径"]["url"];
            LAN = new List<string>();
            LAN.Add(movieUrl);
            pic = new string[LAN.Count][];
            Debug.Log(pic.Length);
            LoadIma();
        }
    
        void LoadIma()
        {
            for (int i = 0;i < pic.Length;i++)
            {
                pic[i] = (load(LAN[i],i));
            }
        }
    
        string[] load(string LAN,int t)
        {
            filePaths = new List<string>();
            string imgtype = "*.mp4|*.mov|*.avi";
            string[] ImageType = imgtype.Split('|');
            for (int i = 0;i < ImageType.Length;i++)
            {
                //获取所有视频视频的路径
                string[] dirs = Directory.GetFiles(@"" + LAN,ImageType[i]);
                //Debug.Log(dirs.Length);
                //movieNumber = dirs.Length;
                for (int j = 0;j < dirs.Length;j++)
                {
                    filePaths.Add(dirs[j]);
                    movieNumber = j;
                    //Debug.Log(movieNumber);
                }
            }
            return fuzhi(t);
        }
    
        public string[] fuzhi(int t)
        {
            pic[t] = new string[filePaths.Count];
            for (int i = 0; i < filePaths.Count;i++)
            {
                pic[t][i] = filePaths[i];
            }
            return pic[t];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    结尾语

    网上开发的各种大神有很多,他们开发出许许多多的插件供我们使用,极大节省了我们的开发时间,在这里向他们表示感谢。我作为一名Unity小菜鸟,希望和大家有问题一起讨论,共同进步,大家有问题可以私聊我。

  • 相关阅读:
    命令解压aar、文件压缩成aar图文详解
    C++11 - 2 - 右值引用与移动构造
    【Java Web】统一处理异常
    云渲染比本地电脑渲染快多少?一个案例生动告诉你云渲染的速度优势
    【RNN从入门到实战】GRU入门到实战——使用GRU预测股票。
    1.4+1.5 L1、L2正则化
    计算机竞赛 身份证识别系统 - 图像识别 深度学习
    My Ninety-second - 最长重复子数列 - By Nicolas
    超分辨率提升IRN网络
    分销会员系统开发
  • 原文地址:https://blog.csdn.net/qq_17367039/article/details/128099463