Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

ARFoundation Camera API not working on Android?

Discussion in 'Handheld AR' started by GuyInfinityAR, Apr 18, 2019.

  1. GuyInfinityAR

    GuyInfinityAR

    Joined:
    Nov 1, 2017
    Posts:
    6
    Hi guys,

    I am trying to synchronically grab the camera frame using the Camera API but the following fails and then never gets fired again, even though the AR features keep on working:


    Code (CSharp):
    1. var cameraSubsystem = ARSubsystemManager.cameraSubsystem;
    2.  
    3. CameraImage image;
    4. if (!cameraSubsystem.TryGetLatestImage(out image))
    5. {
    6.        Debug.Log("Gets here once and then this event never gets fire again in the session");
    7.        return;
    8. }
    Am I missing something or might there be an issue? (I am running my apk on a Pixel 2 device, and building with Unity 2019.1.0f2).

    Thanks!
     
    Last edited: Apr 18, 2019
  2. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    543
    Are you disposing of the CameraImage?
     
  3. GuyInfinityAR

    GuyInfinityAR

    Joined:
    Nov 1, 2017
    Posts:
    6
    Had that line of code been reached - then yes. But the event is only fired once, the "TryGetLatestImage" returns false and the event is not fired again even through I have not unregistered from it.
    (I am returning from the method scope just like the example. I have just edited the original post to state that).
     
  4. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    543
    That line of code does not dispose the CameraImage. Can you post the code you are using?

    To what event are you subscribed?
     
  5. GuyInfinityAR

    GuyInfinityAR

    Joined:
    Nov 1, 2017
    Posts:
    6
    This is just a part of the whole code which was basically based on the sample code for this API so the dispose was not the problem.

    I just had another line disabling the rest of the scope from running. Once removed:

    it does work
    but there is one thing - it seems the first frame might be empty? Although the buffer length is a valid number and the code runs, once I encode the Texture2D - only the first actual image file is 0 bytes long (thus unreadable). The consecutive runs work as expected producing a valid buffer with actual image data.
     
  6. dave-loper

    dave-loper

    Joined:
    Nov 10, 2017
    Posts:
    6
    ARSubsystemManager.cameraSubsystem.TryGetLatestImage(out image)

    returns false. I tried simply the example of "Synchronously convert to color" given here:
    https://docs.unity3d.com/Packages/c...#synchronously-convert-to-grayscale-and-color

    I just replaced

    if (!cameraSubsystem.TryGetLatestImage(out image))
    return;

    with

    if (!cameraSubsystem.TryGetLatestImage(out image))
    {
    Debug.LogError("getting latest image failed again");
    image.Dispose();

    /*// image is a struct, so it's never null. Probably it only needs to be disposed if it has properly been used which would explain why they didn't dispose it in the example code. Still, since it might be that trying-to-get-the-latest-image fails the first couple of times and would after that work, I'd still like to make sure it doesn't fail because there are too many image instances Documentation says : The CameraImage is a struct which represents a native resource. When you are finished using it, you must call Dispose on it to release it back to the system. Although you may hold a CameraImage for multiple frames, most platforms have a limited number of them, so failure to Dispose them may prevent the system from providing new camera images. //*/
    return;
    }
    Debug.Log("getting latest image succeeded for once");

    And I get spammed with the error log, and i never get past that.
    This means the event gets called over and over, but the attempt to get the latest image just always fails.
    Running it on iOS, so i think it has nothing to do with android. And the AR experience works other than that (tracked pose driver, ar camera background, ar plane manager, ar point cloud manager), it's just getting the camera image to CPU that fails, returning false.

    Any help or hints are much appreciated =)



    edit: it actually works on android for me, but not on iOS
     
    Last edited: May 9, 2019
  7. dave-loper

    dave-loper

    Joined:
    Nov 10, 2017
    Posts:
    6
    I got it working playng around with build settings
     
  8. icyuan

    icyuan

    Joined:
    Jul 21, 2019
    Posts:
    2
    how to solve it