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

Shadow&Max

Active Member
Licensed User
Longtime User
Nope, doesn't fail in the example.... Guess I have some searching to do... Thanks Erel...
 

Shadow&Max

Active Member
Licensed User
Longtime User
I found my error... just had to change a few sub calls. Thanks Erel...
 

RAJAN MBA

Member
Licensed User
Longtime User
Yes, I work in a new version to put the database inside a project, not in internet.

and the check if light is low, yes, can be, look the google glass sensors:
The following sensors are supported on Glass:

Hi
Were you able to check wink and double wink sensor? How to raise the event? I think DriveSafe4Glass uses the wink detection. Would be great to emulate that app in B4A.
 

kiki78

Active Member
Licensed User
Longtime User
Just a remark for people that want to use Preview event for image analysis.
Data parameter of Preview event is image coded in YUV420sp, and fortunately this is standardized by Google for all preview camera data.

One interesting point of this format is that luminance is really easy to extract.
Just extract beginning of Data for number of pixels of preview image.
You obtain an array of byte with gray scaled levels for your analysis.

Regards,
 

Beja

Expert
Licensed User
Longtime User
The example in the first post of Erel worked fine.. with real-time movement without loss of quality. but I found the following:

Effects button threw ArithmeticException (devide by zero)

Samsong DUOS GT-18552 - running Android 4.1.2 - 8GB memory and 2GB SDCard.
 

Beja

Expert
Licensed User
Longtime User
Absolutely!
Is there additional library or class that I should have included?
real device exception:

Error occured
An error has occurred in sub:
main_btneffect_click (java line:
311)
java.langArithmaticException:
devide by zero
Continue?
====================================================

And the following is the log at compile time

LogCat connected to: emulator-5554
** Service (kioskservice) Create **
** Service (kioskservice) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject(Reflection.java:242)
at anywheresoftware.b4a.samples.camera.cameraexclass._findcamera(cameraexclass.java:184)
at anywheresoftware.b4a.samples.camera.cameraexclass._initialize(cameraexclass.java:374)
at anywheresoftware.b4a.samples.camera.main._initializecamera(main.java:477)
at anywheresoftware.b4a.samples.camera.main._activity_resume(main.java:292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at anywheresoftware.b4a.samples.camera.main.afterFirstLayout(main.java:104)
at anywheresoftware.b4a.samples.camera.main.access$100(main.java:16)
at anywheresoftware.b4a.samples.camera.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at anywheresoftware.b4a.agraham.reflection.Reflection.classforname(Reflection.java:170)
at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject(Reflection.java:238)
... 20 more
Caused by: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:235)
at java.lang.Class.forName(Class.java:182)
at anywheresoftware.b4a.agraham.reflection.Reflection.classforname(Reflection.java:167)
... 21 more
Caused by: java.lang.NoClassDefFoundError: android.hardware.Camera$CameraInfo
... 25 more
Caused by: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo in loader dalvik.system.PathClassLoader[/data/app/anywheresoftware.b4a.samples.camera-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 25 more
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
java.lang.NoSuchMethodException: getSupportedColorEffects
at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
at java.lang.Class.getDeclaredMethod(Class.java:748)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:802)
at anywheresoftware.b4a.samples.camera.cameraexclass._getsupportedcoloreffects(cameraexclass.java:302)
at anywheresoftware.b4a.samples.camera.main._btneffect_click(main.java:302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
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:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NoSuchMethodException: getSupportedColorEffects
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject(Reflection.java:242)
at anywheresoftware.b4a.samples.camera.cameraexclass._findcamera(cameraexclass.java:184)
at anywheresoftware.b4a.samples.camera.cameraexclass._initialize(cameraexclass.java:374)
at anywheresoftware.b4a.samples.camera.main._initializecamera(main.java:477)
at anywheresoftware.b4a.samples.camera.main._changecamera_click(main.java:449)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
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:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at anywheresoftware.b4a.agraham.reflection.Reflection.classforname(Reflection.java:170)
at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject(Reflection.java:238)
... 21 more
Caused by: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:235)
at java.lang.Class.forName(Class.java:182)
at anywheresoftware.b4a.agraham.reflection.Reflection.classforname(Reflection.java:167)
... 22 more
Caused by: java.lang.NoClassDefFoundError: android.hardware.Camera$CameraInfo
... 26 more
Caused by: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo in loader dalvik.system.PathClassLoader[/data/app/anywheresoftware.b4a.samples.camera-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 26 more
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.hardware.Camera$CameraInfo
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Nexus 5 bug: after calling cam.TakePicture, Camera_PictureTaken is never fired.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
When I use the PreviewImageToJpeg function to save a preview frame to the JPEG format, i notice that the orientation is wrong. The preview images are rotated on the screen, but the JPEG file has the wrong orientation. How can I fix this?
 

wimpie3

Well-Known Member
Licensed User
Longtime User
@wimpie3: look at this small utility library (included in the example) which rotates and converts directly the preview into RGB bitmap, without need to save first to disk

http://www.b4x.com/android/forum/threads/nv21utils-lib-camera-preview-to-argb888.36842/#post-216837

It is intended to work with the camera library and the cameraEx class, perhaps it may fit your needs in an easy way.

Thanks! Your demo works just fine... however, when I try to integrate the "NV21toRGB.proceed" function in my own program, I get this:

B4X:
Fatal signal 6 (SIGABRT) at 0x00001d9b (code=-6), thread 7579
 

JordiCP

Expert
Licensed User
Longtime User
Can you tell me which preview resolution and also bitmap size are you using? Also, if possible, the other arguments for this function call

Bitmap must be mutable
 

wimpie3

Well-Known Member
Licensed User
Longtime User
B4X:
Dim NV21toRGB As NV21util
Dim myBitmap As Bitmap
myBitmap.InitializeMutable(camEx.GetPreviewSize.Height, camEx.GetPreviewSize.Width)       
Main.NV21toRGB.proceed( previewImage, camEx.GetPreviewSize.Width, camEx.GetPreviewSize.Height, 1, myBitmap, True, camEx.Front, camEx.DispRotation, 0)

previewImage is a byte array containing a copy of the preview image. Preview size is 1024x768. The problem is the library works fine when used with your demo program. As soon as I integrate it in my own software, the segfault occurs.
 

JordiCP

Expert
Licensed User
Longtime User
Strange, everything seems correct. Could you please log the following values at runtime?

Log(PreviewPic.length)
Log(myBitmap.Width&","&myBitmap.Height)
Log(CamEx.DispRotation)
Log(camEx.GetPreviewSize.Height&","&camEx.GetPreviewSize.Width)

Also, do you work with the same values in the example?
 
Status
Not open for further replies.
Top