using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class panel5 : MonoBehaviour
{
//拍照按钮
public Button _btnCamera;
//倒计时5秒的文本
public GameObject text;
//设置倒计时5秒
public int TotalTime = 5;
//调用相机拍照
//public string deviceName;
//接收返回的图片数据
public WebCamTexture tex;
// public Texture2D _tex;
public RawImage webrawimage;
//复选框
private Toggle[] toggles;
private int btnIdx1 = -1;
void Start()
{
_btnCamera.onClick.AddListener(OnClickPhot);
//找到所有的toggles
toggles = transform.GetComponentsInChildren<Toggle>();
//给toggle添加事件
for (int i = 0; i < toggles.Length; i++)
{
//这一步是必须记录的,用来区分那个toggle
int a = i;
toggles[a].onValueChanged.AddListener((bool value) => SetEveryToggle(value, a));
}
StartCoroutine(StartCamera(true));
}
//点击拍照按钮开始倒计时
public void OnClickPhot()
{
_btnCamera.gameObject.SetActive(false);
text.SetActive(true);
StartCoroutine(CountDown());
}
//5秒倒计时
IEnumerator CountDown()
{
while (TotalTime >= 0)
{
text.GetComponent<Text>().text = TotalTime.ToString();
yield return new WaitForSeconds(1);
TotalTime--;
}
//for (int TotalTime = 5; TotalTime > -1; TotalTime--)
//{
// Debug.Log(TotalTime.ToString());
// text.GetComponent<Text>().text = TotalTime.ToString();
// yield return new WaitForSeconds(1f);
//}
Texture2D texture2D = CaptureCamera(Camera.main, new Rect(0, 0, 1920, 1080));
StopCamera();
}
private void Update()
{
if (TotalTime < 0)
{
text.SetActive(false);
}
}
void OnGUI()
{
if (tex != null)
{
webrawimage.texture = tex;
}
}
/// <summary>
/// 捕获窗口位置
/// </summary>
/// <param name="webimage"> 图片</param>
/// <param name="chooseimage">选择的图片</param>
/// <param name="isOK">前后摄像头</param>
/// <returns></returns>
public IEnumerator StartCamera(bool isOK)
{
//webrawimage = webimage;
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (tex == null)
tex = new WebCamTexture();
WebCamDevice[] devices = WebCamTexture.devices;
for (int i = 0; i < WebCamTexture.devices.Length; i++)
{
//如果是前置摄像机
if (WebCamTexture.devices[i].isFrontFacing && isOK)
{
tex.deviceName = WebCamTexture.devices[i].name;
break;
}
//如果是后置摄像机
else if (!WebCamTexture.devices[i].isFrontFacing && !isOK)
{
tex.deviceName = WebCamTexture.devices[i].name;
break;
}
}
//webrawimage.texture = tex;
tex.Play();
}
}
///停止拍照
public void StopCamera()
{
if (tex != null)
tex.Stop();
}
/// <summary>
/// 获取截图
/// </summary>
/// <returns>The texture.</returns>
public IEnumerator GetTexture(System.Action<Texture2D, byte[]> act)
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(tex.width, tex.height, TextureFormat.RGB24, false);
for (int i = 0; i < tex.height; i++)
for (int j = 0; j < tex.width; j++)
{
t.SetPixel(i, j, tex.GetPixel(j, i));
}
t.Apply();
byte[] byt = t.EncodeToPNG();
tex.Play();
act(t, byt);
}
Texture2D CaptureCamera(Camera camera, Rect rect)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张照片: {0}", filename));
return screenShot;
}
/// <summary>
/// 连续捕获照片
/// </summary>
/// <returns>The photoes.</returns>
public IEnumerator SeriousPhotoes()
{
while (true)
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(400, 300, TextureFormat.RGB24, true);
t.ReadPixels(new Rect(Screen.width / 2 - 180, Screen.height / 2 - 50, 360, 300), 0, 0, false);
t.Apply();
print(t);
byte[] byt = t.EncodeToPNG();
System.Threading.Thread.Sleep(300);
}
}
//五星打分
void SetEveryToggle(bool value, int _index)
{
if (btnIdx1 >= 0)
{
//ChangeButtonUI(btn1_list[btnIdx1], white1_texture2d);
}
//if (_index == 0 && value)
//{
// Debug.Log("第一个toggle");
//}
//if (_index == 1 && value)
//{
// Debug.Log("第二个toggle");
//}
//if (_index == 2 && value)
//{
// Debug.Log("第三个toggle");
//}
}
}