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

aeropic

Active Member
Licensed User
Longtime User
Hi All,

As I finally succeeded to get "setfocusareas" work with the help of Agraham's code and many other answers (see post-251700), I upload here an updated version of CameraEX class including:
  • GetMaxNumFocusAreas
  • GetFocusAreas
  • SetFocusArea(area As Rect, weight As Int )
Here are some explanations found on Android pages
'Before using this API or setFocusAreas(List), apps should call getMaxNumFocusAreas() to know the maximum number of focus areas first.
'If the value is 0, focus area is not supported.
'Each focus area is a rectangle with specified weight. The direction is relative to the sensor orientation, that is, what the sensor sees.
'The direction is not affected by the rotation or mirroring of setDisplayOrientation(int).
'Coordinates of the rectangle range from -1000 to 1000. (-1000, -1000) is the upper left point. (1000, 1000) is the lower right point.
'The width and height of focus areas cannot be 0 or negative.

'The weight must range from 1 to 1000. The weight should be interpreted as a per-pixel weight - all pixels in the area have the specified weight.
'This means a small area with the same weight as a larger area will have less influence on the focusing than the larger area.
'Focus areas can partially overlap and the driver will add the weights in the overlap region.

'A special case of a null focus area list means the driver is free to select focus targets as it wants.
'For example, the driver may use more signals to select focus areas and change them dynamically.
'Apps can set the focus area list to null if they want the driver to completely control focusing.

'Focus areas are relative to the current field of view (getZoom()).
'No matter what the zoom level is, (-1000,-1000) represents the top of the currently visible camera frame.
'The focus area cannot be set to be outside the current field of view, even when using zoom.

'Focus area only has effect if the current focus mode is FOCUS_MODE_AUTO, FOCUS_MODE_MACRO, FOCUS_MODE_CONTINUOUS_VIDEO, or FOCUS_MODE_CONTINUOUS_PICTURE.

The main in the ZIP includes an example of what can be done with this:
- launch the camera
- press on the screen and a rectangle should appear to indicate where the focus will be done
- if successfull it becomes green, else it turns to red

warning:

As the 3 new routines are supported only on API = 14 and more, I've limited the usage to "new" devices with a test of the API version so that the class remains compatible with 2.3+ devices...
B4X:
'check api level and focus supported
        FocusSupported = False
        Dim r As Reflector
         api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
        If api >= 14 Then
            If camEx.GetMaxNumFocusAreas <> 0 Then FocusSupported = True
        End If
        Log("focus supported : " & FocusSupported)
I would appreciate if somebody who owns a device < API 14 could try the code to check if everything works well with this "is focus supported" test ...
==> the rectangle on the screen should not be displayed

Please note that the code only supports one focusarea. My S3 does not support more !

Have fun
ALain
 

Attachments

  • cameraEX123.zip
    15.8 KB · Views: 292

JoanRPM

Active Member
Licensed User
Longtime User
Is there any way that the preview is in the margins of the panel?
I tried the example, changing the margins of Panel1, and the image always fills the entire screen.

Thank you.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Hello, Joan.

I think I have the same question...
By changing the margins of panel1 - do you mean setlayout of panel1?

So, may I ask the question:

How do I crop the picture taken? Obviously if I change the layout of panel1, the preview is distorted accordingly and the picture taken is not cropped.

Any help will be welcomed. For my app, what I would like to do is take a facial portrait (240 by 320) while the tablet is in Landscape mode.

Thank you.
 

JoanRPM

Active Member
Licensed User
Longtime User
Yes tdocs2, when I change the size of Panel1 layout, the screen image is not modified and shown in full screen.

What I need is to show the image in a small area of the tablet screen.
Is there any way to reduce the view? the same size as Panel1?

An option to put the picture vertically, it may be putting a mask over the image (a black rectangle on each side of the image). So you could see the facial in portrait. Then you will have to crop the image and save it. It may not be the best solution, but ...

Thanks.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Joan and Barry.

Barry,

I do not want to load my app with another library to crop the picture.

I have used Canvas in a limited fashion in the past and I suspect that may provide the crop capability I am looking for. I am not that familiar with bitmap manipulation in B4A, so I would have to experiment.

Joan,

What do you suggest to use to crop the image? My idea to use Canvas (if it works) would be to take the picture, save it to a Bitmap, and then try to manipulate it. But, as I said, Image Processing is not in my skill set.

Would it not be great if the Camera-Ex class is expanded to add a rectangle and take the picture within the rectangle?

Thanks again.

Sandy
 

JoanRPM

Active Member
Licensed User
Longtime User
Sandy, sorry for my bad English.

My idea was just how to display on the screen in real time a smaller image.
The problem is, it seems, can not do a smaller image in the preview. Always fills the entire screen.

My application is that each user could have their photo inside a template card type, similar to Gmail contacts, but could visualize it in real time (he will see all the card and in the upper left the face in real time). Then press a button to take the photo and the system will save it in a database with all his data.

Greetings.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Joan,

Your English is excellent.

If this existed:
Would it not be great if the Camera-Ex class is expanded to add a rectangle and take the picture within the rectangle?
I believe it would easily solve both our problems.

I think our needs are similar. I hope we get an answer.

Best regards.

Sandy
 

hanyelmehy

Active Member
Licensed User
Longtime User
i get error at this line
B4X:
cam.Initialize2(Panel1, "camera", id)
error
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.view.SurfaceView
 
Status
Not open for further replies.
Top