- Home /
How to get webcamtexture to fit into potrait size when camera on and when capturing screenshot?
Hi. I want my app to be able to use the front camera to take a photo and i sort of got the first part by using webcamtexture like so :
WebCamTexture cameraTexture;
public void StartCamera()
{
StartCoroutine(OpenCamera());
}
IEnumerator OpenCamera()
{
WebCamDevice[] devices = WebCamTexture.devices;
for (int i = 0; i < devices.Length; i++)
{
if (devices[i].isFrontFacing)
{
cameraTexture = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
}
}
cameraTexture.Play();
Debug.Log(cameraTexture.isPlaying);
yield return new WaitForEndOfFrame();
float ratio = (float)cameraTexture.width / (float)cameraTexture.height;
fit.aspectRatio = ratio;
PhotoPreview.GetComponentInChildren<RawImage>().texture = cameraTexture;
PhotoPreview.SetActive(true);
}
I've set the aspect ratio fitter in inspector as envelope parent and in editor the camera feed is a rectangle bigger than game view. So i manually changed the uv rect in script like :
PhotoPreview.GetComponentInChildren<RawImage>().uvRect = new Rect(0.4f, 0f, 1f, 1f);
then to take a screenshot like this :
cameraTexture.Stop();
//Create new Texture2D
Texture2D texture = new Texture2D(Screen.width, Screen.height);
//Read the pixels in the Rect starting at 0,0 and ending at the screen's width and height
texture.ReadPixels(new Rect(0.4f, 0f, Screen.width, Screen.height), 0, 0, false);
texture.Apply();
PhotoPreview.GetComponentInChildren<RawImage>().uvRect = new Rect(0f, 0f, 3.5f, 1f);
PhotoPreview.GetComponentInChildren<RawImage>().texture = texture;
yield return new WaitForEndOfFrame();
Here i also set the uv rect manually so then in scene view it looks like rectangles next to each other.
However, in gameview eveyrthing looks nice and proper but if i were to save the screenshot the image saved is distorted. Also on my phone which is a samsung, the camera feed is rotated 90 degrees to the left.
So, am i doing things correctly to get the camera feed from front facing camera? and how do i go about fixing the rotated camera feed?
Your answer
Follow this Question
Related Questions
How to get best resolution from phone camera WebCamTexture? 2 Answers
WebCamTexture, correct resolution and RATIO.And FPS 1 Answer
WebCamTexture deforms image 0 Answers
Could not connect pins - RenderStream() UnityEngine.WebCamTexture:Play() 0 Answers
problem accessing webcamtexture.videorotation angle on windows phone 8 0 Answers