unity 打开摄像头

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Text.RegularExpressions;
using System.IO;
public class TKVideoAndTextureRecordManager : MonoBehaviour
{
    public static TKVideoAndTextureRecordManager TKVideoAndTextureRecordManagerInstance;
    public RawImage cameraTexture;
    public WebCamTexture webCameraTexture;
    
    //public Button SaveButton;
    
    public int captureid = 0;
    long[] listLocalVideos;
    long currenyListLocalVideosPathID;
    public int CurrentItemID;
    private void Awake()
    {
       
    }
    IEnumerator Start()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            string devicename = devices[0].name;
           webCameraTexture = new WebCamTexture(devicename, Screen.width, Screen.height);
            cameraTexture.texture =webCameraTexture;
           webCameraTexture.Play();
        }
        //OnBindEvent();
        //SaveButton.enabled = false;
        //Debug.LogError("666");
    }
    启用此本脚本就启用摄像头
    private void OnEnable()
    {
        //if (TKDeviceInformationManagerRef != null)
        //{
        //    captureid = TKDeviceInformationManagerRef.CurrentTextureID;
        //}
        if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)
        {
            webCameraTexture.Play();
        }
    }
    //禁用此脚本时停止摄像头
    private void OnDisable()
    {
        if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)
        {
            webCameraTexture.Stop();
        }
    }
 
    void Update()
    {
 
        //ScreenChange();
    }
 
    int width;
    /// <summary>
    /// 横竖屏切换
    /// </summary>
    void ScreenChange()
    {
        if (width == Screen.width)
            return;
        width = Screen.width;
 
        if (width > Screen.height)
        {
            cameraTexture.transform.localEulerAngles = Vector3.zero;
        }
        else
        {
            cameraTexture.transform.localEulerAngles = new Vector3(0, 0, -90);
        }
    }
    /// <summary>
    /// 切换手机前后摄像头
    /// </summary>
    /// <param name="isOn"></param>
    void changeCam(bool isOn)
    {
        StartCoroutine(CallCamera(isOn));
    }
    IEnumerator CallCamera(bool isOn)
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            if (webCameraTexture != null)
                webCameraTexture.Stop();
            WebCamDevice[] cameraDevices = WebCamTexture.devices;
            string deviceName = "";
            for (int i = 0; i < cameraDevices.Length; i++)
            {
                //如果是前置摄像机??
                if (WebCamTexture.devices[i].isFrontFacing && isOn)
                {
                    deviceName = WebCamTexture.devices[i].name;
                    TurnCam(isOn);
                    break;
                }
                //如果是后置摄像机
                else if (!WebCamTexture.devices[i].isFrontFacing && !isOn)
                {
                    deviceName = WebCamTexture.devices[i].name;
                    TurnCam(isOn);
                    break;
                }
            }
            webCameraTexture = new WebCamTexture(deviceName, Screen.width, Screen.height);
            cameraTexture.texture = webCameraTexture;
            webCameraTexture.Play();
        }
    }
    ///<summary>
    ///翻转plane,正确显示摄像头数据
    ///</summary>
    ///<param name="isOn">If set to <c>true</c> is turn.</param>
    public void TurnCam(bool isOn)
    {
#if UNITY_IOS || UNITY_IPHONE
        if (!isOn)
        cam_Video.rectTransform.localEulerAngles = new Vector3(180, 0, 90);
        else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 0, -90);
#elif UNITY_ANDROID
        if (!isOn)
            cameraTexture.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
        else
            cameraTexture.rectTransform.localEulerAngles = new Vector3(0, 180, 0);
#endif
    }
 
 
 
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值