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

**URL:** <https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715>\
**Category:** How-to Discussions\
**Created:** [May 6, 2017, 2:57pm UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715 "2017-05-06T14:57:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 6, 2017, 2:57pm UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/1 "2017-05-06T14:57:52Z")

</div>

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

```auto
  [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

```auto
        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

---

<div class="post-metadata">

**Author:** ![Olle\_Fredriksson](https://avatars.discourse-cdn.com/v4/letter/o/e95f7d/32.png) [@Olle\_Fredriksson](https://forums.fusetools.com/u/Olle_Fredriksson)\
**Post date:** [May 9, 2017, 7:52am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/2 "2017-05-09T07:52:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 9, 2017, 8:08am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/3 "2017-05-09T08:08:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olle\_Fredriksson](https://avatars.discourse-cdn.com/v4/letter/o/e95f7d/32.png) [@Olle\_Fredriksson](https://forums.fusetools.com/u/Olle_Fredriksson)\
**Post date:** [May 9, 2017, 8:19am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/4 "2017-05-09T08:19:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 9, 2017, 9:43am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/5 "2017-05-09T09:43:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Baggers](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/baggers/32/665_2.png) [@Baggers](https://forums.fusetools.com/u/Baggers)\
**Post date:** [May 9, 2017, 10:04am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/6 "2017-05-09T10:04:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 9, 2017, 10:28am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/7 "2017-05-09T10:28:23Z")

</div>

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](http://prntscr.com/f5ruvb)  
this is for the CameraManager.Callback class [http://prntscr.com/f5rvod](http://prntscr.com/f5rvod)  
this is for the previewSurfaceView one [http://prntscr.com/f5rv6d](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.

---

<div class="post-metadata">

**Author:** ![Baggers](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/baggers/32/665_2.png) [@Baggers](https://forums.fusetools.com/u/Baggers)\
**Post date:** [May 9, 2017, 11:20am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/8 "2017-05-09T11:20:21Z")

</div>

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](https://www.dropbox.com/request/ZgndLtJQm5eGzG9cicGK) then I can have a look.

---

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 9, 2017, 11:53am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/9 "2017-05-09T11:53:30Z")

</div>

Just done it.

---

<div class="post-metadata">

**Author:** ![Baggers](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/baggers/32/665_2.png) [@Baggers](https://forums.fusetools.com/u/Baggers)\
**Post date:** [May 9, 2017, 11:56am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/10 "2017-05-09T11:56:05Z")

</div>

Great, I’ll have a look now

---

<div class="post-metadata">

**Author:** ![Baggers](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/baggers/32/665_2.png) [@Baggers](https://forums.fusetools.com/u/Baggers)\
**Post date:** [May 9, 2017, 12:20pm UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/11 "2017-05-09T12:20:36Z")

</div>

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

Try this instead:

```auto
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:

```auto
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 🙂

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![prince](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@prince](https://forums.fusetools.com/u/prince)\
**Post date:** [May 10, 2017, 7:01am UTC](https://forums.fusetools.com/t/java-code-in-uno-what-its-possible-and-what-its-not/3715/12 "2017-05-10T07:01:32Z")

</div>

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
