Android Question Manual Focus (Java)

Jack Howard

Member
Licensed User
Longtime User
I am using CameraEX to detect particle sizes, simply by counting pixels next to each other in an image.
But in some conditions the focus is not optimal

I use only Picture Preview and NV21toRGB, rather than Take Picture.

CameraEX does not support manual focus. But Java Camera2 does.

If I can find someone to help me write a Java routine just for changing focus using Camera2, can I still use CameraEx in the same B4A program? or will I have to use Camera2 for all the camera routines which would be a massive undertaking?
 

JordiCP

Expert
Licensed User
Longtime User
I think it is not possible to access Camera2 API while using CameraEx, which is based on the first Camera API.

By manual focus, do you mean to be able to change the focus point (for instance touching on screen), or directly modifying focal length through the API? The first can still be done with Camera(1) API, according to what I have read.

For the second, and before doing anything, you should read this from HERE
B4X:
A given camera device may provide support at one of two levels: limited or full. If a device only supports the limited level, then Camera2 exposes a feature set that is roughly equivalent to the older Camera API, although with a cleaner and more efficient interface. Devices that implement the full level of support provide substantially improved capabilities over the older camera API. Applications that target the limited level devices will run unchanged on the full-level devices; if your application requires a full-level device for proper operation, declare the "android.hardware.camera.level.full" feature in your manifest.

In short, even using camera2 api, not all devices will give full-access to all of its capabilities. So if you are using a specific device for counting particles, you should first check if its hardware allows full-access


In case it does and you need manual focus (not the touch-assisted), perhaps it would be worth to make the setup from inline java porting only the required methods, assuming that there are not many. But it is not straightforward.
 
Upvote 0

Jack Howard

Member
Licensed User
Longtime User
I need to check the image, then focus closer and check the image again, then move focus further away and recheck image.

Any camera could be used, so I will get the correct camera.

I love your idea of using inline Java, it will allow me to try snippets of code bit by bit.

I have found
Camera2BasicFragment.java
Camera2Controller.java
CameraController2.java
Which may have most of the code I need.
I think I need to use SET LENS_FOCUS_DISTANCE
and to do that I must use Camera Manager to open the camera
Then select the camera to use

Then I can deselect camera AF focus with
Camera2Controller.previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
Then I can set the focus with
Camera2Controller.previewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, fDistance);
CaptureRequest.LENS_FOCUS_DISTANCE

I see that CameraEX has a CloseNow function, I wondered if that is called If I could then run the inline code as you suggested and then re run CameraEX
I am only updating the screen every 10 frames, so I could hold a still image while I change the focus.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
The only "question" is if internal camera parameters will hold between instances from camera to camera2 and back again, or they are set to defaults each time they are instantiated.

But it's worth testing it. ;)
 
Upvote 0
Top