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

Status
Not open for further replies.
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: 2,419
Last edited:

cooperlegend

Active Member
Licensed User
No more information after unticking the filtered flag....

I have tried every camera setup option i can think off.

It always crashes... YET it does actually take the picture and save it.

might just have to put a catch around this code until the routine is fixed.




LogCat connected to: B4A-Bridge: samsung SM-T710
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:the59club.sapphire
--------- beginning of system
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (frmsection) Create, isFirst = true **
(BALayout): Layout not available
ExecuteMemoryTable: SELECT name, id FROM sections WHERE app = 1 ORDER By id
** Activity (frmsection) Resume **
** Activity (frmsection) Pause, UserClosed = true **
** Activity (frmquestion) Create, isFirst = true **
(BALayout): Layout not available
ExecuteMemoryTable: SELECT id, question, help, qorder, multiplechoice, scored, '1' AS addquestion FROM testquestionsadd WHERE (venue_id = 175 OR venue_id = 0) AND section = 6 AND (subsection = 0 OR subsection = 0) ORDER By qorder
ExecuteMemoryTable: SELECT id, question, help, qorder, multiplechoice, scored, '0' AS addquestion FROM testquestions WHERE section = 6 AND (subsection = 0 OR subsection = 0) ORDER By tester_qorder
** Activity (frmquestion) Resume **
** Activity (frmquestion) Pause, UserClosed = true **
** Activity (frmtest) Create, isFirst = true **
(BALayout): Layout not available
(BALayout): Layout not available
** Activity (frmtest) Resume **
90
[Height=1080, IsInitialized=false, Width=1920
]
Error occurred on line: 101 (CameraExClass)
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1484)
at android.hardware.Camera.takePicture(Camera.java:1429)
at anywheresoftware.b4a.objects.CameraW.TakePicture(CameraW.java:287)
at the59club.sapphire.cameraexclass._takepicture(cameraexclass.java:330)
at the59club.sapphire.frmtest._pnlcamera_touch(frmtest.java:1065)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$1.run(BA.java:293)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Error occurred on line: 101 (CameraExClass)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:455)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:282)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$1.run(BA.java:293)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
** Activity (main) Resume **
 

wizard699

Active Member
Licensed User
ERROR JAVA line 618 in takePicture.

Hi Erel.
On some smartphone HTC and Samsung, CameraEx show this error immediatly after Picture preview.
I've this picture captured.
How resolve?
 

Attachments

  • Screenshot_2016-02-02-12-01-14.png
    Screenshot_2016-02-02-12-01-14.png
    326.5 KB · Views: 115
Last edited:

Cableguy

Expert
Licensed User
About the auto focus, the simple retrieving of the focus modes do NOT ensure that all are implemented in the hardware... Proof of that is my device, Lenny2 from wiko.
I have "auto" listed as supported, though I KNOW the device is fixed focus... So I tried to set the auto focus, and in the focus done event, I got an exception!
So be aware, device supported modes may not comply with the hardware!
 

wizard699

Active Member
Licensed User
You need to check the logs and see whether there is more information.

The error message itself means that the camera failed to take a picture. Maybe the settings were misconfigured.
Unfortunately, the error occurs only on a couple of smartphones , compared to 100 installed .
I do not have the ability to do debugging on these phones . Are reports and pictures from the outside joints . The image appears ... but after a while you get the error .
 

Baris Karadeniz

Active Member
Licensed User
"camEx.Release" line written at the Sub Activity_Pause gives an error when the app is closed when the camera is open. How can I solve this? My codes are;

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    camEx.Release  <------ LINE 451
End Sub

And the error is;

Installing file.
PackageAdded: package:app.taksim.driver
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
90
[Height=720, IsInitialized=false, Width=960
]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
0
[Height=720, IsInitialized=false, Width=960
]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Error occurred on line: 451 (Main)
java.lang.RuntimeException: Class instance was not initialized (cameraexclass)
at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:249)
at app.taksim.driver.cameraexclass._release(cameraexclass.java:72)
at app.taksim.driver.main._activity_pause(main.java:754)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at app.taksim.driver.main.onPause(main.java:262)
at android.app.Activity.performPause(Activity.java:5555)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3364)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3333)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3311)
at android.app.ActivityThread.access$1100(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 

Baris Karadeniz

Active Member
Licensed User
I initialized camera in the sub, because camera will not be displayed in the main panel. It will be displayed in another panel called as PanelCamera. What should I do in this case?

B4X:
Sub BtnCamera_Click
    PanelMain.Visible = False
    PanelCamera.Visible = True
    InitializeCamera
End Sub
 

Baris Karadeniz

Active Member
Licensed User
I tried to release it in anywhere else seen at the codes below related to the CameraPanel but when I open CameraPanel, camera shows meaningless view. If I release it in pause sub, camera works but it gives error which I gave above when the app is closed if the camera is open. I need to find another solution. May be release it in both 2 places?

B4X:
Sub BtnCamera_Click
    PanelMain.Visible = False
    PanelCamera.Visible = True
    InitializeCamera
End Sub

Sub BtnCameraBack_Click
    camEx.Release
    PanelCamera.Visible = False
    PanelMain.Visible = True
End Sub
 

Declan

Well-Known Member
Licensed User
I am using the example in the first post.
How can I set the following parameters in code:
Flash Mode
Picture Size.
I would like the picture to always be 320x240
The Flash will always be ON, if available
I will use the "Focus + TP" for the taking of picture instead of the "Take Picture"
 

Declan

Well-Known Member
Licensed User
With:
CamEx.SetFlashMode and CamEx.SetPictureSize.
Would this be the correct syntax:
B4X:
    camEx.SetFlashMode("on")
    camEx.SetPictureSize(320, 240)
Would this code be placed in:
Sub Activity_Create(FirstTime As Boolean)
 
Status
Not open for further replies.
Top