i have a camera app using camera2 Api, i implemented onTouchListener to the TextureView i use for Previewing from Camera, what i want is when a user taps the screen to be able to focus on that area exactly like the built in Android Camera. i don't know what it's going wrong, it works when i use AF_MODE_AUTO but it's not precise , it focuses only whats on the center of the screen.
Here it's my code:
mTextureView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Rect rect = cameraCharacteristics
.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Size size = cameraCharacteristics
.get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE);
int areaSize = 200;
int right = rect.right;
int bottom = rect.bottom;
int viewWidth = mTextureView.getWidth();
int viewHeight = mTextureView.getHeight();
int ll,
rr;
Rect newRect;
int centerX = (int) event.getX();
int centerY = (int) event.getY();
ll = ((centerX * right) - areaSize) / viewWidth;
rr = ((centerY * bottom) - areaSize) / viewHeight;
int focusLeft = clamp(ll, 0, right);
int focusBottom = clamp(rr, 0, bottom);
newRect = new Rect(focusLeft, focusBottom, focusLeft
+ areaSize, focusBottom + areaSize);
MeteringRectangle meteringRectangle = new MeteringRectangle(
newRect, 500);
MeteringRectangle[] meteringRectangleArr = { meteringRectangle };
System.out.println("metering rectARR" + meteringRectangleArr);
mPreviewCaptureRequestBuilder.set(
CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
mPreviewCaptureRequestBuilder.set(
CaptureRequest.CONTROL_AF_REGIONS,
meteringRectangleArr);
mPreviewCaptureRequestBuilder.set(
CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
try {
mCameraCaptureSession.setRepeatingRequest(
mPreviewCaptureRequest,
mSessionCaptureCallback,
mBackgroundHandler);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
return true;
}
});
}
mPreviewCaptureRequestBuilder.set( CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
– Difficult