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

Mahares

Expert
Licensed User
Longtime User
I tried the code, but it did not help, unless my placement of the code is suspect:
B4X:
Sub Camera1_PictureTaken (Data() As Byte)
camEx.SavePictureToFile(Data, Main.DBFilePath, PicName)
LoadbitmapSample2(Main.DBFilepath,PicName,100%x,100%y,True)  'CHECK THIS
camEx.StartPreview .
.
End Sub

And in this sub:
B4X:
Sub LoadbitmapSample2(Dir As String,Filename As String,MaxWidth As Int,MaxHeight As Int,AutoRotate As Boolean) As Bitmap

I replaced this line:
B4X:
bm=LoadBitmapSample(Dir,Filename,100%x,100%y)
by:
B4X:
bm=LoadBitmapSample(Main.DBFilepath,PicName,100%x,100%y)
I replaced this line:
B4X:
exifdata1.Initialize(Dir,Filename)
by this:
B4X:
exifdata1.Initialize(Main.DBFilepath,PicName)
 

Mahares

Expert
Licensed User
Longtime User
You do not need to replace the variable names. You just need to pass the correct values.
If what you mean by the above just do the below in the call to the sub inside the Sub Camera1_PictureTaken (Data() As Byte) , I did that.
B4X:
LoadbitmapSample2(Main.DBFilepath,PicName,100%x,100%y,True)
Nothing works. On the device viewer it is in the correct view (upright). Using any viewer when copied to PC, the picture is rotated 90 degrees to the left, whether I use the added sub or not. Other devices do not show this peculiar behavior. It does not look like there is a solution for the Samsung Galaxy Tab2. I am surprised nobody else who uses Galaxy Tab2 have not reported something similar to this.
 

Mahares

Expert
Licensed User
Longtime User
Some devices save the orientation information in the EXIF metadata instead of actually rotating the image. This method will not change the way the bitmap is saved. It only allows you to load images correctly based on the orientation information.
If I understand you correctly, I have to live with that for the Galaxy. If it is not doable, I just have to accept it. This is taking too long to resolve for something that should not be so complex.
 

kkolle

Member
Licensed User
Longtime User
Hi,
can someone tell me why i get Exception, when i try the CameraEx Class example?

cameraexclass_setdisplayorientation (java line: 391)


java.lang.NoSuchFieldError: anywheresoftware.b4a.BA.activityBA


at anywheresoftware.b4a.agraham.reflection.Reflection.GetActivity(Reflection.java:610)
at anywheresoftware.b4a.samples.camera.cameraexclass._setdisplayorientation(cameraexclass.java:391)
at anywheresoftware.b4a.samples.camera.cameraexclass._camera_ready(cameraexclass.java:87)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.objects.CameraW$2$1.run(CameraW.java:137)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
 

gelay

Member
Licensed User
Longtime User
Hi guys

I have a little problem with the whitebalance the get method is working perfectly, but I can't set it.

cameraex
B4X:
Public Sub Getwhitebalance As String
   Return GetParameter("whitebalance")
End Sub


Public Sub setWhiteBalance(white_balance As String)
   SetParameter("whitebalance", white_balance)
End Sub

main
B4X:
Dim white_balance() As String = Array As String("auto", "INCANDESCENT", "FLUORESCENT", "WARM_FLUORESCENT", "DAYLIGHT", _
      "CLOUDY_DAYLIGHT", "TWILIGHT", "SHADE")
   
   Dim white_balanc As String = white_balance(Rnd(0, white_balance.Length))
   camEx.setWhiteBalance(white_balanc)
   ToastMessageShow(white_balanc, False)
   camEx.CommitParameters

runtime exeption set parameter failed
 

srossi

Member
Licensed User
Longtime User
Hello im new in basic android im trying to run camera ex example and i get an error
classnotfoundexception:
android.hardware.camera$camerainfo

can somebody help me thx
 

hypergreatthing

Member
Licensed User
Longtime User
How do i implement:
final void autoFocus(Camera.AutoFocusCallback cb)
Starts camera auto-focus and registers a callback function to run when the camera is focused.

I put in a SetFocusArea but i can't focus on it without calling autoFocus apparently.
 

MotoMusher

Active Member
Licensed User
Longtime User
I wanted to note that this has hung my camera twice in 4 pictures taken. First attempt was focus + tp. Rebooted. Took 2 successfully with take picture (front and back). Next with back locked app. No error, just frozen for several minutes until I killed it with task manager. Camera was locked as expected which reboot fixes.

I cannot get normal take picture to reproduce error, but focus + tp locks every time.

Not complaining, just wanted to note it since I know this was new. I was in debug mode running the example from the first post of this page, downloaded today. Camera library is 2.20 as shown in libs tab. Reflection version is 2.40 as shown in libs tab

Samsung Galaxy S 2 (Tmobile)
Android version 4.04

I was looking to move to this from ACL since I can't get the front cam working with ACL, and don't need anything other than macro, autofocus and torch.

I am happy to do any troubleshooting you would like.
 
Last edited:

padvou

Active Member
Licensed User
Longtime User
You are correct. The cameras are only closed after 5 seconds. This allows your app to quickly reopen the camera when the orientation changes.

Add this code to CameraEx and call it:
B4X:
Public Sub CloseNow
   cam.Release
   r.Target = cam
   r.RunMethod2("releaseCameras", True, "java.lang.boolean")
End Sub

How do you "un-release" it after running CloseNow? I mean re-take control of the camera.
Also, there are several occasions when the app freezes when it takes a picture and I can't figure out why.. As a workaround, how do I release the camera when the app freezes, so I don't need to reload my device?
I attach the relevant unfiltered log
 

Attachments

  • log.zip
    3.5 KB · Views: 230
Last edited:
Status
Not open for further replies.
Top