B4A Library [Class] CameraEx - Extends the camera library functionality

Status
Not open for further replies.
Example based on B4XPages: https://www.b4x.com/android/forum/threads/b4x-b4xpages-barcode-reader.120417/#content

CameraEx class wraps the Camera object and using reflection and other code it extends its functionality.

CameraEx class requires Android 2.3+ and depends on Camera library v2.20+
CameraEx features:
  • Easily open the back or front camera
  • Preview images and saved images orientation will match the device orientation (all orientations are supported)
  • Gives access to Camera.Parameters native class (flashmode, picture size, effects and other settings)
  • Includes methods to convert preview images to JPEG and to save the taken pictures.
  • It should be simple to add more methods to this class

SS-2012-11-28_12.18.31.png


See this page for the constant values and other possible methods:
https://developer.android.com/reference/android/hardware/Camera.Parameters.html

Note that you should call CommitParameters after changing one or more parameters.

CameraExClass module is included in the attached example.

V1.30 is attached - Includes various new methods.

v1.20 - Includes all the various posts in this thread as well as AutoFocusAndTakePicture method which first calls AutoFocus and then takes a picture (if AutoFocus was successful).

Edit (06/2018): A new version was uploaded with targetSdkVersion set to 26.
 

Attachments

  • CameraEx.zip
    13.2 KB · Views: 4,320
Last edited:

cledic

Member
Licensed User
Longtime User
Hello Erel,
many thanks for this class and the library too.

I found that in the example there is a missing parameter at line 51 that should be:
ToastMessageShow("Picture saved.", False)

And I want to ask if will be possible to have the function:
getSupportedColorEffects()
available to verify which colors effect the device support.

Thanks again
Clemente.
 

cledic

Member
Licensed User
Longtime User
Erel,
I changed the btnEffect_Click Sub in:

Sub btnEffect_Click

Dim effects As List = camEx.GetSupportedColorEffects

Dim effect As String = effects.Get(Rnd(0, effects.Size))

camEx.SetColorEffect(effect)
ToastMessageShow(effect, False)

camEx.CommitParameters
End Sub

and works very well.

Thanks
Clemente
 

peacemaker

Expert
Licensed User
Longtime User
Suggest updated subs:

B4X:
Private Sub Camera_Ready (Success As Boolean)
Try
   If Success Then
      r.target = cam
      nativeCam = r.GetField("camera")
      r.target = nativeCam
      parameters = r.RunMethod("getParameters")
      SetDisplayOrientation
   End If
   CallSub2(target, event & "_ready", Success)
Catch
   ToastMessageShow("Camera problem.", False)
End Try
End Sub

Public Sub Release
Try
   cam.Release
Catch
End Try
End Sub

As i have a tablet with only the front camera. So any try to work with the back one by default gives a error catch.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Errors were about returning the non-inited lists of all properties.
It needs to check the size of the lists before using their items.

And my chinese tablet is so strange that a back side default camera (that does not exist really) is Ready.Success = true, but gives a error at StartPreview.
I guess, some hardware exists, but no sensor itself.
So, it's just a suggestion - to make all the subs error-proof, as many variants with cameras are possible.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
This including the sub GetSupportedColorEffects, i attached the result project without errors during cameras toggling (but i have only the front camera for this device):

B4X:
LogCat connected to: 20080411
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


0, 0
Supported sizes:
640x480
320x240
(List) Not initialized
0, 0
Supported sizes:
640x480
320x240
(List) Not initialized
java.lang.NullPointerException
   at android.view.SurfaceView.updateWindow(SurfaceView.java:1139)
   at android.view.SurfaceView.access$000(SurfaceView.java:87)
   at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1621)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2446)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
0, 0
Supported sizes:
640x480
320x240
(List) Not initialized
0, 0
Supported sizes:
640x480
320x240
(List) Not initialized
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Fatal signal 11 (SIGSEGV) at 0x00000070 (code=1)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
cameraexclass_getsupportedcoloreffects (B4A line: 173)
Return r.RunMethod("getSupportedColorEffects")
java.lang.NoSuchMethodException: getSupportedColorEffects []
   at java.lang.Class.getConstructorOrMethod(Class.java:460)
   at java.lang.Class.getDeclaredMethod(Class.java:685)
   at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:203)
   at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:791)
   at anywheresoftware.b4a.samples.camera.cameraexclass._getsupportedcoloreffects(cameraexclass.java:327)
   at anywheresoftware.b4a.samples.camera.main._btneffect_click(main.java:297)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:153)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:149)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:55)
   at android.view.View.performClick(View.java:3511)
   at android.view.View$PerformClick.run(View.java:14109)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NoSuchMethodException: getSupportedColorEffects []
270, 90
Supported sizes:
640x480
320x240
flashmodes:(List) Not initialized
270, 90


Supported sizes:
640x480
320x240
flashmodes:(List) Not initialized
270, 90
Supported sizes:
640x480
320x240
flashmodes:(List) Not initialized
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
No wakelock.
** Activity (main) Pause, UserClosed = true **
 

Attachments

  • cameraEXclass1.01_project_error-proof.zip
    9.5 KB · Views: 1,015
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Will any of this eventually just become part of the Camera 2 library, or maybe a CameraEx Library? Seems like extra work/overhead/wrappers to do this stuff with reflection (And a Class + Library). I could see the image conversion stuff that really isn't related to the camera specifically being in a class, but you probably spent more time making the class for the subs that are nothing more than a wrapper or reflection call than it would have been to just put them in the Library that has access to the objects without reflection.

I'm not complaining as I love the camera getting attention now and the orientation issues and such was the whole reason I wasn't able to use it and made my EZcamera library (I can give you the code on it too if needed/wanted to add even more capability to it). I just figured it would have been easier on you to have it in the library and be done.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Will any of this eventually just become part of the Camera 2 library
No.

The camera library supports Android 1.6 and above. Many of the methods in this class depend on Android 2.3. This is why they cannot be merged into the Camera library. It is true that I can create a new Camera2 library however there are methods that are only available in Android 4 and above. So quickly we will need Camera3 library.

As demonstrated in post #3 it is very simple to wrap the calls. There is no performance advantage for building this code in a library. The reflection overhead is insignificant in this case.
 

Mahares

Expert
Licensed User
Longtime User
@Erel:
1. When a device is equipped with only a FRONT facing camera, you have to click 'Change Camera' to activate it. I tried to see if I can start the FRONT facing camera automatically, but could not.
2. When I dropped the quality from:
B4X:
camEx.SetJpegQuality(90)
to
B4X:
camEx.SetJpegQuality(20)
the size of the picture dropped from 1044 KB to only 1013 KB. I was hoping for a bigger drop. Can you figure that out?
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
2. When I dropped the quality from:
B4X:
camEx.SetJpegQuality(90)
to
B4X:
camEx.SetJpegQuality(20)
the size of the picture dropped from 1044 KB to only 1013 KB. I was hoping for a bigger drop. Can you figure that out?

I usually only work in the 70%-100% range. Anything less than that doesn't gain much and/or looks horrible (Depends on picture content and brightness too). Biggest jumps tend to be in those first 10%- 90-100%, but usually PNG gets around the same size without loss of picture quality for that range, so unless it is a small web picture I usually just go PNG too.
 

Mahares

Expert
Licensed User
Longtime User
@Roger and @Erel: Thank you very much for the quick responses. I changed the file extension to PNG from JPG and changed the quality of the picture from 90 to 20. The file size decreased from 913 KB at 90% quality to 249 KB at 20% without sacrificing the actual acuity of the picture. That is substantial.
 
Status
Not open for further replies.
Top