Resolution
public int ResolutionWidth = 1280;
public int ResolutionHeight = 720;
Initializaing the web cam
private void InitializeCamera()
{
// config webcam
_webcamtex = new WebCamTexture(ResolutionWidth, ResolutionHeight, 30);
// show camera texture
FrameImage.material.mainTexture = camTexture;
camTexture.Play();
}
Pause Camera
private void PauseCamera()
{
_webcamtex.Pause();
_photoUi.HidePhotoMask();
}
Restart
private void RestartCamera()
{
_webcamtex.Play();
_photoUi.ShowPhotoMask();
}
Stop
private void StopCamera()
{
_webcamtex.Stop();
}
Capture Photo
private IEnumerator CaptureTextureAsPNG()
{
Debug.Log("Saving photo");
yield return new WaitForEndOfFrame();
Texture2D textureFromCamera = _photoUi.GetCameraFrame();
textureFromCamera.SetPixels(_photoUi.GetCameraPixels());
textureFromCamera.Apply();
byte [] bytes = textureFromCamera.EncodeToPNG();
SavePhoto(bytes);
}
Save Photo
private void SavePhoto(byte[] bytes)
{
// creating the folder
if (!Directory.Exists(_filePath))
{
Directory.CreateDirectory(_filePath);
}
Debug.Log("Saving file at: " + _filePath);
// return Filename
_fileName = GetUniqueFileName();
_fileFullPath = $"{_filePath}{_fileName}";
File.WriteAllBytes(_fileFullPath, bytes);
if (_onPhotoTaken != null)
{
Photo photo = new Photo(
_fileName, _fileFullPath, _filePath, bytes,
ResolutionWidth, ResolutionHeight);
_onPhotoTaken(photo);
}
}
private string GetUniqueFileName()
{
DateTime time = DateTime.Now;
string timeStr = time.ToLongDateString();
return $"photo{timeStr}.png";
}