Unity摄像头交互打开,关闭,保存

直接上代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// 简单的摄像头单例类,挂载在场景物体上
/// </summary>
public class WebCamera : MonoBehaviour
{    
    public static WebCamera Instance;
    /// <summary>
    /// 当前摄像头下标,存在多个摄像头设备时用于切换功能
    /// </summary>
    private int curCamIndex = 0;
    /// <summary>
    /// 所有摄像头设备列表
    /// </summary>
    private WebCamDevice[] devices;
    /// <summary>
    /// 摄像头渲染纹理
    /// </summary>
    private WebCamTexture webCamTex;
    /// <summary>
    /// 当前设备的名称
    /// </summary>
    public string deviceName { get; private set; }
    /// <summary>
    /// 摄像头是否打开
    /// </summary>
    public bool CameraIsOpen { get; private set; }
    /// <summary>
    /// 最终渲染画面
    /// </summary>
    public Texture renderTex { get; private set; }

    /// <summary>
    /// 最新的截图
    /// </summary>
    public Texture2D lastShotText { get; private set; }
    /// <summary>
    /// 画面的宽高
    /// </summary>
    private int width,height;
    /// <summary>
    /// 帧率
    /// </summary>
    private int fps;

    void Awake()
    {
        Instance = this;
    }

    private void Update()
    {
       
    }
    public void InitCamera(int width,int height,int fps=30)
    {        
        this.width = width;
        this.height = height;
        this.fps = fps;
    }

    /// <summary>
    /// 打开摄像头
    /// </summary>
    public void OpenCamera()
    {
        //用户授权
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            //显示画面的设备就是要打开的摄像头
            devices = WebCamTexture.devices;
            if (devices.Length <= 0)
            {
                Debug.LogError("没有检测到摄像头,检查设备是否正常"); return;
            }
            deviceName = devices[curCamIndex].name;
            webCamTex = new WebCamTexture(deviceName, width,height,fps);

            renderTex = webCamTex;
            //开启摄像头
            webCamTex.Play();
            CameraIsOpen = true;
        }
    }

    /// <summary>
    /// 关闭摄像头
    /// </summary>
    public void CloseCamera()
    {
        if (CameraIsOpen && webCamTex != null)
        {
            webCamTex.Stop();
            CameraIsOpen=false;
        }
    }
    /// <summary>
    /// 切换摄像头
    /// </summary>
    public void SwapCamera()
    {
        if (devices.Length > 0)
        {
            curCamIndex = (curCamIndex + 1) % devices.Length;
            if (webCamTex!= null)
            {
                CloseCamera();
                OpenCamera();
            }
        }
    }

    public void SaveScreenShot(string path)
    {
        Texture2D shotTex = TextureToTexture2D(webCamTex);
        lastShotText = shotTex;
        byte[] textureBytes = shotTex.EncodeToJPG();
        string fileName = "Test.jpg"; //string.Format("IMG_{0}{1}{2}_{3}{4}{5}.jpg",DateTime.Now.Year,DateTime.Now.Month,//保存名称
           // DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        Debug.Log($"图片已保存:{path}/{fileName}");
        File.WriteAllBytes($"{ path}/{fileName}", textureBytes);
        if (File.Exists($"{path}/{fileName}"))
        {
            Debug.Log("找到照片");
        }
        else
        {
            Debug.Log("未找到");
        }
    }


    /// <summary>
    /// Texture转换成Texture2D
    /// </summary>
    /// <param name="texture"></param>
    /// <returns></returns>
    private Texture2D TextureToTexture2D(Texture texture)
    {
        Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
        RenderTexture currentRT = RenderTexture.active;
        RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
        Graphics.Blit(texture, renderTexture);

        RenderTexture.active = renderTexture;
        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        texture2D.Apply();

        RenderTexture.active = currentRT;
        RenderTexture.ReleaseTemporary(renderTexture);

        return texture2D;
    }
    private void OnDisable()
    {
        CloseCamera();
    }

}

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Unity是一款跨平台的游戏引擎,它可以用于开发各种类型的游戏和应用程序。在Unity中,摄像头识别是指利用摄像头捕捉图像,并进行图像处理、分析和识别的功能。 Unity提供了相机组件,可以通过将相机组件添加到场景中的游戏对象上来实现摄像头识别。相机组件可以设置摄像头的位置、方向和视野范围等参数,来控制摄像头的行为。 在摄像头识别中,一般会使用图像处理和计算机视觉算法来实现对象的识别和跟踪。Unity提供了OpenCV等图像处理库的接口,可以方便地进行图像处理和分析。通过将摄像头捕获的图像传入图像处理算法,可以实现对图像中目标对象的识别和跟踪。 除了基本的摄像头捕获和图像处理功能,Unity还提供了一些辅助工具和插件,可以帮助开发者更方便地实现摄像头识别。例如,Vuforia是一款强大的增强现实引擎,可以与Unity集成,提供全面的增强现实功能,包括图像识别、目标跟踪和虚拟物体的叠加等。 总而言之,Unity摄像头识别是利用Unity引擎的相机组件和图像处理算法,实现对摄像头捕获的图像进行分析和识别的功能。通过这个功能,开发者可以开发出基于摄像头交互应用程序,如增强现实游戏、人脸识别应用等。 ### 回答2: Unity 摄像头识别是指使用Unity引擎中的摄像头功能来实现图像识别和物体追踪的技术。Unity引擎提供了强大的摄像头模块,可以让开发者在游戏或应用中利用摄像头实现各种功能。 在Unity中,通过使用WebCamTexture类可以轻松地获取和操作摄像头图像。开发者可以通过调用该类的构造函数来创建WebCamTexture实例,然后将其应用于游戏对象的渲染器或材质。这样,摄像头捕捉到的图像就会在游戏中实时显示出来。 利用WebCamTexture,开发者可以实现摄像头识别功能。例如,可以使用OpenCV等开源图像处理库来对摄像头捕获的图像进行处理,实现图像识别、人脸识别等功能。开发者可以编写自定义的算法对图像进行分析和处理,从而实现特定的应用场景,如虚拟现实、增强现实等。 除了图像识别,Unity摄像头还可以用于实现物体追踪功能。通过将摄像头的图像与预定义的模型或目标进行比较,可以实时追踪特定物体的位置和姿态。这对于游戏开发或增强现实应用来说非常有用,可以实现虚拟物体与真实场景的交互。 总而言之,Unity摄像头识别是指利用Unity引擎中的摄像头功能实现图像识别和物体追踪的技术。开发者可以利用摄像头实时获取图像,并使用各种开源图像处理库和自定义算法对图像进行处理和分析,从而实现不同的应用场景,如虚拟现实、增强现实等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值