Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webcamTexture rotation differs in unity3d under Android

I am having a difficulty rotating the webcamtexture in my Android device.

Here is my scene in the editor: enter image description here

and here is the image in my phone: enter image description here

You can see the difference in rotation and scaling between the phone and the editor.

Here is the code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Camera_pnl : MonoBehaviour {

    //// Use this for initialization
    WebCamTexture webCameraTexture;

    void Start() {
        GUITexture BackgroundTexture = gameObject.AddComponent<GUITexture>();
        BackgroundTexture.pixelInset = new Rect(0,0,Screen.width,Screen.height);
        WebCamDevice[] devices = WebCamTexture.devices;

        foreach (WebCamDevice cam in devices)
        {
            if (cam.isFrontFacing )
            {    
                webCameraTexture  = new WebCamTexture(cam.name);
                webCameraTexture.deviceName  = cam.name;
                webCameraTexture.Play();
                BackgroundTexture.texture = webCameraTexture;
            }
        }
    }

    void Update () {
        if (Input.GetKey (KeyCode.Escape)) {
            Invoke("LoadGame",0.1f);
        }
    }

    void LoadGame ()
    {
        webCameraTexture.Stop ();
        webCameraTexture = null;
        Application.LoadLevelAsync("Game");
    }
}

How can I fix this problem so my phone displays the correct rotation of the webcamtexture ?

like image 642
Sora Avatar asked Mar 08 '15 13:03

Sora


1 Answers

You need to look at the rotate direction and then rotate the preview of the captured input, this can be done by rotating the preview in realtime.

int rotAngle = -webCamTexture.videoRotationAngle;
while( rotAngle < 0 ) rotAngle += 360;
while( rotAngle > 360 ) rotAngle -= 360;

Following code is from Camera Capture Kit a plugin that we use to (https://www.assetstore.unity3d.com/en/#!/content/56673) - we use it for a social game/app to let the user take and share photos inside the game.

You need to flip the image as well when the rotation is 270 or 90 degrees in some cases. This is the code for Android.

if( Application.platform == RuntimePlatform.Android ) {
            flipY = !webCamTexture.videoVerticallyMirrored; }

You need to take these factors into consideration when you render the preview of the webcamtexture as well as when you get the pixels, the pixels in the image will have to be rotated as well if you want to save and share the image, that is a costly process to do frame by frame in the preview, that is why you need to do it after the photo has been taken once.

Cheers

like image 195
Chris Avatar answered Oct 19 '22 11:10

Chris