简体   繁体   中英

How to take & save picture / screenshot using Unity ARCore SDK?

I found the answer to how to do it natively here but Im running Unity.

How to take picture with camera using ARCore

I'm not sure how to access the Unity surface renderer thread to be able to drop in those functions.

This looks like a good start. Ideas?

Edit:

Using Texture2d ReadPixels or ScreenCapture.CaptureScrenshot are not viable as they are blocking the render thread. The code below is enough to block the thread.

StartCoroutine(TakeScreenshot());

private IEnumerator TakeScreenshot()
    {
        yield return new WaitForEndOfFrame();

        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

Edit 2: Am considering using this technique . Will report back if successful.

I found this repo on github which makes it super simple to use both native iOS and Android code to save a screenshot to the device. Technically you could just use the Texture2D capture code to get the screenshot, but the follow NativeGallery call allows you to write it to the device. The GitHub readme covers what settings you need to enable in order to allow writing to the device.

private void ScreenshotButtonClicked()
{
    StartCoroutine(TakeScreenshot());
}

private IEnumerator TakeScreenshot()
{
    SetUIActive(false);

    yield return new WaitForEndOfFrame();

    Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    ss.Apply();

    NativeGallery.SaveToGallery(ss, "Native Screenshots", DateTime.Now.ToString().Replace("/", "-"));

    SetUIActive(true);
}

private void SetUIActive(bool active)
{
    Button.gameObject.SetActive(active);
    ScaleSlider.gameObject.SetActive(active);
}

ARCore provides its own TextureReader class that is extremely fast and doesn't reproduce the side effects noted. TextureReader_acquireFrame

https://github.com/google-ar/arcore-unity-sdk/blob/master/Assets/GoogleARCore/Examples/ComputerVision/Scripts/TextureReaderApi.cs

Are you looking to learn how to do it? If you are just looking to capture a screenshot - it is built into Unity. See https://docs.unity3d.com/ScriptReference/Application.CaptureScreenshot.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM