markdown Unity - WebCam

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Unity - WebCam相关的知识,希望对你有一定的参考价值。

Resolution
``` c#
public int ResolutionWidth = 1280;
public int ResolutionHeight = 720;
```
Initializaing the web cam
``` c#
private void InitializeCamera()
{
	// config webcam
	_webcamtex = new WebCamTexture(ResolutionWidth, ResolutionHeight, 30);
	// show camera texture
	FrameImage.material.mainTexture = camTexture;
    camTexture.Play();
}
```

Pause Camera
```c#
private void PauseCamera()
{
	_webcamtex.Pause();
  _photoUi.HidePhotoMask();
}
```

Restart
``` c#
private void RestartCamera()
{
	_webcamtex.Play();
	_photoUi.ShowPhotoMask();
}
```

Stop
``` c#
private void StopCamera()
{
	_webcamtex.Stop();
}
```

Capture Photo
``` c#
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
``` c#
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";
}
```

以上是关于markdown Unity - WebCam的主要内容,如果未能解决你的问题,请参考以下文章

markdown Unity2019HubAutoAndroidPath.md

markdown Unity - 加载图像

markdown Unity Life Jacket

markdown Unity - 文件夹中的文件

FFmpeg 播放 RTSP/Webcam 流

markdown 从Unity中的材质/脚本控制固定功能状态