ZoeRobotIngles/Assets/Scripts/TakePicture/TakePhoto.cs

122 lines
3.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class TakePhoto : MonoBehaviour
{
WebCamTexture webCamTexture;
bool camAvailable;
Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
// Start is called before the first frame update
void Awake()
{
/*webCamTexture = new WebCamTexture();
GetComponent<SpriteRenderer>().material.mainTexture = webCamTexture; //Add Mesh Renderer to the GameObject to which this script is attached to
webCamTexture.Play();*/
defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
{
camAvailable = false;
return;
}
webCamTexture = new WebCamTexture(devices[0].name, Screen.width, Screen.height);
webCamTexture.Play();
background.texture = webCamTexture;
//CorTakePhoto();
camAvailable = true;
}
private void Update()
{
if (camAvailable)
return;
float ratio = (float)webCamTexture.width / (float)webCamTexture.height;
fit.aspectRatio = ratio;
float scaleY = webCamTexture.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -webCamTexture.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
}
public bool response = false;
public void CorTakePhoto() {
StartCoroutine(takePhoto());
}
public IEnumerator takePhoto() // Start this Coroutine on some button click
{
// NOTE - you almost certainly have to do this here:
byte[] photo1, photo2, photo3;
// it's a rare case where the Unity doco is pretty clear,
// http://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
// be sure to scroll down to the SECOND long example on that doco page
Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
photo.SetPixels(webCamTexture.GetPixels());
photo.Apply();
//Encode to a PNG
photo1 = photo.EncodeToPNG();
yield return new WaitForSecondsRealtime(.5f);
photo = new Texture2D(webCamTexture.width, webCamTexture.height);
photo.SetPixels(webCamTexture.GetPixels());
photo.Apply();
photo2 = photo.EncodeToPNG();
yield return new WaitForSecondsRealtime(.5f);
photo = new Texture2D(webCamTexture.width, webCamTexture.height);
photo.SetPixels(webCamTexture.GetPixels());
photo.Apply();
photo3 = photo.EncodeToPNG();
WWWForm form = new WWWForm();
form.AddBinaryData("foto", photo1);
form.AddBinaryData("foto2", photo2);
form.AddBinaryData("foto3", photo3);
string url = "https://zoe.myrobotlab.com.br/api/alexa/recognizeFace/asd123dsa654w789";
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
www.downloadHandler = new DownloadHandlerBuffer();
www.certificateHandler = new LoginAPI.BypassCertificate();
yield return www.SendWebRequest();
Debug.Log(www.downloadHandler.text);
Text txt = GameObject.Find("TextTeste").GetComponent<Text>();
txt.text = www.downloadHandler.text;
if (www.isNetworkError || www.isHttpError)
{
}
else
{
}
}
}
}