Reputation: 263
I'm using these methods to activate the on Touch focus on SurFaceView
private SurfaceView surfaceiew;
private CameraSource camSource;
private Camera camera;
private Camera.Parameters params;
private void initCameraFocusListener() {
surfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
cameraFocus(event, camSource, Camera.Parameters.FOCUS_MODE_AUTO);
return false;
}
});
}
// camera focus method
private boolean cameraFocus(MotionEvent event, @NonNull CameraSource cameraSource, @NonNull String focusMode) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();
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);
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));
Rect focusArea = new Rect();
focusArea.set(touchRect.left * 2000 / surfaceView.getWidth() - 1000,
touchRect.top * 2000 / surfaceView.getHeight() - 1000,
touchRect.right * 2000 / surfaceView.getWidth() - 1000,
touchRect.bottom * 2000 / surfaceView.getHeight() - 1000);
// Submit focus area to camera
ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(new Camera.Area(focusArea, 1000));
for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
camera = (Camera) field.get(cameraSource);
if (camera != null) {
params = camera.getParameters();
params.setFocusMode(focusMode);
params.setFocusAreas(focusAreas);
camera.setParameters(params);
// Start the autofocus operation
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
return true;
}
return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
return false;
}
it's working perfectly on most devices , but i found out that it's not working on Huawei Honor 5x which uses Android 5.0 version.
the weird part is the method instead of doing its job , it takes me to previous page(activity) .
How to do the on Touch Auto Focus with CameraSource And SurfaceView correctly ?
Upvotes: 1
Views: 144
Reputation: 263
i guess the problem is because I'm using a Deprecated class : Camera
Upvotes: 1