How do you take multiple photos using Webcamtexture and rotate it to show it as smartphone rotation?
Hi, good evening.
I was trying to make an app to take 5 photos with a counter for Android. But it closes by itself without errors taking a photo. And it shows the camera tilted 90º. I tried use this that I found on the documentation, but it doesn't work
transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);
It would be like this for my code, but it doesn't work
imageObj.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);
I'm not understanding how the webcam works, it freezes and then tries to take another one, but it freezes and closes, but there's a saved photo.
I have this code:
public class MultiShot : MonoBehaviour
{
public Button SwitchBtn;
public Button CaptureBtn;
public Dropdown CounterDpd;
public Text CounterTxt;
private float seconds = 3;
private float counter = 3;
private bool started = false;
private bool[] counters = {false,false,false,false, false};
private int cCounter = 0;
public GameObject imageObj;
private WebCamTexture webcamTexture;
private WebCamDevice[] devices;
private Resolution[] resolutions;
private int device = 0;
private System.DateTime now;
private string capName = "Photo";
IEnumerator Start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
devices = WebCamTexture.devices;
SwitchBtn.onClick.AddListener(SwitchCamera);
CaptureBtn.onClick.AddListener(CaptureCamera);
resolutions = devices[device].availableResolutions;
webcamTexture = new WebCamTexture(devices[device].name, resolutions[0].width, resolutions[0].height, resolutions[0].refreshRate);
webcamTexture.Play();
imageObj.GetComponent<RawImage>().texture = webcamTexture;
}
else
{
Application.Quit();
}
}
void SwitchCamera()
{
if ((device+1) < devices.Length)
{
device++;
}
else
{
device = 0;
}
webcamTexture.Stop();
webcamTexture = new WebCamTexture(devices[device].name, resolutions[0].width, resolutions[0].height, resolutions[0].refreshRate);
if (webcamTexture != null)
{
webcamTexture.Play();
imageObj.GetComponent<RawImage>().texture = webcamTexture;
}
}
void CaptureCamera()
{
switch(CounterDpd.value)
{
case 0:
seconds = 3;
break;
case 1:
seconds = 5;
break;
case 2:
seconds = 10;
break;
case 3:
seconds = 15;
break;
}
started = true;
cCounter = 0;
counters[cCounter] = true;
counter = seconds;
CounterTxt.gameObject.SetActive(true);
CounterTxt.text = Mathf.RoundToInt(counter).ToString();
now = System.DateTime.Now;
}
IEnumerator TakeSnapshot(string name)
{
yield return new WaitForEndOfFrame();
Texture2D snap = new Texture2D(webcamTexture.width, webcamTexture.height);
snap.SetPixels(webcamTexture.GetPixels());
snap.Apply();
string date = now.Year.ToString() + now.Day.ToString() + now.Month.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
captured[cCounter] = Application.persistentDataPath + "/" + name + cCounter.ToString() + date + ".png";
File.WriteAllBytes(captured[cCounter], snap.EncodeToPNG());
}
}
void Update()
{
if(started && cCounter < counters.Length && counters[cCounter])
{
if (counter > 0f)
{
counter -= Time.deltaTime;
CounterTxt.text = Mathf.RoundToInt(counter).ToString();
}
else
{
counters[cCounter] = false;
StartCoroutine(TakeSnapshot(capName));
cCounter++;
if (cCounter < counters.Length)
{
counters[cCounter] = true;
counter = seconds;
CounterTxt.text = Mathf.RoundToInt(counter).ToString();
}
else
{
started = false;
CounterTxt.gameObject.SetActive(false);
}
}
}
}
}
I tried using void instead IEnumerator, and make it wait 2 seconds, if webcamtexture is playing, but it doesn't work. Have a nice day
Your answer
Follow this Question
Related Questions
Microsoft HoloLens Emulator - Callback for PhotoCapture.CreateAsync not being called 1 Answer
slowly fade out 360 photo without also fading the UI inside 0 Answers
Is it possible to show a webCam live feed and Video at the same time? 0 Answers
Hololens 2 WebcamTexture not working in XAML builds 0 Answers
Windowed Unity program. Getting Color from Desktop. 0 Answers