Unity3d 开发HoloLens2启动摄像头实现截图、拍照功能

Unity3d 开发HoloLens2启动摄像头实现截图、拍照功能


好多人问版本:Unity 2019.4.15f1c1 和 MRTK2.5.1

功能需求

需要调用摄像头进行拍照、截图等功能,之前计划使用高通Vuforia SDK,但是因为不用AR识别功能,同时Vuforia的画面会有水印截图效果不佳,所以选择直接启动摄像头来获取画面。

实现

经测试发现和调用其他设备的摄像头方式无异,这里请求了权限后启动摄像头。
代码:


    private WebCamTexture webCamTextrue;
    [Header("摄像机画面")]
    public RawImage cameraTexture;
    /// <summary>
/// 开启摄像机
/// 调用StartCoroutine(DeviceInit());
    /// </summary>
    IEnumerator DeviceInit()
    {
    
    
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//请求授权使用摄像头
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
    
    
            try
            {
    
    
                WebCamDevice[] devices = WebCamTexture.devices;
                string deviceName = devices[0].name;
                webCamTextrue = new WebCamTexture(deviceName, 1280, 720);//new WebCamTexture(deviceName, 640, 360);
                cameraTexture.texture = webCamTextrue;
                webCamTextrue.Play();
            }
            catch (Exception e)
            {
    
    
                Debug.LogError("Camera Init Exception:" + e);
            }
        }
}


开启权限

需要在player设置里勾选WebCam选项,开启摄像头权限。

在这里插入图片描述

同时初次运行时请求摄像头弹出要同意。如果之前安装过该应用,可能需要卸载重新安装,否则可能会出现调摄像头失败,画面是黑色的情况。

猜你喜欢

转载自blog.csdn.net/qq_33789001/article/details/113543453
今日推荐