WebCam Texture Rotation
Hi All...
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:
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:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using HutongGames.PlayMaker;
using System;
public class WebCam : MonoBehaviour {
int currentCamIndex = 0;
WebCamTexture tex;
public RawImage currentImage;
public RawImage previewImage;
//public Text filePath;
public Text startStopText;
//stores users full name
private string username = "fUserName";
//image file will be saved with following extension
private string pngExtention = ".png";
public Text debug;
public DeviceOrientation myDeviceOrientation;
private Quaternion quatRotation;
public void StartStopCamera() {
if (tex != null) { // stop the camera
StopWebCam();
} else { // Start the camera
StartWebCam();
}
}
public void Start() {
GetLatestDeviceOrientation ();
//Default Rotationf of RawImage os 0
quatRotation = new Quaternion();
quatRotation.eulerAngles = new Vector3 (0, 0, 0);
}
public void SwitchCamera(){
if (WebCamTexture.devices.Length > 0) {
currentCamIndex += 1;
currentCamIndex %= WebCamTexture.devices.Length;
//filePath.text = currentCamIndex.ToString ();
//if texture is not null then stopcamera and start again
if (tex != null) {
StopWebCam ();
StartStopCamera ();
}
}
}
public void StopWebCam() {
if (tex!=null && tex.isPlaying) {
currentImage.texture = null;
tex.Stop ();
tex = null;
}
}
private void StartWebCam() {
WebCamDevice device = WebCamTexture.devices [currentCamIndex];
tex = new WebCamTexture (device.name);
currentImage.texture = tex;
currentImage.transform.rotation = quatRotation;
tex.Play ();
}
public void SaveImage ()
{
try {
SetDebugText ("Save image started");
if (tex != null) {
Texture2D tempTex = new Texture2D (tex.width, tex.height, TextureFormat.ARGB32, false);
tempTex.SetPixels (tex.GetPixels ());
tempTex.Apply ();
SetDebugText ("before ");
FsmString fUserName = FsmVariables.GlobalVariables.FindFsmString (username);
SetDebugText ("after " + fUserName.Value);
//FsmString fUserName = new FsmString ("SarthakShah");
//fUserName.Value = "SarthakShah";
System.IO.File.WriteAllBytes (Application.persistentDataPath + "/" + fUserName.Value + pngExtention, tempTex.EncodeToPNG ());
byte[] imgData = File.ReadAllBytes (Application.persistentDataPath + "/" + fUserName.Value + pngExtention);
Texture2D imgtex = new Texture2D (tex.width, tex.height);
imgtex.LoadImage (imgData);
previewImage.texture = imgtex;
SetDebugText ("Save image finished");
SetDebugText (Application.persistentDataPath + "/" + fUserName.Value + pngExtention);
}
} catch (Exception e) {
debug.text = e.ToString ();
//username;
Debug.Log (e.ToString ());
Debug.Log (username);
}
}
public Texture2D FillnClear(Texture2D tex2D, Color whatToFillWith){
for (int i = 0; i < tex2D.width; i++) {
for (int j = 0; j < tex2D.width; j++) {
if (tex2D.GetPixel (i, j) == Color.clear)
tex2D.SetPixel (i, j, whatToFillWith);
}
}
return tex2D;
}
private void GetLatestDeviceOrientation() {
myDeviceOrientation = Input.deviceOrientation;
}
public void Update(){
float z = -90;
GetLatestDeviceOrientation ();
// Check if you are facing rare camera
if (currentCamIndex == 0) {
// rare camera is activated
//filePath.text = "Rear camera is activate";
switch (myDeviceOrientation) {
case DeviceOrientation.Portrait:
// filePath.text = " P Rear camera is activate ";
z = -90;
quatRotation.eulerAngles = new Vector3 (0, 0, z);
break;
case DeviceOrientation.LandscapeLeft:
// filePath.text = " L L Rear camera is activate ";
z = 0;
quatRotation.eulerAngles = new Vector3 (0, 0, z);
break;
case DeviceOrientation.LandscapeRight:
//filePath.text = " L R Rear camera is activate ";
break;
}
} else if (currentCamIndex == 1) {
// front camera is activated
//filePath.text = "Front camera is activated";
} else {
// not sure which camera
//filePath.text = "Not sure which camera";
}
//quatRotation.eulerAngles = new Vector3 (0, 0, z);
}
private void SetDebugText(string debugMessage){
if (debug != null) {
debug.text = debugMessage;
}
}
}
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
Your answer
Follow this Question
Related Questions
Facebook Audience Network conflicts with WebCamTexture 0 Answers
Android vs iOS build capacity 0 Answers
Unity Android camera crash 0 Answers
Read and save current git commit hash into script 1 Answer
WebCamDevice and WebCamTexture FPS 0 Answers