当前位置: 代码迷 >> 综合 >> u3d 打开摄像头 截屏
  详细解决方案

u3d 打开摄像头 截屏

热度:80   发布时间:2024-01-28 01:20:49.0

代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class VideoTextureRecordManager : MonoBehaviour
{public RawImage cameraTexture;public WebCamTexture webCameraTexture;// Start is called before the first frame updatevoid Start(){StartCoroutine(GetWebCamDevice());}IEnumerator GetWebCamDevice(){yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);if (Application.HasUserAuthorization(UserAuthorization.WebCam)){WebCamDevice[] devices = WebCamTexture.devices;if(devices.Length != 0){string devicename = devices[0].name;webCameraTexture = new WebCamTexture(devicename, Screen.width, Screen.height);cameraTexture.texture = webCameraTexture;webCameraTexture.Play();}else{Debug.Log("未找到摄像头");}}}////启用此本脚本就启用摄像头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();}}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_IPHONEif (!isOn)cam_Video.rectTransform.localEulerAngles = new Vector3(180, 0, 90);else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 0, -90);
#elif UNITY_ANDROIDif (!isOn)cameraTexture.rectTransform.localEulerAngles = new Vector3(0, 0, 0);elsecameraTexture.rectTransform.localEulerAngles = new Vector3(0, 180, 0);
#endif}public void StartScreenShot(){if(webCameraTexture != null)webCameraTexture.Pause();string _name = "Screenshot_" + GetCurTime() + ".png"; ;StartCoroutine(ScreenShot(_name));}IEnumerator ScreenShot(string strname){Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);yield return new WaitForEndOfFrame();tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);tex.Apply();yield return tex;byte[] byt = tex.EncodeToPNG();string path = Application.persistentDataPath + "/" + strname;File.WriteAllBytes(path, byt);Debug.Log(path);if (webCameraTexture != null)webCameraTexture.Play();}/// <summary>/// 获取当前年月日时分秒/// </summary>/// <returns></returns>string GetCurTime(){return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();}// Update is called once per framevoid Update(){}
}

 

  相关解决方案