Android Question JavaObject or Reflection Code

walterf25

Expert
Licensed User
Longtime User
Hi all, does anyone know how to write this code using either JavaObject or Reflection libraries, maybe both are needed?

I'm not so good and don't really understand much how to use those two libraries, i've tried several things but none of them work

B4X:
        try {
            mCamera.setPreviewCallbackWithBuffer(callback);
            for (int i = 0; i < 16; i++)
                mCamera.addCallbackBuffer(new byte[1280*480*3/2]);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }

I am able to create the callback Object like this
B4X:
    Dim callback As Object
    Dim r2 As JavaObject
    r2 = cam
    callback = r2.InitializeStatic("android.hardware.Camera$PreviewCallback")
i am using the CameraEx class example but i am trying to add the above code so that I can use this class in my EasyAR Library to be able to use a custom camera.

Thanks in advanced for any help or suggestions.

Walter
 

stevel05

Expert
Licensed User
Longtime User
If you already have the camera object and you just need to call the additional methods then this should work:

B4X:
Try
    Dim mCameraJO As JavaObject = mCamera
    mCameraJO.Runmethod("setPreviewCallbackWithBuffer",Array(callback))
    Dim i As Int
    For i = 0 To 15
        Dim BBuffer As Byte(1280*480*3/2)
        mCameraJO.Runmethod("addCallbackBuffer",Array(BBuffer))
    Next
    mCameraJO.Runmethod("startPreview",Null)
Catch
     Log(LastException)
End Try
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I am using CameraEx class and I pass the cam parameter from that class but i get the following error:
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
On looking at the rest of the Camera class, I'm not sure that your callback code with do what you are expecting, as there is no sub defined to call.

You may need to create a callback using :

B4X:
Callback = Jo.CreateEventFromUI("android.hardware.Camera$PreviewCallback","VidCallback",Null)

Then add a sub to handle it:


B4X:
Sub VidCallback_Event (MethodName As String, Args() As Object) As Object
    
End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I think we're getting somewhere, i tried your second suggestion and now I get the following:

java.lang.IllegalArgumentException: Expected receiver of type android.hardware.Camera, but got java.lang.Class<android.hardware.Camera>

Walter
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That's a new error from the creation of the callback,the original error will still be there.

It looks like the underlying camera object is not exposed in the library, I haven't used it so perhaps someone that has can help you further. Sorry to lead you on a wild goose chase.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I've dug a little deeper into this and it's not clear to me how setting a callback buffer will allow accessing a custom camera, it doesn't appear to change any thing, only creates a callback sub which receives the preview frames in the buffers you provide when creating the callback.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Or are you looking to display a custom sized preview?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
So the reason for doing this, according to one of the EasyAR examples is that it improves preview efficiency and frame rate by allowing preview frame memory reuse.

Also because in the previewCallback callback function Erel is only exposing the data[] parameter in the signature and I also need the Camera parameter to be exposed in that same signature.
I am talking about this callback function
B4X:
 @Override
        public void onPreviewFrame(final byte[] data, final Camera camera)
Erel is only exposing the byte[] data paratemer and not the Camera parameter.

I think i'm just gonna have to write a separate class and wrap this as well, i was trying to avoid having to do this but it seems that for the sake of time I will just have to do that.

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You don't need the camera parameter. It will be always the same as the nativeCam global variable.
Oh I see, OK, i will give it a try tomorrow again.

Thanks,
Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…