Android Question (Solved) Reflection issue in CameraEx PreviewImageToJpeg.

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
I have an app with two activities. From first activity the user starts the second activity by a button click, which shows a camera preview and every 2 or 3 seconds converts the YUV image to JPG using CameraEx Class. When the user closes the connection, the activity is finish-ed and cameraEx object is destroied.
If user clicks the first activity button the cycle is repeated again.
Many cycles I get no problem, but in some case the PreviewImageToJpeg generates method.not.found in getPreviewSize. I trap the error so the program is able to continue, but in this case further conversions fail.
I can get the error for many cycles, but after a few tries the process works again!
A call should not work in this alternate fashion!
Have anybody an hint for me?
 

tigrot

Well-Known Member
Licensed User
Longtime User
Here is the "very simple" Camera1.preview event manager:

B4X:
Sub Camera1_Preview (PreviewPic() As Byte)
    If Communicator.started=False Then Return   ' just to be sure than everything has started
    If Communicator.videoconnesso= False Then Return ' idem
    If DateTime.Now > lastPreviewSaved + IntervalMs Then
       If  Communicator.astreams.OutputQueueSize=0 Then ' I want to be sure that nothing is queued for xmit
          lastPreviewSaved = DateTime.Now
          Try
            Dim jpeg() As Byte = camEx.PreviewImageToJpeg(PreviewPic, 70) 'here is the faulty code call
          Catch
              Log(LastException.Message)
              Return
          End Try
          Try
            CallSubDelayed2(Communicator, "Send", jpeg)
          Catch
            log ("Send error")
          End Try
        End If
      
    End If
End sub

Code for camera init

B4X:
Private Sub InitializeCamera
    If asktoend = True Then 'there was a remote request for disconnection
      asktoend=False
      Communicator.started=False
      CallSubDelayed(Connector,"disconnect")
      StopService(Communicator)
      Activity.Finish
      StartActivity(Main)
      Return
    End If
    Log("Inizialize camex")
    camEx.Initialize(Panel1, frontCamera, Me, "Camera1")
    If Communicator.tryingToConnect1  = False Then label11.Text="CONNESSO"
End Sub


Code for Camera_ready:

B4X:
Sub Camera1_Ready (Success As Boolean)
    Log("camera ready:" & Success)
    If Success Then
        camEx.SetPreviewSize(320,240) ' I have hardcoded these values just to test, nothing changed
        camEx.CommitParameters
        camEx.StartPreview
        camEx.SetJpegQuality(90)
        camEx.SetPictureSize(320,240)  ' I have hardcoded these values just to test, nothing changed
        camEx.CommitParameters
        camEx.GetPreviewSize              ' have put this line to see if the call was working here, no error shown...
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

In CameraEx class I have tried to see if other method calls were working in PreviewImageToJpeg but it seems that none of the reflector call works.
As I told the preview keeps on showing on screen, while I get java.lang.NoSuchMethodException: getPreviewSize

Thank you very much.

Mauro
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I'm almost sure that I destroy in some way the Parameters object in CameraEx Class. It's loaded in Camera_ready event and never after. This could explain my inability to get parameters after the first error.
Checked...
When the conversion works I get for Parameters content:
android.hardware.Camera$Parameters@406b2940
android.hardware.Camera$Parameters@406a6bd0

When it doesnt:
java.lang.Object@406eb440

the stack is:
java.lang.NoSuchMethodException: getPreviewSize
at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
at java.lang.Class.getDeclaredMethod(Class.java:731)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:802)
at com.YourPersonalAssistantTest.app.cameraexclass._previewimagetojpeg(cameraexclass.java:739)
at com.YourPersonalAssistantTest.app.camera._camera1_preview(camera.java:498)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:858)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:815)
at com.YourPersonalAssistantTest.app.cameraexclass._camera_preview(cameraexclass.java:130)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:589)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
java.lang.Object@406eb440
java.lang.NoSuchMethodException: getPreviewSize
at java.lang.ClassCache.findMethodByName(ClassCache.java:247)


Dunno what happens!

 
Last edited:
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I was able to completelly block the phone with a rapid use of the button TakePicture in CameraEx class. I'm not able to see what is behind the Camera.TakePicture, which is called from Class. Now I'm taking battery off the phone and powering it again. In the example there is no direct call of th previewtojpeg function, i should modify it... I think this is a problem of the demo, not disabling the button till the work is done. Let me see where the error is coming from. I think the problem is in Camera library.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
The issue was caused by an erratic second Initialize of the CameraEx object. I didn't realize it because in both case I got Success = True from the CameraEx_ready event, and this is an issue, maybe of Android itself...
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
The issue was caused by an erratic second Initialize of the CameraEx object. I didn't realize it because in both case I got Success = True from the CameraEx_ready event, and this is an issue, maybe of Android itself...

Hello,
I am encountering the exact same problem – except I am explicitly doing a second initialize of the camera (for example returning to my app after pressing the home button). In activity_pause I release the camera so that other apps can use it. When restarting my app I initialize the camera a second time, since it had been previously released, and I get the following error in the setJpegQuality sub of CameraEx:
B4X:
** Activity (main) Resume **
java.lang.NoSuchMethodException: setJpegQuality [int]
    at java.lang.Class.getConstructorOrMethod(Class.java:472)
    at java.lang.Class.getDeclaredMethod(Class.java:640)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:817)
    ...

How did you solve his error?

Thanks,
Barry.

Added a little later:
I believe I found my error. It's a timing error where I am accessing the setJpegQuality function after the second initialization before the _Ready event executes.
 
Last edited:
Upvote 0
Top