我有一个具备触摸对焦功能的安卓应用。在我尝试过的所有手机上(Nexus 5X,三星Galaxy S7,华硕ZenFone 3 Deluxe)都能正常工作,除了Google Pixel。
下面是用户点击屏幕时我使用的代码:
public void focusAt(Point point) {
try {
// compute metering rectangle from the focus point
// see here: https://github.com/PkmX/lcamera/blob/master/src/pkmx/lcamera/MainActivity.scala (from line 759)
int meteringRectangleSize = 300;
int left = activeArraySize.left;
int right = activeArraySize.right;
int top = activeArraySize.top;
int bottom = activeArraySize.bottom;
float x = (float)point.x / mPreviewSize.getWidth();
float y = (float)point.y / mPreviewSize.getHeight();
MeteringRectangle rectangle = new MeteringRectangle(
Math.max(0, Math.round(left + (right - left) * y - meteringRectangleSize / 2)),
Math.max(0, Math.round(bottom - (bottom - top) * x - meteringRectangleSize / 2)),
meteringRectangleSize,
meteringRectangleSize,
1
);
// create a new capture request
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// set the Auto focus mode to auto and set the region computed earlier
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{rectangle});
// add the preview surface as a target and start the request
mPreviewBuilder.addTarget(previewSurface);
mPreviewSession.capture(mPreviewBuilder.build(), null, mBackgroundHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
你知道Pixel上正在发生什么吗?
EDI:我是这样获取activeArraySize的:
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
String cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);