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

Douglas Farias

Expert
Licensed User
@Erel i tested this example in 3 diferents devices, and when u take a picture using front camera in portrait, this flip on horizontal the image.
only in front camera, back camera is normal.

this bug dont happens on the preview, only on the final photo, on the saved image
file.

how can i fix this? tested in a moto g4 , samsung j2 and sony e4 (front camera + portrait = fliped horizontal picture)

i know this is not right place to post this, but this bug is on the example too :)

thx
 
Last edited:

Rusty

Well-Known Member
Licensed User
CameraEx - If the device is set to automatically store GPS location, can CameraEx also automatically store the GPS location in the Exif data?
Or do you have to update the Exif data yourself?
Thanks,
Rusty
 

padvou

Active Member
Licensed User
Hi!
Here's some Subs I added to the Class and I use in a project of mine:

B4X:
Public Sub GetSupportedWhiteBalance As List
    r.target = parameters
    Return r.RunMethod("getSupportedWhiteBalance")
End Sub

Public Sub GetWhiteBalance As String
    r.target = parameters
    Return r.RunMethod("getWhiteBalance")
End Sub

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

I also added this code in the Activity Module to test:

B4X:
Sub Button1_Click
   
    For i=0 To L.Size-1
       
        Dim testval As String=L.Get(i)
        If testval=CurMode Then
            If i <L.Size-1 Then CurMode=L.Get(i+1) Else CurMode=L.Get(0)
           
            camEx.SetWhiteBalance(CurMode)
           
            ToastMessageShow(camEx.GetWhiteBalance,False)
            camEx.StopPreview
            camEx.CommitParameters
            camEx.StartPreview
            Exit
        End If
       
    Next
End Sub

I have a question though.
A device I'm using gives "manual" as one of the white balance modes.
Does anyone know how to use it in code and send a manual value?
 

biometrics

Active Member
Licensed User
Has anyone used this with a Samsung J1 entry level phone?

I have reports that it's failing to take a picture with .TakePicture.

Unfortunately I don't have such a phone to test so that makes it difficult to sort out.
 

cxdzbl

Active Member
Licensed User
For a long time did not solve the problem of the direction of the photo, and finally solved today, thank you
 

hayderOICO

Member
Licensed User
hi erel

I just upgraded from the 1.21 to the 1.3 library. added javaobjects. but the commit parameter fails.

the "demo" camera app you bundled does work fine though. here's some of my code.

using b4A v 5.02

B4X:
#Region  Project Attributes
    #ApplicationLabel: Cam
    #VersionCode: 5
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    Private frontCamera As Boolean = False
    Private usbserial As felUsbSerial
    Private manager As UsbManager
    Dim DisableCameraTimer As Timer
    Dim TakePicTimer As Timer
End Sub

Sub Globals
    Private Panel1 As Panel
    Private Panel2 As Panel
    Private camEx As CameraExClass
    Private btnTakePicture As Button
    Dim awake As PhoneWakeState
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("allinone")

       If FirstTime Then
    manager.Initialize
      End If

    DisableCameraTimer.Initialize("DisableCameraTimer1",300)
    TakePicTimer.Initialize("pictimer",300)
    awake.KeepAlive(True)
End Sub


Sub Activity_Resume
    InitializeCamera
    manager.Initialize
    awake.KeepAlive(True)
End Sub

Private Sub InitializeCamera
    camEx.Initialize(Panel2, frontCamera, Me, "Camera1")
    DisableCameraTimer.Enabled=True
    frontCamera = camEx.Front
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    camEx.Release
    awake.ReleaseKeepAlive
End Sub

Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetColorEffect("none")
        camEx.SetFlashMode("Off")
        camEx.SetPictureSize(4032, 3024)
        camEx.ExposureCompensation=-6
        camEx.CommitParameters
        camEx.StartPreview
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub btnTakePicture_Click
    If mode_select = "I" Then
        camEx.TakePicture
    Else
        camEx.SetFlashMode("torch")
        camEx.CommitParameters
        camEx.TakePicture
    End If
    btnTakePicture.Enabled = False
End Sub

Sub DisableCameraTimer1_Tick
    DisableCameraTimer.Enabled=False
    ProgressDialogHide
End Sub

Sub pictimer_Tick
    TakePicTimer.Enabled=False
    PicTake
End Sub

Sub PicTake
    camEx.TakePicture
    btnTakePicture.Enabled = False
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    camEx.SetFlashMode("off")
    camEx.CommitParameters
        Else  
    End If
End Sub
 
Last edited:

Rusty

Well-Known Member
Licensed User
Is there a way to find a "new thread" as suggested above?
It would be nice if a URL was posted in the "final" thread pointing to the new thread so we/I could easily find the "new thread" with a simple click...
Just sayin'... :)
Rusty
 

Erel

Administrator
Staff member
Licensed User

NeoTechni

Well-Known Member
Licensed User
I'm having trouble restarting the camera after it shuts down. Is there certain subs I should be calling to properly shut it down besides camEx.Release, and camEx.Initialize to turn it on? Or would you prefer this in a new thread?

EDIT: I think I found the issue. The panel needed to be visible.
 
Last edited:

Croïd

Active Member
Licensed User
Hello
Do you know, how I can add a precision circle for focus mode? ( as Google camera)
Thanks

focus.jpg
 

JUAN CARLOSORDOÑEZ

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

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).
Hello. When I run your example its shows "
B4A Versión: 7.30
Parseando código. (0.02s)
Compilando código (0.07s)
Compilado código de diseños (0.01s)
Organizando librerías. (0.00s)
Generando el fichero R. (0.05s)
Compilando el código Java generado. (1.38s)
Convirtiendo byte code a dex optimizado. Error
java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Exception in thread "main"

Please help me. thanks!!
 

hears

Active Member
Licensed User
can you share this library JAVA code? i try to make a same library now.
 
Status
Not open for further replies.
Top