Search Unity

WebCam Texture Rotation

Discussion in 'Android' started by gregacuna, Aug 13, 2017.

  1. gregacuna

    gregacuna

    Joined:
    Jun 25, 2015
    Posts:
    59
    Our "Acting Kindly" mobile game/app allows users to take selfies and share them to Facebook. The intern who wrote the WebCam.cs script didn't add functionality so the camera image rotates with the orientation of the phone. I'm only a beginner when it comes to C# and having trouble figuring out how to do this. I found one post which seems to have a script to do it...but now sure. Here's that post:

    http://answers.unity3d.com/question...ratio.html?childToView=1148424#answer-1148424

    I have two basic problems:

    1) Android... the script isn't rotating the camera image when the phone is rotated (to upside down from original position from when it is started) so the image both shows on the camera and posts to facebook as upside-down.

    2) iOS...the image is always upside down on the phone compared to what posts. So if it looks right side up...it posts upside down and if it is upside down then it posts right side up.

    Here is the script being used that causes the above behavior:

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.IO;
    5. using HutongGames.PlayMaker;
    6. using System;
    7. public class WebCam : MonoBehaviour {
    8.      int currentCamIndex = 0;
    9.      WebCamTexture tex;
    10.      public RawImage currentImage;
    11.      public RawImage previewImage;
    12.      //public Text filePath;
    13.      public Text startStopText;
    14.      //stores users full name
    15.      private string username = "fUserName";
    16.      //image file will be saved with following extension
    17.      private string pngExtention = ".png";
    18.      public Text debug;
    19.      public DeviceOrientation myDeviceOrientation;
    20.      private Quaternion quatRotation;
    21.      public void StartStopCamera() {
    22.          if (tex != null) { // stop the camera
    23.              StopWebCam();
    24.          } else { // Start the camera
    25.              StartWebCam();
    26.          }
    27.      }
    28.      public void Start() {
    29.          GetLatestDeviceOrientation ();
    30.          //Default Rotationf of RawImage os 0
    31.          quatRotation = new Quaternion();
    32.          quatRotation.eulerAngles = new Vector3 (0, 0, 0);
    33.      }
    34.      public void SwitchCamera(){
    35.          if (WebCamTexture.devices.Length > 0) {
    36.              currentCamIndex += 1;
    37.              currentCamIndex %= WebCamTexture.devices.Length;
    38.              //filePath.text = currentCamIndex.ToString ();
    39.              //if texture is not null then stopcamera and start again
    40.              if (tex != null) {
    41.                  StopWebCam ();
    42.                  StartStopCamera ();
    43.              }
    44.          }
    45.      }
    46.      public void StopWebCam() {
    47.          if (tex!=null && tex.isPlaying) {
    48.              currentImage.texture = null;
    49.              tex.Stop ();
    50.              tex = null;
    51.          }
    52.      }
    53.      private void StartWebCam() {
    54.          WebCamDevice device = WebCamTexture.devices [currentCamIndex];
    55.          tex = new WebCamTexture (device.name);
    56.          currentImage.texture = tex;
    57.          currentImage.transform.rotation = quatRotation;
    58.          tex.Play ();
    59.      }
    60.      public void SaveImage ()
    61.      {
    62.          try {
    63.              SetDebugText ("Save image started");
    64.    
    65.              if (tex != null) {
    66.                  Texture2D tempTex = new Texture2D (tex.width, tex.height, TextureFormat.ARGB32, false);
    67.                  tempTex.SetPixels (tex.GetPixels ());
    68.                  tempTex.Apply ();
    69.                  SetDebugText ("before ");
    70.                  FsmString fUserName = FsmVariables.GlobalVariables.FindFsmString (username);
    71.                  SetDebugText ("after " + fUserName.Value);
    72.                  //FsmString fUserName = new FsmString ("SarthakShah");
    73.                  //fUserName.Value = "SarthakShah";
    74.                  System.IO.File.WriteAllBytes (Application.persistentDataPath + "/" + fUserName.Value + pngExtention, tempTex.EncodeToPNG ());
    75.                  byte[] imgData = File.ReadAllBytes (Application.persistentDataPath + "/" + fUserName.Value + pngExtention);
    76.                  Texture2D imgtex = new Texture2D (tex.width, tex.height);
    77.                  imgtex.LoadImage (imgData);
    78.                  previewImage.texture = imgtex;
    79.            
    80.              SetDebugText ("Save image finished");
    81.              SetDebugText (Application.persistentDataPath + "/" + fUserName.Value + pngExtention);
    82.              }
    83.          } catch (Exception e) {
    84.              debug.text = e.ToString ();
    85.              //username;
    86.              Debug.Log (e.ToString ());
    87.              Debug.Log (username);
    88.          }
    89.      }
    90.      public Texture2D FillnClear(Texture2D tex2D, Color whatToFillWith){
    91.          for (int i = 0; i < tex2D.width; i++) {
    92.              for (int j = 0; j < tex2D.width; j++) {
    93.                  if (tex2D.GetPixel (i, j) == Color.clear)
    94.                      tex2D.SetPixel (i, j, whatToFillWith);
    95.              }
    96.          }
    97.          return tex2D;
    98.      }
    99.      private void GetLatestDeviceOrientation() {
    100.          myDeviceOrientation = Input.deviceOrientation;
    101.      }
    102.      public void Update(){
    103.          float z = -90;
    104.          GetLatestDeviceOrientation ();
    105.          // Check if you are facing rare camera
    106.          if (currentCamIndex == 0) {
    107.              // rare camera is activated
    108.              //filePath.text = "Rear camera is activate";
    109.              switch (myDeviceOrientation) {
    110.              case DeviceOrientation.Portrait:
    111.              //    filePath.text = " P Rear camera is activate ";
    112.                  z = -90;
    113.                  quatRotation.eulerAngles = new Vector3 (0, 0, z);
    114.                  break;
    115.              case DeviceOrientation.LandscapeLeft:
    116.              //    filePath.text = " L L Rear camera is activate ";
    117.                  z = 0;
    118.                  quatRotation.eulerAngles = new Vector3 (0, 0, z);
    119.                  break;
    120.              case DeviceOrientation.LandscapeRight:
    121.                  //filePath.text = " L R Rear camera is activate ";
    122.                  break;
    123.              }
    124.          } else if (currentCamIndex == 1) {
    125.              // front camera is activated
    126.              //filePath.text = "Front camera is activated";
    127.          } else {
    128.              // not sure which camera
    129.              //filePath.text = "Not sure which camera";
    130.          }
    131.          //quatRotation.eulerAngles = new Vector3 (0, 0, z);
    132.      }
    133.      private void SetDebugText(string debugMessage){
    134.          if (debug != null) {
    135.              debug.text = debugMessage;
    136.          }
    137.      }
    138. }
    Could I integrate the code from the post link above or is there a simple way to make the image show right side up on the camera display and save the same so the post is also right?

    Thanks, Greg