1. Are you ready for the Galaxy S20? Here is everything we know so far!

Take photo in background - ARCore + Unity3D

Discussion in 'Android Development' started by Rich3030, Aug 8, 2020.

  1. Rich3030

    Rich3030 Lurker
    Thread Starter

    I have built an Android plugin (.AAR file) for a Unity3d Augmented Reality app. The Augmented Reality Unity3d app is the standard HelloAR demo app. This AR app uses Google AR Core. Information on developing native Andriod plugins for Unity3D can be found here.

    The Unity3D AR app loads the .AAR plugin and tries to open the camera, take a picture and save it to file, without any input from the user: no UI, no button press, and no preview of the capture to the user.

    When my plugin attempts to take a picture, I get the following exception:

    Code (Text):
    1. 2020/08/06 18:47:15.857 19906 21180 Info CameraManagerGlobal Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_OPEN for client com.cambridge.helloar API Level 1
    2. 2020/08/06 18:47:15.877 19906 19906 Warn System.err java.io.IOException: setPreviewTexture failed
    3. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.hardware.Camera.setPreviewTexture(Native Method)
    4. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager.takePhoto(PluginManager.java:242)
    5. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager.access$100(PluginManager.java:38)
    6. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager$4.run(PluginManager.java:351)
    7. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.os.Handler.handleCallback(Handler.java:883)
    8. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.os.Handler.dispatchMessage(Handler.java:100)
    9. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.os.Looper.loop(Looper.java:237)
    10. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.app.ActivityThread.main(ActivityThread.java:8107)
    11. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at java.lang.reflect.Method.invoke(Native Method)
    12. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    13. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
    14. 2020/08/06 18:47:15.877 19906 19906 Debug TAKEPHOTO Opened camera
    15. 2020/08/06 18:47:15.877 19906 19906 Debug TAKEPHOTO Started preview
    16. 2020/08/06 18:47:15.877 19906 19906 Warn System.err java.lang.RuntimeException: takePicture failed
    17. 2020/08/06 18:47:15.877 19906 19906 Warn System.err    at android.hardware.Camera.native_takePicture(Native Method)
    18. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.hardware.Camera.takePicture(Camera.java:1564)
    19. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.hardware.Camera.takePicture(Camera.java:1506)
    20. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager.takePhoto(PluginManager.java:250)
    21. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager.access$100(PluginManager.java:38)
    22. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at com.cambridge.helloar.PluginManager$4.run(PluginManager.java:351)
    23. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.os.Handler.handleCallback(Handler.java:883)
    24. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.os.Handler.dispatchMessage(Handler.java:100)
    25. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.os.Looper.loop(Looper.java:237)
    26. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at android.app.ActivityThread.main(ActivityThread.java:8107)
    27. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at java.lang.reflect.Method.invoke(Native Method)
    28. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    29. 2020/08/06 18:47:15.878 19906 19906 Warn System.err    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
    I've tried both the front and back camera but I get this exception in both cases.

    I have also noticed that I get a warning on my AR app saying something like "another app is using the camera. Tap this app or try closing the other app", so it's definitely something to do with the camera being blocked by ARCore. I do not want to prevent the Unity3D AR app from using the camera... of course. Instead, I want the AR app and my plugin to BOTH have access to the camera.

    I know my Plugin code works because doing this in a non-AR app works and a picture is taken in the background.

    Here is the code:
    Code (Java):
    1.  
    2. @SuppressWarnings("deprecation")
    3. private static void takePhoto() {
    4.  
    5.     Log.d("TAKEPHOTO", "Surface created");
    6.  
    7.     SurfaceTexture surfaceTexture = new SurfaceTexture(10);
    8.     Camera camera = Camera.open(0);
    9.     camera.getParameters().setPreviewSize(1, 1);
    10.     try {
    11.         camera.setPreviewTexture(surfaceTexture);
    12.     } catch (IOException e) {
    13.         e.printStackTrace();
    14.     }
    15.  
    16.     Log.d("TAKEPHOTO", "Opened camera");
    17.  
    18.     Log.d("TAKEPHOTO", "Started preview");
    19.     camera.takePicture(null, null, new Camera.PictureCallback() {
    20.  
    21.         @Override
    22.         public void onPictureTaken(byte[] data, Camera camera) {
    23.             Log.d("TAKEPHOTO", "Took picture");
    24.  
    25.             File pictureFileDir = new File("/sdcard/CaptureByService");
    26.             if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
    27.                 return;
    28.             }
    29.             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    30.             String date = dateFormat.format(new Date());
    31.             String photoFile = "PictureFront_" + "_" + date + ".jpg";
    32.             String filename = pictureFileDir.getPath() + File.separator + photoFile;
    33.             File mainPicture = new File(filename);
    34.             try {
    35.                 FileOutputStream fos = new FileOutputStream(mainPicture);
    36.                 fos.write(data);
    37.                 fos.close();
    38.                 Log.d("TAKEPHOTO", "image saved");
    39.             } catch (Exception error) {
    40.                 Log.d("TAKEPHOTO", "Image could not be saved");
    41.                 error.printStackTrace();
    42.             }
    43.             camera.release();
    44.         }
    45.     });
    46. }
    47.  

    Does anyone have any suggestions/solutions?
     


Loading...

Share This Page

Loading...