我使用这些方法激活SurFaceView上的on Touch焦点
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;
}
它在大多数设备上都能正常运行,但我发现它并不适用于使用Android 5.0版本的Huawei Honor 5x。
奇怪的部分是方法而不是完成它的工作,它需要我到上一页(活动)。
如何正确使用CameraSource和SurfaceView进行触控自动对焦?
答案 0 :(得分:1)
我想这个问题是因为我使用的是不推荐使用的类:相机