Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by gregacuna · Aug 13, 2017 at 08:44 AM · androidioswebcamtexturewebcam

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:

http://answers.unity3d.com/questions/773464/webcamtexture-correct-resolution-and-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:

 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

196 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges