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

yckhor

Member
Licensed User
Longtime User
Thanks, Erel. Figure out it's the panel dimension that caused the non proportional image. I changed the panel size to 640x480 and got it working fine on landscape & portrait. I will try the setPreviewSize. Thank you very much!
 

yckhor

Member
Licensed User
Longtime User
Hi Erel,
SetPreviewSize does not help with the out of proportion issue. I tried 800x480 full screen which is the native resolution of my HTC Desire HD and it did not show correctly. However, if I resize the panel, it will display the live view image correctly in both portrait & landscape orientation. What else can I do? If you try rotating the Camera sample app, it shows the same behaviour when you orientate the phone from portrait to landscape, make sure you focus on one object while doing so, you will see what I am taking about. Saved jpeg seems OK, only the preview. I am newbie, anything I missed? Help!!!!

p/s: the first 2 photos were snapshots of my app with a panel sized to 640x480 while the next 2 photos were snapshots of the camera ex sample. All photos shown a shot of a iphone5 in both landscape & portrait and notice the shape of the phone.
 

Attachments

  • Screenshot_2013-06-27-19-48-44.jpg
    Screenshot_2013-06-27-19-48-44.jpg
    17 KB · Views: 276
  • Screenshot_2013-06-27-19-48-34.jpg
    Screenshot_2013-06-27-19-48-34.jpg
    16.4 KB · Views: 241
  • Screenshot_2013-06-27-19-47-19.jpg
    Screenshot_2013-06-27-19-47-19.jpg
    18.8 KB · Views: 253
  • Screenshot_2013-06-27-19-47-05.jpg
    Screenshot_2013-06-27-19-47-05.jpg
    19.1 KB · Views: 246
Last edited:

yckhor

Member
Licensed User
Longtime User
Are you sure that 800x480 is a supported preview size?

HTC Desire HD has a 4.3" 480x800 screen.
I had made a bit of changes and found the preview sizes are avail below

You are right that 800x480 is not a supported preview size. If in that case, what res should I be choosing?

EDIT: found it! I used W divide by H and get the index. Matching 800x400 is 640x384, now everything looks nice. Thanks!

Preview W: 3264 H: 2448
Preview W: 3264 H: 1952
Preview W: 2592 H: 1952
Preview W: 2592 H: 1936
Preview W: 2592 H: 1728
Preview W: 2592 H: 1552
Preview W: 2048 H: 1536
Preview W: 2048 H: 1360
Preview W: 2048 H: 1216
Preview W: 1600 H: 1200
Preview W: 1584 H: 1056
Preview W: 1280 H: 960
Preview W: 1280 H: 848
Preview W: 1280 H: 768
Preview W: 1024 H: 768
Preview W: 640 H: 480
Preview W: 640 H: 416
Preview W: 640 H: 384
Preview W: 512 H: 384
Preview W: 400 H: 400
Preview W: 272 H: 272
 
Last edited:

Morgan Pileggi

New Member
Licensed User
Longtime User
CameraEx example won't compile

Hello;
I'm trying to get the CameraEx example to run. I downloaded the new camera library (though in the ide it lists the version as v2.10 not v2.20)
And thought i set my lib path correctly; however when i try to run the app
get an error with the Reflector Type:

Parsing code. Error
Error parsing program.
Error description: Unknown type: reflector
Are you missing a library reference?
Occurred on line: 9
Private r As Reflector


Is there another Lib i should be including for this example?
(i have Camera , core and phone selected)

Thanks for any help you can give me;

b4a has been invaluable
Morgan Pileggi
 

Morgan Pileggi

New Member
Licensed User
Longtime User
Thanks; that helps a lot.

I had to comment out the autofocus call:

'Calls AutoFocus and then takes the picture if focus was successfull.
Public Sub FocusAndTakePicture
'cam.AutoFocus
End Sub


Now the example works (for me on a galaxy sIII w/android 4.1.2)

I think i saw something on this thread about the autofocus being a problem


Morgan Pileggi
 

yckhor

Member
Licensed User
Longtime User
hi, I had just purchased a Samsung Galaxy S4 GT9505 and having some strange problems running my app that uses CameraEx. See attached photo. There is a green band covering half of the camera view.

The same app runs flawlessly in my HTC Desire HD with Android 4.2.2

Anyone experienced this before?
 

Attachments

  • Screenshot_2013-08-05-11-55-46.png
    Screenshot_2013-08-05-11-55-46.png
    167.3 KB · Views: 259

yckhor

Member
Licensed User
Longtime User
Is this your UI (with the camera button)?

How are you initializing the camera?

Yes, this is my UI. It works ok with my another android

Sub Activity_Resume
InitializeCamera
End Sub

Private Sub InitializeCamera
CameraEx.Initialize(pnlCamera, frontCamera, Me, "Camera")
frontCamera = CameraEx.Front
End Sub

Sub Camera_ready (Success As Boolean )
If Success Then
CameraEx.SetJpegQuality(90)
CameraEx.SetPreviewSize (640, 384)
CameraEx.CommitParameters
CameraEx.StartPreview
Log(CameraEx.GetPreviewSize)
Else
ToastMessageShow("Cannot open camera.", True)
End If
End Sub

basically, I just copy from the sample.
 

yckhor

Member
Licensed User
Longtime User
Yes, this is my UI. It works ok with my another android

Sub Activity_Resume
InitializeCamera
End Sub

Private Sub InitializeCamera
CameraEx.Initialize(pnlCamera, frontCamera, Me, "Camera")
frontCamera = CameraEx.Front
End Sub

Sub Camera_ready (Success As Boolean )
If Success Then
CameraEx.SetJpegQuality(90)
CameraEx.SetPreviewSize (640, 384)
CameraEx.CommitParameters
CameraEx.StartPreview
Log(CameraEx.GetPreviewSize)
Else
ToastMessageShow("Cannot open camera.", True)
End If
End Sub

basically, I just copy from the sample.


seems to be caused by SetPreviewSize. If I remark this line, it works.
S4 has a resolution of 1920x1080, if I use 100%x and 100%y for the width & height, the preview looks ok but again, the image looks distorted, square looks like a rectangle. Any comments?
 

Attachments

  • Screenshot_2013-08-05-17-25-29.jpg
    Screenshot_2013-08-05-17-25-29.jpg
    14.1 KB · Views: 227
  • Screenshot_2013-08-05-17-25-44.jpg
    Screenshot_2013-08-05-17-25-44.jpg
    12.5 KB · Views: 215

yckhor

Member
Licensed User
Longtime User
seems to be caused by SetPreviewSize. If I remark this line, it works.
S4 has a resolution of 1920x1080, if I use 100%x and 100%y for the width & height, the preview looks ok but again, the image looks distorted, square looks like a rectangle. Any comments?

Got it resolved by comment off SetPreviewSize and tweaking the ratio and resize the panel.
Thanks for the help!
 

Attachments

  • Screenshot_2013-08-05-20-46-46.png
    Screenshot_2013-08-05-20-46-46.png
    181.5 KB · Views: 221
  • Screenshot_2013-08-05-20-46-40.png
    Screenshot_2013-08-05-20-46-40.png
    170.3 KB · Views: 222

kanaida

Active Member
Licensed User
Longtime User
I'm getting a strange casting error when it tries to initialize.
cam.initialize2(Panel1,"camera",id)
Can't cast ImageView to SurfaceView.

The code is pretty much a copy of the sample at this point.
B4X:
Sub Globals
    Private pCam As Panel
    Private cam As CameraExClass
End Sub

Sub InitializeCamera
    cam.Initialize(pCam,False,Me,"Camera1")
    frontCamera =cam.Front
End Sub

libs used in the app:
Camera 2.20
Core 2.71
Http 1.31
Phone 2.10
Reflection 2.40
SQL 1.20
StringUtils 1.02
TouchImageView 2.00

CameraExClass module 1.20

The regular camera object worked in a little test I did. Just not sure what's different here.
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

I am getting this below error
B4X:
** Activity (takepicture) Create, isFirst = true **
** Activity (takepicture) Resume **
cameraexclass_initialize (B4A line: 32)
cam.Initialize2(Panel1, "camera", id)
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 com.bayalu.fpacmanager.cameraexclass._initialize(cameraexclass.java:690)
    at com.bayalu.fpacmanager.takepicture._initializecamera(takepicture.java:511)
    at com.bayalu.fpacmanager.takepicture._activity_resume(takepicture.java:323)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at com.bayalu.fpacmanager.takepicture.afterFirstLayout(takepicture.java:95)
    at com.bayalu.fpacmanager.takepicture.access$100(takepicture.java:16)
    at com.bayalu.fpacmanager.takepicture$WaitForLayout.run(takepicture.java:74)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4898)
    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:1008)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
    at dalvik.system.NativeStart.main(Native Method)

I am using a sidebar in my app. I am initializing the camera as shown below

B4X:
Private Sub InitializeCamera
    Sidebar.ContentPanel.LoadLayout("CameraLayout")
    camEx.Initialize(cameraPanel, frontCamera, Me, "Camera1")
    frontCamera = camEx.Front
End Sub

The Camera Layout contains a panel(cameraPanel) and a botton.

Let me know where i am going wrong

Regards,
SK
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi Erel,

I have added a lot of additional functionality to the CameraEx library, as a well as extensive comments to assist with the common parameters available.

Additional subs added:
  • getSceneMode , setSceneMode , GetSupportedSceneModes
  • getZoom , setZoom , GetMaxZoom, isZoomSupported
  • getWhiteBalance , setWhiteBalance , GetSupportedWhiteBalance
  • getFocusMode , setFocusMode , GetSupportedFocusModes
  • GetPreviewSize

Also in most cases I have changed Get/Set compatible subs to the new gettter and setter styles which I think fit in more with the B4a syntax.
For example, camEx.ColorEffect="sepia" instead of camEx.SetColorEffect("sepia")

The main example code also has a Camera_LogFeatures sub which dumps the supported device cameras features, which is handy to quickly review what your camera is capable of supporting.

(Note: In some parameter comments, where other: is stated, these are additional parameters from certain manufacturers cameras, like Galaxy S4 for instance)

SEE ATTACHED
 

Attachments

  • CameraEx_JB.zip
    12.1 KB · Views: 1,221
Last edited:
Status
Not open for further replies.
Top