Tim Autin
Tim Autin

Reputation: 6165

Camera2 API focus not working on Google Pixel

I have an Android app with tap to focus capability. It works well on all the phones I tried (Nexus 5X, Samsung Galaxy S7, Asus ZenFone 3 Deluxe) except the Google Pixel.

Here is the code I'm using when the user taps the screen:

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();
    }
}

Any idea on what's going on on the Pixel?

EDI: I got activeArraySize this way:

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);

Upvotes: 3

Views: 1153

Answers (1)

Eddy Talvala
Eddy Talvala

Reputation: 18137

Do you leave AF_MODE to AUTO and the AF_REGIONS to {rectangle} in your subsequent repeating request as well?

If they're only set to AUTO on the trigger request, then you'll potentially be resetting autofocus immediately back to CONTINUOUS_PICTURE / no regions or whatever your repeating request is set to.

So make sure you've set AF_MODE to AUTO and AF_REGIONS to {rectangle} for your repeating request, though do not set AF_TRIGGER to START for more than the one capture() call.

Upvotes: 1

Related Questions