- Home /
How to get best resolution from phone camera WebCamTexture?
I'm writing a camera application in Unity with uGUI. I use the WebCamTexture with uGUI's RawImage like so
WebCamTexture webcam = new WebCamTexture();
webcam.Play();
rawImage.texture = webcam;
rawImage.SetNativeSize();
Now to get its data I used
public byte[] GetPNG()
{
Texture2D tex = new Texture2D(webcam.width,webcam.height);
tex.SetPixels(webcam.GetPixels());
tex.Apply();
return tex.EncodeToPNG();
}
But I noticed the resolution is very very low. Like 640x480. Even if I set request size on
WebCamTexture webcam = new WebCamTexture(10000,10000,30);
I debugged webcam
width and height and it is still like 1920x1080 while my Android phone is capable for taking more megapixels. And not to mention it is VERY sluggish when I requested 10000 10000 (and got back only 1920x1080) maybe this is a bug? When I requested precisely 1920x1080 it is not laggy. (but still bad resolution)
This is camera app developed with Unity. Very low resolution.
This is the native android camera app. Notice that the preview image is much more sharper, which is reflected in quality of resulting file too.
Unfortunately no, and I already stopped development of this app so I didn't inspect further...
Answer by Fattie · Feb 29, 2016 at 12:31 AM
Regarding this ridiculous problem caused by unity ...
@Fattie Hi there, Do you know if I always need both .texture
and .mainTexture
assignments when working with a WebCamTexture
? WebCamTexture webcamTexture = new WebCamTexture();
rawimage.texture = webcamTexture;
<===this rawimage.material.mainTexture = webcamTexture;
<===and this webcamTexture.Play();
Hello @Fattie I am still getting the same issue where the image will be written to my phone to a resolution of 640x480. I have tried the script that you provided but it still doesn't seem to solve my problem.
public Texture2D TakePhoto() // Start this Coroutine on some button click
{
rotationVector.z = -activeCameraTexture.videoRotationAngle;
image.rectTransform.localEulerAngles = rotationVector;
// Set AspectRatioFitter's ratio
float videoRatio =
(float)activeCameraTexture.width / (float)activeCameraTexture.height;
imageFitter.aspectRatio = videoRatio;
// Unflip if vertically flipped
image.uvRect =
activeCameraTexture.videoVertically$$anonymous$$irrored ? fixedRect : defaultRect;
imageParent.localScale =
activeCameraDevice.isFrontFacing ? fixedScale : defaultScale;
string name = string.Format("{0}_Capture{1}.png", Application.productName, System.DateTime.Now.ToString("yyyy-$$anonymous$$$$anonymous$$-dd_HH-mm-ss"));
// Take photo and save it to persistent data folder
Texture2D photo = new Texture2D(activeCameraTexture.width, activeCameraTexture.height);
photo.SetPixels(activeCameraTexture.GetPixels());
photo.Apply();
byte[] bytes = photo.EncodeToPNG();
//File.WriteAllBytes(Application.persistentDataPath, bytes);
NativeGallery.SaveImageToGallery(bytes, Application.productName + " Captures", name);
return photo;
}
// Set the device camera to use and start it
public void SetActiveCamera(WebCamTexture cameraToUse)
{
if (activeCameraTexture != null)
{
activeCameraTexture.Stop();
}
activeCameraTexture = cameraToUse;
activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device =>
device.name == cameraToUse.deviceName);
image.texture = activeCameraTexture;
image.material.mainTexture = activeCameraTexture;
activeCameraTexture.Play();
}
void Update()
{
// Skip making adjustment for incorrect camera data
if (activeCameraTexture.width < 100)
{
Debug.Log("Still waiting another frame for correct info...");
return;
}
rotationVector.z = -activeCameraTexture.videoRotationAngle;
image.rectTransform.localEulerAngles = rotationVector;
float videoRatio =
(float)activeCameraTexture.width / (float)activeCameraTexture.height;
imageFitter.aspectRatio = videoRatio;
image.uvRect =
activeCameraTexture.videoVertically$$anonymous$$irrored ? fixedRect : defaultRect;
// $$anonymous$$irror front-facing camera's image horizontally to look more natural
imageParent.localScale =
activeCameraDevice.isFrontFacing ? fixedScale : defaultScale;
}
}