Search Unity

Question how can I take pictures with AR Camera and save images? (android)

Discussion in 'AR' started by gyeongyun, Sep 9, 2022.

  1. gyeongyun

    gyeongyun

    Joined:
    Jun 19, 2021
    Posts:
    8
    I am developing an AR Camera app (android) using Unity AR Foundation, how can I take pictures with AR Camera and save images?

    If you click the button with the code below, I even took a screenshot of the screen, but the image file is not stored in a folder in the path you set. What should I do?

    Code (CSharp):
    1. public Camera camera;  
    2.  
    3. private int resWidth;
    4. private int resHeight;
    5. string path;
    6.  
    7. void Start()
    8. {
    9.     resWidth = Screen.width;
    10.     resHeight = Screen.height;
    11.     path = Application.persistentDataPath;
    12.     Debug.Log(path);
    13. }
    14.  
    15. public void ClickScreenShot()
    16. {
    17.     DirectoryInfo dir = new DirectoryInfo(path);
    18.     if (!dir.Exists)
    19.     {
    20.         Directory.CreateDirectory(path);
    21.     }
    22.     string name;
    23.     name = path + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
    24.     RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    25.     camera.targetTexture = rt;
    26.     Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    27.     Rect rec = new Rect(0, 0, screenShot.width, screenShot.height);
    28.     camera.Render();
    29.     RenderTexture.active = rt;
    30.     screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    31.     screenShot.Apply();
    32.  
    33.     byte[] bytes = screenShot.EncodeToPNG();
    34.     File.WriteAllBytes(name, bytes);
    35. }
    36.  
     
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,051