Search Unity

When I am using camera in my ipad it looks inverted?Trying to take a picture?

Discussion in 'Scripting' started by zyonneo, Aug 30, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi, I am trying to take a picture and save it in a persistent path.When I tried the following code,it looks inverted and less clear when compared to ipad's normal camera.After some posts in stackoverflow suggestion was to add rotation.I added that but no effect.

    Here are the reference links -https://stackoverflow.com/questions/24496438/can-i-take-a-photo-in-unity-using-the-devices-camera
    https://docs.unity3d.com/ScriptReference/WebCamTexture-videoRotationAngle.html
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using System.IO;
    7. using UnityEngine.Networking;
    8.  
    9. public class TakePhoto : MonoBehaviour
    10. {
    11.  
    12.  
    13.     WebCamTexture webCamTexture;
    14.     public Quaternion baseRotation;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         webCamTexture = new WebCamTexture();
    19.         baseRotation = transform.rotation;
    20.         webCamTexture.Play();
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         GetComponent<RawImage>().texture = webCamTexture;
    28.  
    29.  
    30.  
    31.     }
    32.  
    33.  
    34.     public void CaptureSnap()
    35.     {
    36.         StartCoroutine(UploadPNG());
    37.     }
    38.  
    39.  
    40.  
    41.     IEnumerator UploadPNG()
    42.     {
    43.         // We should only read the screen buffer after rendering is complete
    44.         yield return new WaitForEndOfFrame();
    45.  
    46.  
    47.         // Create a texture the size of the screen, RGB24 format
    48.         int width = Screen.width;
    49.         int height = Screen.height;
    50.         Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    51.  
    52.         // Read screen contents into the texture
    53.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    54.         tex.Apply();
    55.  
    56.         // Encode texture into PNG
    57.         byte[] bytes = tex.EncodeToPNG();
    58.         Destroy(tex);
    59.  
    60.         // For testing purposes, also write to a file in the project folder
    61.         File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
    62.  
    63.      
    64.  
    65.     }
    66.  
    67.  
    68. }
    69.  
     
    Last edited: Aug 30, 2019