Java code in uno , what its possible and what its not

Hi guys , i’m implementing an sdk who use camera to scan some document using java foreign code inside uno this is my code.

  [Foreign(Language.Java)]
  public extern(Android) void LaunchScan()
  @{
         CameraManager cameraManager = new CameraManager(Activity.getRootActivity(), new CameraManager.Callback() {
          @Override
          public void onCameraReady(CameraManager cameraManager, Camera camera) {}

          @Override
          public void onCameraFailure(CameraManager cameraManager) {}

          @Override
          public void onShutterTriggered(CameraManager cameraManager) {}

          @Override
          public void onPictureTaken(CameraManager cameraManager, int cameraOrientation, ScanContainer scanContainer) {}
          });

         PreviewSurfaceView previewSurfaceView = new PreviewSurfaceView(Activity.getRootActivity()) {
            @Override
            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}

            @Override
            public void setAspectRatio(int width, int height) {}
        };

        Camera.PreviewCallback previewCallback = new Camera.PreviewCallback(){
              @Override
              public void onPreviewFrame(byte[] data, Camera camera) {}
          };

        // cameraManager.startPreview(previewSurfaceView,  previewCallback);
        //cameraManager.initialize()

  @}

when i try to call cameraManager.startPreview the app is successfully built but when i call launch scan the app quite and i get this : Can't create handler inside thread that has not called Looper.prepare()

i tried to put cameraManager.startPreview inside runOnUiThread like this

        Activity.getRootActivity().runOnUiThread(new Runnable() {
         @Override
         public void run() {
             cameraManager.startPreview(previewSurfaceView,  previewCallback);
         }
        });

but i continue to get the this error on my console when i call launch scan, when i quote the cameraManager.startPreview , and call cameraManager.initialize() the error continue to appear , it disappear only when i quote PreviewSurfaceView previewSurfaceView object instance. my question is , how can i put object instance outside and share them between function if its possible

It sounds to me like more or all of the code should be running on another thread (probably the UI thread), so I would try moving the Java code in the first snippet into the run() { ... } in the second snippet. Does that help?

I already tried this , it gives me the same error: Can't create handler inside thread that has not called Looper.prepare()

All of it? In your original post you wrote that you only put the call to startPreview there.

Yes but after posting this , i tried a lot of thing even what you said without finding a solution.

Hi there Prince,

Sorry but I’m unable to produce the issue as I dont know what CameraManager class you are using I tried https://developer.android.com/reference/android/hardware/camera2/CameraManager.html but it doesnt have a CameraManager.Callback class

Hmmm , its the sdk class, nothing to do with a pure android class, it use the android camera api and some opencv stuff to perform analyze on docs etc. the docs of the sdk is just local so this is some screens , maybe it can help you understand my issue.

this is for the CameraManager => http://prntscr.com/f5ruvb
this is for the CameraManager.Callback class http://prntscr.com/f5rvod
this is for the previewSurfaceView one http://prntscr.com/f5rv6d

CameraManager.initializeCamera and other stuff like this without launching the camera work perfectly. the problem is with launching the camera preview.

Hi prince, the screenshots of documentation don’t tell me what library this is or where to get it. Is CameraManager a class from your own camera library? If you have the project hosted somewhere, or are happy to upload it to our dropbox then I can have a look.

Just done it.

Great, I’ll have a look now

Found it. You were initializing your Runnable's fields on the wrong thread.

Try this instead:

public static void LaunchScan392(final UnoObject _this) {
    Activity.getRootActivity().runOnUiThread(new Runnable() {

        CameraManager cameraManager;   // dont initialize here
        PreviewSurfaceView previewSurfaceView;
        Camera.PreviewCallback previewCallback;

        @Override
        public void run() {
            // initialize here, on the uiThread.
            cameraManager = new CameraManager(Activity.getRootActivity(), new CameraManager.Callback() {
                @Override
                public void onCameraReady(CameraManager cameraManager, Camera camera) {
                }

                @Override
                public void onCameraFailure(CameraManager cameraManager) {
                }

                @Override
                public void onShutterTriggered(CameraManager cameraManager) {
                }

                @Override
                public void onPictureTaken(CameraManager cameraManager, int cameraOrientation, ScanContainer scanContainer) {
                }
            });

            previewSurfaceView = new PreviewSurfaceView(Activity.getRootActivity()) {
                @Override
                public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                }

                @Override
                public void setAspectRatio(int width, int height) {
                }
            };

            previewCallback = new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                }
            };
            cameraManager.startPreview(previewSurfaceView, previewCallback);
        }
    });
}

Once this is done your app crashes with a different error:

E AndroidRuntime: java.lang.NullPointerException: Attempt to read from field 'int android.hardware.Camera$Size.height' on a null object reference
E AndroidRuntime: 	at com.thegrizzlylabs.geniusscan.sdk.camera.CameraManager.startPreview(CameraManager.java:69)
E AndroidRuntime: 	at com.foreign.Genuis$1.run(Genuis.java:69)
E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:739)
E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:95)

This doesnt look like a Fuse error so I’ll leave that one to you :slight_smile:

Also your imports show that the CameraManager is not the standard Android CameraManager…

import com.thegrizzlylabs.geniusscan.sdk.camera.CameraManager;
import com.thegrizzlylabs.geniusscan.sdk.camera.PreviewSurfaceView;
import com.thegrizzlylabs.geniusscan.sdk.core.GeniusScanLibrary;
import com.thegrizzlylabs.geniusscan.sdk.core.ScanContainer;

… it looks like an inhouse library to me. You have the aar file in your lib/ folder too! Just so you know these were the details it really helps for us to know earlier so we can help narrow down the issue faster.

I hope this helps.

Hooo thank you a lot Chris, yes it’s not a fuse error , i will take this.
Yes its an in-house library, who use a part of the camera library.
Thank you again