Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camerax] Implement focus mode configuration #120467

Closed
1 of 2 tasks
camsim99 opened this issue Feb 10, 2023 · 7 comments · Fixed by flutter/packages#6176
Closed
1 of 2 tasks

[camerax] Implement focus mode configuration #120467

camsim99 opened this issue Feb 10, 2023 · 7 comments · Fixed by flutter/packages#6176
Assignees
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter p: camera The camera plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team

Comments

@camsim99
Copy link
Contributor

camsim99 commented Feb 10, 2023

In the camera plugin API, there are two methods related to the configuration of the focus mode. These should be implemented in the CameraX plugin:

  • setFocusMode(....)
  • setFocusPoint(..)
@camsim99 camsim99 added c: new feature Nothing broken; request for a new capability platform-android Android applications specifically plugin p: camera The camera plugin c: proposal A detailed proposal for a change to Flutter P2 Important issues not at the top of the work list labels Feb 10, 2023
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
@Hixie Hixie removed the plugin label Jul 6, 2023
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-android Owned by Android platform team triaged-android Triaged by Android platform team labels Jul 8, 2023
@jstevensconverus
Copy link

Hello, is there an update on this issue? My company is unable to use this plugin until the issue is resolved.

@camsim99
Copy link
Contributor Author

Hello, is there an update on this issue? My company is unable to use this plugin until the issue is resolved.

I have just begun working on this feature; will update here when a PR is submitted for review.

@camsim99 camsim99 moved this from To do to In progress in Android Camera Re-write Dec 13, 2023
@camsim99 camsim99 self-assigned this Dec 13, 2023
@camsim99 camsim99 added P1 High-priority issues at the top of the work list and removed P2 Important issues not at the top of the work list labels Jan 2, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jan 24, 2024
…ing focus & exposure points and offset (#5659)

Wraps CameraX APIs needed to at least implement setting focus and exposure points and offset (`setFocusPoint`,`setExposurePoint`, `setExposureOffset`). Listed by class:

`CameraControl`
- [`startFocusAndMetering`](https://developer.android.com/reference/androidx/camera/core/CameraControl#startFocusAndMetering(androidx.camera.core.FocusMeteringAction))
- [`cancelFocusAndMetering`](https://developer.android.com/reference/androidx/camera/core/CameraControl#cancelFocusAndMetering())
- [`setExposureCompensationIndex`](https://developer.android.com/reference/androidx/camera/core/CameraControl#setExposureCompensationIndex(int))

`FocusMeteringAction`
- `create` method for [this class](https://developer.android.com/reference/androidx/camera/core/FocusMeteringAction.Builder#Builder(androidx.camera.core.MeteringPoint,int))

`FocusMeteringResult`
- `create` method for [this class](https://developer.android.com/reference/androidx/camera/core/FocusMeteringResult)
- [`isFocusSuccessful`](https://developer.android.com/reference/kotlin/androidx/camera/core/FocusMeteringResult#isFocusSuccessful())

`MeteringPoint`
- `create` method for the points that [`SurfaceOrientedMeteringPointFactory`](https://developer.android.com/reference/androidx/camera/core/SurfaceOrientedMeteringPointFactory) creates
- [`getDefaultPointSize`](https://developer.android.com/reference/androidx/camera/core/MeteringPointFactory#getDefaultPointSize())

Part of flutter/flutter#120468 and flutter/flutter#120467.
@camsim99
Copy link
Contributor Author

camsim99 commented Feb 1, 2024

Looking into how we may be able to replicate auto/locked Flutter-defined focus modes and found some helpful discussion sthat I likely will need to implement this: https://groups.google.com/a/android.com/g/camerax-developers/c/VRsER_nHeHM?pli=1, https://groups.google.com/a/android.com/g/camerax-developers/c/h0Q4Al8TXmc?pli=1

@camsim99
Copy link
Contributor Author

Working on implementing setFocusMode and it's not clear to me how to implement locked versus auto mode, so I want to write out my thoughts as of now so I can correct my assumptions as needed with this as a baseline.

Locked mode:
To implement locked mode in camera_android, we did something like

// When locking the auto-focus the camera device should do a one-time focus and afterwards
// set the auto-focus to idle. This is accomplished by setting the CONTROL_AF_MODE to
// CONTROL_AF_MODE_AUTO.
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
captureRequestBuilder.set(
  CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); 
captureSession.capture(captureRequestBuilder.build(), null, backgroundHandler);
captureRequestBuilder.set(
  CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
captureSession.setRepeatingRequest(
  captureRequestBuilder.build(), null, backgroundHandler);

It's my understanding that this is what is going on under the hood in CameraX when we set an AF MeteringPoint via startFocusAndMetering.

Auto mode:
To implement auto mode in camera_android, we did something like

captureRequestBuilder.set(
    CaptureRequest.CONTROL_AF_MODE,
    recordingVideo
        ? CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO
        : CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Cancel existing AF state.
captureRequestBuilder.set(
    CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
captureSession.capture(captureRequestBuilder.build(), null, backgroundHandler);
// Set AF state to idle again.
captureRequestBuilder.set(
    CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
captureSession.capture(captureRequestBuilder.build(), null, backgroundHandler);

To implement this with CameraX, I believe we will need to remove any AF MeteringPoints from any running FocusMeteringActions set via startFocusandMetering or, if we have only set AF MeteringPoints, cancel focus and metering altogether. This is because the continuous auto focus mode is CameraX's default mode.

@camsim99
Copy link
Contributor Author

It turns out that CameraX always uses auto focus mode, preferring CaptureRequest.CONTROL_AF_MODE for CaptureRequest. CONTROL_AF_MODE_AUTO, but may fallback to CONTROL_AF_MODE_CONTINUOUS_VIDEO/CONTROL_AF_MODE_CONTINUOUS_PICTURE based on the source code, and never sets CaptureRequest.CONTROL_AF_TRIGGER to CameraMetadata.CONTROL_AF_TRIGGER_IDLE.

To work around this, I'm going to (1) try implementing locked focus mode with Camera2 interop, and depending on the results of that and digging deeper into the philosophy of CameraX only using autofocus mode, (2) consider the pros/cons of not implementing locked focus mode.

@camsim99
Copy link
Contributor Author

camsim99 commented Feb 20, 2024

I just tried using Camera2CameraControl to manually lock the focus mode, with two attempts:

Attempt 1 (fail):
I added the capture request options CameraMetadata.CONTROL_AF_TRIGGER_IDLE for CaptureRequest.CONTROL_AF_TRIGGER with Camera2CameraControl.addCaptureRequestOptions, and I still saw autofocus.

Attempt 2 (success?):
I set the capture request options CaptureRequest.CONTROL_AF_MODE_AUTO for CaptureRequest.CONTROL_AF_MODE and CameraMetadata.CONTROL_AF_TRIGGER_IDLE for CaptureRequest.CONTROL_AF_TRIGGER with Camera2CameraControl.setCaptureRequestOptions, and it actually seemed to work, though I wonder how this may conflict with the default behavior.

auto-submit bot pushed a commit to flutter/packages that referenced this issue Feb 21, 2024
…eOffset` (#6059)

This PR implements `setFocusPoint`, `setExposurePoint`, `setExposureOffset` and makes some small fixes here and there, each of which I have left a comment about for context.

Part of flutter/flutter#120468 & flutter/flutter#120467.

~NOTE: Should land after #6068 done :)
auto-submit bot pushed a commit to flutter/packages that referenced this issue Mar 19, 2024
Implements `setFocusMode` (also adds support for disabling auto-cancel to `FocusMeteringAction` host API implementation to accomplish this) + some minor documentation improvements based on discoveries I made while working on this :)

Fixes flutter/flutter#120467.

~To be landed after: #6110 Done :)
@camsim99 camsim99 moved this from In progress to Done in Android Camera Re-write Mar 19, 2024
Copy link

github-actions bot commented Apr 2, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter p: camera The camera plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team
Development

Successfully merging a pull request may close this issue.

4 participants