Set the camera focus area in Android
android, android-camera, focus
Solution
this work for me focus on camera 1 Api
@Override
public boolean onTouchEvent(MotionEvent event) {
//variable for storing the time of first click
//constant for defining the time duration between the click that can be considered as double-tap
final int MAX_DURATION = 200;
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
startTime = System.currentTimeMillis();
}
else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if(System.currentTimeMillis() - startTime <= MAX_DURATION)
{
//capture image on Double Tap
mCamera.autoFocus(ShotActivity_CameraActivity.this);
}
}
}
return true;
}
then this method to handle touch focus area
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
Rect touchRect = new Rect(
(int) (x - 100),
(int) (y - 100),
(int) (x + 100),
(int) (y + 100) );
final Rect targetFocusRect = new Rect(
touchRect.left * 2000/mTextureView.getWidth() - 1000,
touchRect.top * 2000/mTextureView.getHeight() - 1000,
touchRect.right * 2000/mTextureView.getWidth() - 1000,
touchRect.bottom * 2000/mTextureView.getHeight() - 1000);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
try {
List<Camera.Area> focusList = new ArrayList<Camera.Area>();
Camera.Area focusArea = new Camera.Area(targetFocusRect, 1000);
focusList.add(focusArea);
params.setFocusAreas(focusList);
params.setMeteringAreas(focusList);
mCamera.setParameters(params);
/* mCamera.autoFocus(this);*/
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Unable to autofocus");
}
}
}
i hope this help
Problem
following several tutorials and examples I came up with the next algorithm to set the camera focus on a specific spot, the problem is that the camera completely ignores the spot and performs a normal overall focus instead of the rect area which I have specified. Is there anything else that I am missing in the algorithm? This has been tested on several phones all with Android 4.0 and above, so the focus area API is supported on these devices. Note, the app I am writing works in landscape mode only. ``` @Override public boolean onTouchEvent(final MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { float x = event.getX(); float y = event.getY(); float touchMajor = event.getTouchMajor(); float touchMinor = event.getTouchMinor(); Rect touchRect = new Rect((int)(x - touchMajor / 2), (int)(y - touchMinor / 2), (int)(x + touchMajor / 2), (int)(y + touchMinor / 2)); this.submitFocusAreaRect(touchRect); } } private void submitFocusAreaRect(final Rect touchRect) { Camera.Parameters cameraParameters = camera.getParameters(); if (cameraParameters.getMaxNumFocusAreas() == 0) { return; } // Convert from View's width and height to +/- 1000 Rect focusArea = new Rect(); focusArea.set(touchRect.left * 2000 / cameraSurfaceView.getWidth() - 1000, touchRect.top * 2000 / cameraSurfaceView.getHeight() - 1000, touchRect.right * 2000 / cameraSurfaceView.getWidth() - 1000, touchRect.bottom * 2000 / cameraSurfaceView.getHeight() - 1000); // Submit focus area to camera ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>(); focusAreas.add(new Camera.Area(focusArea, 1000)); cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); cameraParameters.setFocusAreas(focusAreas); camera.setParameters(cameraParameters); // Start the autofocus operation camera.autoFocus(this); } ```