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,262
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
I'm a tad confused with what CameraEX is finding on my Moto-X phone... KitKat 4.4.4...

I tried to do some Zoom things with it in my app. Here's the Log:

Zoom Supported: true
Max zoom________
60
0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
90
Picture sizes________
4320x2432
3232x2424
3264x1836
2592x1944
1920x1080
1600x1200
1280x960
1280x720
640x480
640x360
Flash modes________
auto
on
off
torch
Focus modes________
auto
continuous-picture
infinity
continuous-video
Colour effects________
none
mono
negative
solarize
sepia
posterize
whiteboard
blackboard
aqua
Scene modes________
auto
action
portrait
landscape
night
night-portrait
theatre
beach
snow
White balance options________
auto
incandescent
fluorescent
daylight
cloudy-daylight
Zoom Supported: true
Max zoom________
60
0

That last 0 is the Current Zoom level.

Now:
  • When I try to get through effects, it stops at sepia and freezes the phone. They work all the way up to sepia and then just freeze... no effects, no nuttin! However, the gallery has a TON of editing features.
  • I can't get Zoom to trigger at all and the phone definitely has it.
  • Haven't tried the focus modes...
  • Haven't a clue how to use the Scene modes on the phone itself, so I wouldn't know how to trigger them in this.
  • Haven't tried White-Balance yet... Just want to get the simple stuff working...
Any suggestions???
 

Shadow&Max

Active Member
Licensed User
Longtime User
That may be Erel and you're probably right... But the device manufacturer is Motorola, owned by Google, and this was designed for the original release of KitKat by Google's own company!
 

bautistasbr

New Member
Licensed User
Longtime User
Hi
When i test the download example,its said me

javac 1.8.0_11
src\anywheresoftware\b4a\samples\camera\cameraexclass.java:604: error: cannot find symbol
BA.senderHolder.set(sender);
^
symbol: variable senderHolder
location: class BA
 

bautistasbr

New Member
Licensed User
Longtime User
3.82 The last

Edit: Ok,i reinstall an now compile ^^
But the emulated camera dont work :(
 
Last edited:

MaFu

Well-Known Member
Licensed User
Longtime User
Based on Jim Browns version i added two additional functions to CameraExClass:
GetRotation - returns the camera rotation (needed in Camera_Preview event)
GetSupportedPreviewSizes - returns an array of CameraSize types containing the available preview sizes​
 

Attachments

  • cameraEX124.zip
    15.9 KB · Views: 423

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

thanks for your implementation; i just found out, that there is a problem on my mobile with the autofocus: if you tap more then three times on the screen, you get:
Error occurred on line: 569 (cameraexclass)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
...
Caused by: java.lang.RuntimeException: autoFocus failed

Is this problem common or only related to my device?
 

Shadow&Max

Active Member
Licensed User
Longtime User
Yes, I notice that auto-focus on my Moto-X really doesn't work. At times the image will get out of focus, and never come back until you close the camera and reinitialize it.
 

Baltazar

Member
Licensed User
Longtime User
Hi Everyone! A newbie to B4A here. Is there a way to force the camera preview to any preview screen dimension so desired? For example , I want to keep the preview image to around 60%x & 20%y in portrait mode - that is without unnecessarily cropping the images.
 

Noize

Member
Licensed User
Longtime User
Hi, I've imported Cameraex class into my project, and when I try to launch it stops on
cam.Initialize2(Panel1, "camera", id) line showing a java.lang.ClassException:android.widget.Button cannot be cast to android.view.SurfaceView , what means this?.
 

Baltazar

Member
Licensed User
Longtime User
Hi, I've imported Cameraex class into my project, and when I try to launch it stops on
cam.Initialize2(Panel1, "camera", id) line showing a java.lang.ClassException:android.widget.Button cannot be cast to android.view.SurfaceView , what means this?.
Did you use a button to show the cam image? Use a an ImageView or a Panel so the code would be something like :
B4X:
dim   myCamView as  Panel
'...... .... don't forget to initialize the panel
cam.Initialize2(myCamView, "camera", 0)     ' 1 for the other cam
 

Noize

Member
Licensed User
Longtime User
I'm trying to add the view to a panel created by the designer, wich has a action bar, should I have to open in a free panel on an additional view?

tnx
 

Noize

Member
Licensed User
Longtime User
Ok I tried to open it on a WebView into the panel and it rules.
Tnx for your help
 

Johan Schoeman

Expert
Licensed User
Longtime User
I have added the flowing code to the CameraEx project (draw vertical and horizontal lines on a panel that is overlaid on the camera panel). At some stage it just crashed. See log further down. Using Samsung S4 mini. Had to power down and restart to get camera functioning again. Any idea?

B4X:
Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetJpegQuality(90)
        camEx.CommitParameters

        Panel2.BringToFront
        Panel2.Color = Colors.Transparent
        Dim c2 As Canvas
        c2.Initialize(Panel2)
        c2.DrawLine(0,Panel2.height/2,Panel2.Width,Panel2.Height/2,Colors.Red,1dip)    'horizontal line
        c2.DrawLine(0,Panel2.height/4,Panel2.Width,Panel2.Height/4,Colors.Red,1dip)    'horizontal line
        c2.DrawLine(0,3*Panel2.height/4,Panel2.Width,3*Panel2.Height/4,Colors.Red,1dip)    'horizontal line
       
        c2.DrawLine(0,1*Panel2.height/8,Panel2.Width,1*Panel2.Height/8,Colors.Red,1dip)    'horizontal line
        c2.DrawLine(0,3*Panel2.height/8,Panel2.Width,3*Panel2.Height/8,Colors.Red,1dip)    'horizontal line
        c2.DrawLine(0,5*Panel2.height/8,Panel2.Width,5*Panel2.Height/8,Colors.Red,1dip)    'horizontal line
        c2.DrawLine(0,7*Panel2.height/8,Panel2.Width,7*Panel2.Height/8,Colors.Red,1dip)    'horizontal line
       
        c2.DrawLine(Panel2.width/2,0,Panel2.width/2,Panel2.Height,Colors.Red,1dip)     'vertical line
        c2.DrawLine(Panel2.width/4,0,Panel2.width/4,Panel2.Height,Colors.Red,1dip)     'vertical line
        c2.DrawLine(3*Panel2.width/4,0,3*Panel2.width/4,Panel2.Height,Colors.Red,1dip)     'vertical line
       
        c2.DrawLine(1*Panel2.width/8,0,1*Panel2.width/8,Panel2.Height,Colors.Red,1dip)     'vertical line
        c2.DrawLine(3*Panel2.width/8,0,3*Panel2.width/8,Panel2.Height,Colors.Red,1dip)     'vertical line
        c2.DrawLine(5*Panel2.width/8,0,5*Panel2.width/8,Panel2.Height,Colors.Red,1dip)     'vertical line
        c2.DrawLine(7*Panel2.width/8,0,7*Panel2.width/8,Panel2.Height,Colors.Red,1dip)     'vertical line       
        Panel2.Invalidate
       
        camEx.StartPreview
       
        Log(camEx.GetPreviewSize)

    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

** Activity (main) Create, isFirst = true **

(Main, 23) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
** Activity (main) Resume **

java.lang.NullPointerException

at anywheresoftware.b4a.objects.CameraW.AutoFocus(CameraW.java:191)
at anywheresoftware.b4a.samples.CameraEx.cameraexclass._focusandtakepicture(cameraexclass.java:345)
at anywheresoftware.b4a.samples.CameraEx.main._btnfocus_click(main.java:474)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4377)
at android.view.View$PerformClick.run(View.java:18044)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)

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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 

Attachments

  • Crash.zip
    11.2 KB · Views: 271

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks Erel. Happened again on my Samsung S4 mini when I clicked on Picture Size button. Phone was unfortunately not connected to PC/IDE at that time so could not get the log. Will try and simulate it again and then post the unfiltered log
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
using the example project, and click on "Take picture", the app crash.
Here it is the log:

B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


java.lang.reflect.InvocationTargetException


success = false, (RuntimeException) java.lang.RuntimeException: Fail to connect to camera service


java.lang.NullPointerException


    at anywheresoftware.b4a.objects.CameraW.TakePicture(CameraW.java:287)
    at anywheresoftware.b4a.samples.camera.cameraexclass._takepicture(cameraexclass.java:600)
    at anywheresoftware.b4a.samples.camera.main._btntakepicture_click(main.java:414)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
    at android.view.View.performClick(View.java:4222)
    at android.view.View$PerformClick.run(View.java:17337)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4895)
    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:994)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException

What's wrong?
 
Status
Not open for further replies.
Top