I'm creating a camera app that will use a tap to focus function, much like many of the standard camera apps come with. I first get the (x,y) coordinate of a touch event, then deliver this value to the UI. Then, I try to set the focus area, like this:
Rect newRect = new Rect(left,top,right, bottom);
Camera.Parameters params = mCamera.mCameraInstance.getParameters();
Camera.Area focusArea = new Camera.Area(newRect, 1000);
List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(focusArea);
params.setFocusAreas(focusAreas);
However, it appears to have no effect. So, I hardcoded some values into the rectangle (this way, I can rule out bad coordinates from the touch event). According to the docs, it is looking for a coordinate in the space (-1000, -1000)(upper left) to (1000, 1000)(bottom right).
So I replaced my rectangle above with this one:
//should target a 100x100 square in the center of the screen
Rect newRect = new Rect(-50,-50,50, 50);
And still, seems to have no effect. I know my camera supports setting focus areas, because first of all, the camera app uses it successfully, and also params.getMaxNumFocusAreas() returns 1.
If anyone has used this with success in the past, please let me know!!
I found this similar question that seems to also indicate that this API simply doesn't work on Android 4.0 devices and up (im testing on 4.1.1, Galaxy S3). And yet, the camera apps on these devices still definitely has tap-to-focus functionality. What am I missing???
I had this problem today :/
And after hours of struggling, I found the solution!
It's strange, but it appears that setting focus-mode to "macro" right before setting focus-areas solved the problem ;)
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
params.setFocusAreas(focusAreas);
mCamera.setParameters(params);
I have Galaxy S3 with Android 4.1.2
I hope this will work for you either :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With