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

susu

Well-Known Member
Licensed User
Longtime User
I need to auto turn on flash from a service. If you tried it, please share your input. Thanks.
 

mauro vicamini

Active Member
Licensed User
Longtime User
CameraEx class requires Android 2.3+ and depends on Camera library v2.20+
Hi,
I've tried the exemple on my Android 2.3.6 but di app freeze when I take the picture and it block the camera (it give me "camera error" if I try to use the camera after have killed the freezed example app). On Android 4.4.2 it works.
Maybe on 2.3+ it's necessary to set some other specific parameter?

Thanks.
 

Urishev

Member
Licensed User
Longtime User
Hello! I study the example “CameraEX”.
How to change the code to keep the camera running continuous autofocus?
 

grafsoft

Well-Known Member
Licensed User
Longtime User
If camEx.GetSupportedFocusModes.IndexOf("auto") > -1 Then 'auto focus is supported
Try
camEx.SetContinuousAutoFocus
Catch
Log(LastException)
End Try

End If
 

Urishev

Member
Licensed User
Longtime User
I can't enter continuous focus mode. The problem in the code or in Sumsung GT P1000 ? Or in me?
Sub Camera1_Ready (Success As Boolean)
If Success Then
camEx.SetContinuousAutoFocus
camEx.CommitParameters
camEx.StartPreview
end if
 

JordiCP

Expert
Licensed User
Longtime User
Not all cameras support continuous autofocus. Look at the first line of grafsoft's answer
 

Urishev

Member
Licensed User
Longtime User
The command "camEx.FocusAndTakePicture" takes effect, appears autofocus. But "camEx.SetContinuousAutoFocus" - no autofocus. Why?
 

JordiCP

Expert
Licensed User
Longtime User
The code for SetContinuousAutoFocus is
B4X:
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

I would put logs in each one of the cases, so you will see if the problem is that your device does not support this mode or, even if it supports it, it isn't working properly
 

Urishev

Member
Licensed User
Longtime User
Thank you. I use code CameraExClass. At the start of the program Log:"Continuous focus mode is not available".
The problem in the device?
I think to go from CameraExClass to ACL. But ACL has no FocusDistance?
 
Last edited:

cooperlegend

Active Member
Licensed User
Longtime User
Hi, I am getting an error on my Samsung Tab S2 (but not on my Samsung S5 phone) when taking a picture (although picture is actually taken ok)

sub:cameraexclass_takepicture (java line: 608)

Please let me know if I can fix this please.
 

Urishev

Member
Licensed User
Longtime User
Thank you all for participating. Got my Samsung GT P1000 does not support ContinuousAutoFocus. On another device all is well.
 

cooperlegend

Active Member
Licensed User
Longtime User
90
[Height=1080, IsInitialized=false, Width=1920
]
Error occurred on line: 101 (CameraExClass)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
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)
Caused by: 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:137)
at the59club.sapphire.frmtest._pnlcamera_touch(frmtest.java:1041)
... 17 more
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
 

cooperlegend

Active Member
Licensed User
Longtime User
Thanks Erel,

I tried loads of different settings, but still no joy.

My code (works fine on my phone, but not on my tablet)

B4X:
Sub btnCamera_Click
    If btnCamera.Text = "Camera" Then
        pnlCamera.BringToFront
        pnlCamera.Visible = True
        btnCamera.Text = "Take"
        camEx.Initialize(pnlCamera, frontCamera, Me, "CameraTester")
        frontCamera = camEx.Front       
    Else
        camEx.FocusAndTakePicture
        'camEx.TakePicture   
    End If
End Sub

Sub CameraTester_Ready (Success As Boolean)
    Dim lSizes() As CameraSize
    Dim iMean As Int
   
    If Success Then
        camEx.SetJpegQuality(90)
        lSizes = camEx.GetSupportedPicturesSizes
        iMean = Floor(lSizes.Length / 2)
        camEx.SetPictureSize(lSizes(iMean).Width, lSizes(iMean).Height)
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

So picturesize is always a value size for each device, jpegquality is 90 percent.

What am I missing?

Thanks

** 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:137)
at the59club.sapphire.frmtest._pnlcamera_touch(frmtest.java:1058)
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 **
 
Status
Not open for further replies.
Top