Unity如何调用摄像头并显示在窗口内

Unity如何调用摄像头并显示在窗口内

序言

在学习Unity开发AR过程中,发现无论是Vuforia还是Unity中的ARkit或者ARcore,均是建立一个ARcamera,而这个camera是受他们AR内核支配的,并不是最原始的可控摄像头,所以萌生了想要做一个受自己控制的摄像头。实现这个愿望的第一步就是将摄像头的视频内容接入到Unity中,这篇文章就是讲的这个内容。

目标

  1. Unity可以打开摄像头并在窗口中显示
  2. 如果有多个摄像头,则可以通过按钮进行切换

unity中的实现

  1. 首先创建一个3D项目

  2. 在Hierarchy中创建一个Canvas(画布),在Game窗口中可以设定Canvas大小,目标设定为(1280*820)。

  3. 画布中创建以下子项目
    RawImage(Raw Image)->用于显示摄像头的内容,大小为1280*720,并将RawImage与Canvas上边缘对齐。
    CameraOnButton (button)->用于开启和关闭摄像头,点击第一次时是打开,点击第二次时为关闭。
    CameraSwitchButton (button)->用于切换摄像头。
    BG_Im(Image)->用于放在Canvas底部作为UI的背景使用,颜色调成深灰色。
    创建好后如下所示:
    在这里插入图片描述
    在这里插入图片描述

  4. 创建一个空对象(Empty Object),取名为CameraController, 在CameraController下创建新脚本取名“CameraContrller”,脚本内容如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CameraContrller : MonoBehaviour
{
    

    int currentCamIndex = 0;

    WebCamTexture tex;

    public RawImage display;

    public Text startStopText;

    public void SwapCam_Clicked()
    {
        if (WebCamTexture.devices.Length > 0 )//WebCamTexture.devices.Length表示接入电脑的摄像机数量,当摄像机数量大于0时,运行下面切换操作。
        {
            currentCamIndex += 1;
            currentCamIndex %= WebCamTexture.devices.Length;//这行代码很有学习价值,它的意思是说设定一个数,计数器未达到这个数时,可以增加,当计数器达到这个数时会被重置为0
        }
    }


    public void StartStopCam_Clicked()
    {
        if(tex != null)//stop camera
        {
            StopWebCam();
            startStopText.text = "Start Camera";

        }
        else//start camera
        {
            WebCamDevice device = WebCamTexture.devices[currentCamIndex];
            tex = new WebCamTexture(device.name);//根据设备名称创建一个新的WebCamTexture的类,并赋值给tex,此时tex已经包含了摄像头的视频信号。
            display.texture = tex;//将摄像头的视频信号传递给RawImage中进行画面显示。

            tex.Play();//播放
            startStopText.text = "Stop Camera";
        }

    }

    private void StopWebCam()
    {
        display.texture = null;
        tex.Stop();
        tex = null;
    }
}

接下来,需要将RawImage和CameraOnButton中的Text赋值给CameraController中,并将CameraController赋值给CameraOnButton和CameaSwitchButton中,分别引用各自的函数,如下图:
CameraOnButton:
在这里插入图片描述
CameraSwitchButton:
在这里插入图片描述
以上内容建立好后,运行即可。

可以通过 Unity 的 WebCamTexture 类来调用摄像头拍照。以下是一个简单的示例代码: ``` using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { private WebCamTexture webcamTexture; // Start is called before the first frame update void Start() { // 获取摄像头设备 WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length == 0) { Debug.Log("No camera detected!"); return; } // 使用第一个摄像头设备 webcamTexture = new WebCamTexture(devices[0].name); // 开始捕捉摄像头画面 webcamTexture.Play(); } // 拍照方法 public void TakePhoto() { // 创建 Texture2D 对象 Texture2D photo = new Texture2D(webcamTexture.width, webcamTexture.height); // 从摄像头捕捉画面到 Texture2D 对象 photo.SetPixels(webcamTexture.GetPixels()); photo.Apply(); // 保存照片到本地 byte[] bytes = photo.EncodeToPNG(); System.IO.File.WriteAllBytes(Application.dataPath + "/photo.png", bytes); } // 在场景中显示摄像头画面 void OnGUI() { if (webcamTexture != null && webcamTexture.isPlaying) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), webcamTexture, ScaleMode.ScaleToFit); } } // Stop capturing webcam texture when the script is disabled or destroyed void OnDisable() { if (webcamTexture != null) { webcamTexture.Stop(); } } void OnDestroy() { if (webcamTexture != null) { webcamTexture.Stop(); } } } ``` 在 Start() 方法中,我们获取设备上的摄像头设备,并使用第一个设备。然后通过调用 `WebCamTexture.Play()` 方法开始捕捉摄像头画面。 在 TakePhoto() 方法中,我们创建一个 Texture2D 对象,然后从摄像头捕捉画面到 Texture2D 对象中。最后,我们将照片保存到本地。 在 OnGUI() 方法中,我们通过调用 `GUI.DrawTexture()` 方法来显示摄像头画面。 最后,在 OnDisable() 和 OnDestroy() 方法中,我们停止捕捉摄像头画面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值