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

ChrShe

Member
Licensed User
Longtime User
Ah ha! Looks like I have a mangled install. It's supposed to be 2.71 was Help->About says 2.5.
Cleaned up my install and it's working now. Thank you!
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

Whenever I take a picture, it is getting rotated 90 deg both in landscape as well as portrait mode. How do u stop this from happening?

Regards,
SK
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub LoadbitmapSample2(Dir As String,Filename As String,MaxWidth As Int,MaxHeight As Int,AutoRotate As Boolean) As Bitmap
  Dim bm As Bitmap
  bm=LoadBitmapSample(Dir,Filename,100%x,100%y)
  If AutoRotate Then
      Dim bm2 As BitmapExtended
      Dim exifdata1 As ExifData
      Try
        exifdata1.Initialize(Dir,Filename)
        'http://sylvana.net/jpegcrop/exif_orientation.html
        ' 1) transform="";;
        ' 2) transform="-flip horizontal";;
        ' 3) transform="-rotate 180";;
        ' 4) transform="-flip vertical";;
        ' 5) transform="-transpose";;
        ' 6) transform="-rotate 90";;
        ' 7) transform="-transverse";;
        ' 8) transform="-rotate 270";;
        Select Case exifdata1.getAttribute(exifdata1.TAG_ORIENTATION)
        Case exifdata1.ORIENTATION_ROTATE_180 '3
            bm=bm2.rotateBitmap(bm,180)
        Case exifdata1.ORIENTATION_ROTATE_90 '6
            bm=bm2.rotateBitmap(bm,90)
        Case exifdata1.ORIENTATION_ROTATE_270 '8
            bm=bm2.rotateBitmap(bm,270)
        End Select
      Catch
      End Try
  End If
  Return bm
End Sub

will exifdata1 take the bitmap data from the bytes directly? Cause I dont want to save the image to the SD card every frame.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Dang, I wish we had access to the view itself, and could just get the BMP
There has to be a way or augmented reality apps or barcode scanners wouldn't work
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Just found out the preview image is the full resolution of the camera (on my nexus 5 it's 1080p! hence why it's so slow) even though I set the preview image to a respectable 160*120. There's got to be a better way to get the preview image, especially without converting to JPG then back to BMP, as well as getting the camera orientation (currently I ask the user...)
 

sgt

Member
Licensed User
Longtime User
Hi Erel

I am wanting to read the camera's ISO setting to write to the saved image using the EXIF Lib. The camera settings for the ISO is set to 'Auto' and the value I get back using cam.GetParameter("iso") is 'auto' is there away to get what the camera used internal for this this setting. The native camera app writes a value into the Exif Iso tag field when the setting is set to 'auto'

Thanks sgt
 

sgt

Member
Licensed User
Longtime User
Yes it is saved when using the standard cam.SavePictureToFile method. It seems any 'Auto' settings get real number values written to the Exif tags. I will change my code to suit. Thanks Erel
 

GMan

Well-Known Member
Licensed User
Longtime User
I fwill get an error on runtime here in the class:
B4X:
cam.Initialize2(Panel1, "camera", id)
After checking, i found no Initialize2 sub or so, but deleting the 2 causes an error when compiling

P.S.: the error is a NullPointerEception
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Solved - with the normal Camera lib without class
 

Sergio Haurat

Active Member
Licensed User
Longtime User
Please add this code:

B4X:
Public Sub SavePictureToBitmap(Data() As Byte) As Bitmap
  Dim insData As InputStream
  insData.InitializeFromBytesArray(Data, 0, Data.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(insData)
    insData.Close
    Dim bmp_draw As BitmapDrawable
    bmp_draw.Initialize(bmp)
    Return bmp_draw.Bitmap
End Sub
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Yo, i read it : the Initialize2 is the same as initalize, except....
but as written: i solved it with the lib - normaö photo function is enough for that case: don't need Effects and Focus and Simbalalla :D
 

JordiCP

Expert
Licensed User
Longtime User
Is it possible to add a FaceDetection listener callback through Reflection?

I know it is only from API 14, but would be nice to be able to play with, in the devices that support it
 

Shadow&Max

Active Member
Licensed User
Longtime User
I'm getting an error on cam.Initialize2(Panel1, "camera", id)... Anyone know what this means and how to fix it? It simply dies on this line...

** Activity (addedit) Resume **
Error occurred on line: 32 (cameraexclass)
java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.SurfaceView
at anywheresoftware.b4a.objects.CameraW.shared(CameraW.java:97)
at anywheresoftware.b4a.objects.CameraW.Initialize2(CameraW.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:162)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18423)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5083)
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:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
 
Status
Not open for further replies.
Top