WebCanTest
2017-11-07 本文已影响26人
萧非子
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class WebCanTest : MonoBehaviour {
private string deviceName;//摄像头名称
private WebCamTexture _WebCamTexture;//摄像纹理
public RawImage rawImage;//显示摄像的图片
public Image image;//显示排出来的照片
private string infoLog = "";//日志信息
public Text debugInfo; // 显示debug信息
private string strTempTime;//当前时间,拍照的照片名称
private static WebCamDevice[] webCamArray = null;//硬件_摄像头数组
private int index = 0;
private void Awake()
{
}
private void Update()
{
debugInfo.text = infoLog;
}
// 获取摄像头设备
public void GetMicrophoneDevice()
{
webCamArray = WebCamTexture.devices;
if (webCamArray.Length == 0)
{
Debug.LogError("找不到摄像头硬件设备!");
ShowInfoLog("找不到摄像头硬件设备!");
return;
}
else
{
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
Debug.Log("摄像头已经就绪,可以录像!");
ShowInfoLog("摄像头已经就绪,可以录像!!");
}
else
{
Debug.Log("摄像头权限被禁止,请获取摄像头权限!");
ShowInfoLog("摄像头权限被禁止,请获取摄像头权限!");
}
}
}
public void OpenWebCam()
{
GetMicrophoneDevice();
_WebCamTexture.Play();
rawImage.GetComponent<RawImage>().texture = _WebCamTexture;
Debug.Log("开启摄像头!");
ShowInfoLog("开启摄像头!");
}
IEnumerator Start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
webCamArray = WebCamTexture.devices;
deviceName = webCamArray[index].name;
//摄像机摄像的区域
_WebCamTexture = new WebCamTexture(deviceName, 400, 300, -112);
//_WebCamTexture.Play();
// Debug.Log("录像头运行中!!");
//ShowInfoLog("录像头运行中!");
}
}
//暂停
public void PauseWebCam()
{
_WebCamTexture.Pause();
Debug.Log("暂停摄像头!");
ShowInfoLog("暂停摄像头!");
}
//停止
public void StopWebCam()
{
_WebCamTexture.Stop();
rawImage.GetComponent<RawImage>().texture = null;
Debug.Log("停止摄像头!");
ShowInfoLog("停止摄像头!");
}
//拍照,存贮照片在本地
public void Photograph()
{
StartCoroutine("GetTexture2D");//获取摄像头图像,并把图片保存在本地
Debug.Log("拍照!");
ShowInfoLog("拍照!");
print(GameObject.Find("CameraTexture").transform.localPosition);
}
//获取摄像头图像,并把图片保存在本地
IEnumerator GetTexture2D()
{
print(GameObject.Find("CameraTexture").transform.localPosition);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(400,400);//新,实例化一张空白长400,宽400的图片,
t.ReadPixels(new Rect(240,416,400f,400f),0,0,false);//新空白图片的像素为,这个区域中的像素(摄像头在屏幕中的区域的图像),
t.Apply();//把像素应用在图片
//把图片数据转换为byte数组
byte[] byt = t.EncodeToPNG();
//然后保存为图片
string strTempTime = System.DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);//当前时间,把图片命名为当前时间
System.IO.File.WriteAllBytes(Application.dataPath + "/Resources/" + strTempTime + ".jpg", byt);//把图片保存在本地,Resources文件夹中,名字为当前时间.jpg
//System.IO.File.WriteAllBytes(Application.persistentDataPath+"/"+strTempTime + ".jpg", byt);//把图片保存在本地,文件夹中,名字为当前时间.jpg
UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的图片立刻导入。接下来才可以被使用
image.material.mainTexture= Resources.Load(strTempTime, typeof(Texture2D))as Texture2D;//把拍的照片显示出来;
Debug.Log("照片保存在本地!");
ShowInfoLog("照片保存在本地!");
}
//显示日志提示信息
void ShowInfoLog(string info)
{
debugInfo.text = "";
infoLog += info;
infoLog += "\r\n";
}
}