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
    }
 
 
 
    
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章