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

knorton

New Member
Licensed User
Longtime User
enableShutterSound

:sign0104:
Is this method supported?
If so can someone help show how to add this
to the CameraEx class
Thanks
Jay
 

margret

Well-Known Member
Licensed User
Longtime User
I downloaded an ran the example, It is really Cool!

Thank You:icon_clap:

One thing I may be over looking, I can't find a focus in the sample. It all works just fine but it does not focus the image before or after the shot, so all images are blurry. The stock app focuses just as you touch the take picture.

What am I missing?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this code to CameraEx:
B4X:
Public Sub GetSupportedFocusModes As List
   r.target = parameters
   Return r.RunMethod("getSupportedFocusModes")
End Sub

Public Sub SetContinuousAutoFocus
   Dim modes As List = GetSupportedFocusModes
   If modes.IndexOf("continuous-picture") > -1 Then
      SetFocusMode("continuous-picture")
   Else If modes.IndexOf("continuous-video") > -1 Then
      SetFocusMode("continuous-video")
   Else
      Log("Continuous focus mode is not available")
   End If
End Sub

Public Sub SetFocusMode(Mode As String)
   r.target = parameters
   r.RunMethod2("setFocusMode", Mode, "java.lang.String")
End Sub

Call SetContinuousAutoFocus in Camera_Ready (before the commit call).
 

EddyW

Member
Licensed User
Longtime User
Demo app locked

I am trying the demo app on a galaxy note but i get the message 'Cannot open camera.' the preview don't work.

When debug i see that success in the line below is false
Private Sub Camera_Ready (Success As Boolean)

I use b4a 2.30 with camera 2.01 and reflection 2.20

CameraService::connect X (pid 8492) rejected (existing client).
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.objects.CameraW$2.run(CameraW.java:112)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:349)
at android.hardware.Camera.open(Camera.java:303)
id=312 Removed maio idx=1 Map Size=3
... 9 more
id=314(3) createSurface 0x1fc11f4 (1280x752),1 flag=0, maio
setHidden false
setHidden false
 
Last edited:

EddyW

Member
Licensed User
Longtime User
If we restart the galaxy note and start the demo app then we see the preview.
If we then take a picture the app locks we then have to kill the app.

And if we then open the App again we get the message that no camera found.
After reboot the galaxy note the preview works again until we take a picture.

we did some more testing and looks like a timing error.
When we in debug from line to line it works but in runtime it cant find the camera
 
Last edited:

EddyW

Member
Licensed User
Longtime User
We have tried to set a delay (a sleep function) before the cam.StartPreview but (mostly) without success.

But when we have a preview and take a picture it looks like it comes in a loop in function camera_preview(data as byte)
 

splatt

Active Member
Licensed User
Longtime User
@EddyW, I've been running the example on a Galaxy Note (4.0.3) with no problems at all. I've also heavily modified it and still no problems.
 

EddyW

Member
Licensed User
Longtime User
@Erel We have not modified the example code, we added only a sleep function.

@Splatt We are using the galaxy note (4.0.4.)

The same app works fine on the galaxy tab 2 (4.0.3.)
 

EddyW

Member
Licensed User
Longtime User
We did some more testing and tried also the 'Advanced Camera Library V2.00' lib.
Somehow this lib don't works on a galaxy note (android 4.04) but the other
lib works fine. So for our project we switch to the other lib.

Thx for your help
 

wheretheidivides

Active Member
Licensed User
Longtime User
Call SetContinuousAutoFocus in Camera_Ready (before the commit call).

I have read your info on the camera, installed it, copied over the extra subroutines. I also was able to follow the original code to take a snapshot. Now, I was wondering if someone could help out a little more.

1) I copied over the"CameraExClass". Do you have to initialize it somehow in "Sub Process_Globals". What is the code? What I tried didn't work.

2) How do you call the subroutines like autofocus? I tried and failed. maybe cause it has not been initialized?

3) are there a list of code commands to show how to get the camera stuff to work. What was given was a generic android list and not B4A code.

please use code and keep it simple. Just 1 line per command. Shortcuts just confuse beginners. Always use longhand.

Thanks.
 
Last edited:

wheretheidivides

Active Member
Licensed User
Longtime User
1)What EXACTLY does the following do? I can guess, but wanted to be sure. The example program is just a camera, however in my program it will be used to shoot receipts. So I don't want the camera to stay on. Am I to understand that after taking the picture you add "camEx.Release"? and does "InitializeCamera" start the camera?


B4X:
Sub Activity_Pause (UserClosed As Boolean)
   camEx.Release
End Sub

Sub Activity_Resume
   InitializeCamera
End Sub

2) How to you get autofocus to work? I added the code to the class. It is as simple as calling it? code please. Like this? After taking a pic the screen freezes up. The first picture records and continues to the camera but the second time clicking to take a picture freezes up the program. I have to exit the program. It crashes. Then if I rerun the program it comes up with the buttons a blank screen. Nothing works. When I press the buttons they give errors. Reinstalling the program does not work. I have to shut the phone off and on again. This happens when I added the "camEx.SetContinuousAutoFocus". Is this the correct spot?

Sub Camera1_Ready (Success As Boolean)
If Success Then
camEx.StartPreview
camEx.SetJpegQuality(90)
camEx.SetContinuousAutoFocus
camEx.CommitParameters
Else
ToastMessageShow("Cannot open camera.", True)
End If
End Sub

3) now a tough question that I've been thinking about for a while. Is there a way to have a camera icon on the login screen? or have a special code logging in that will run the camera? That way when you want to take a pic it will go immediately to the camera?
 
Last edited:
Status
Not open for further replies.
Top