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

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
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



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

Last edited:

cledic

Member
Licensed 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.
 

Erel

Administrator
Staff member
Licensed User
I found that in the example there is a missing parameter at line 51 that should be:
Fixed. Thank you.

About the getSupportedColorEffects method. You can add this sub:
B4X:
Public Sub GetSupportedColorEffects As List
   r.target = parameters
   Return r.RunMethod("getSupportedColorEffects")
End Sub
 

cledic

Member
Licensed 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

Well-Known Member
Licensed 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:

NJDude

Expert
Licensed User
I've noticed that on the Nexus 7 the camera effects (sepia, aqua, etc) do not work. :(
 

Erel

Administrator
Staff member
Licensed User
@peacemaker, can you post the error from the logs that you received when trying to open the front camera? The code should open the back camera instead.

@NJDude, you can check the supported effects. See post #3.
 

peacemaker

Well-Known Member
Licensed 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:

NJDude

Expert
Licensed User
@NJDude, you can check the supported effects. See post #3.
I'll play with it more, maybe the N7 camera is a little different, the effects on the class are supported I just don't see the "preview" and doesn't save the picture with the selected effect.
 

peacemaker

Well-Known Member
Licensed 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

Last edited:

Erel

Administrator
Staff member
Licensed User
I now understand. It errors when you try to open the back facing camera. I missed it before.
 

Erel

Administrator
Staff member
Licensed User
@NJDude, try to restart preview after you set the color effect:
B4X:
camEx.StopPreview
camEx.StartPreview
Does it make any difference?
 

Roger Garstang

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed 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

Well Known Member
Licensed 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?
 

Erel

Administrator
Staff member
Licensed User
1. Currently the code assumes that there is always a back facing camera. It will be fixed.

2. Not sure. You can decrease the picture size. It will decrease the file size for sure.
 

Roger Garstang

Well-Known Member
Licensed 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

Well Known Member
Licensed 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