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] Implements torch mode #4903

Merged
merged 53 commits into from
Oct 10, 2023
Merged

[camerax] Implements torch mode #4903

merged 53 commits into from
Oct 10, 2023

Conversation

camsim99
Copy link
Contributor

@camsim99 camsim99 commented Sep 11, 2023

Implements the torch flash mode. Also wraps classes necessary for the implementation (a method in Camera, CameraControl).

Fixes flutter/flutter#120715.
Fixes flutter/flutter#115846.
Part of flutter/flutter#115847.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

camsim99 added 30 commits May 1, 2023 16:46
@@ -81,7 +82,8 @@ public void setUp(
GeneratedCameraXLibrary.LiveDataHostApi.setup(binaryMessenger, liveDataHostApiImpl);
GeneratedCameraXLibrary.ObserverHostApi.setup(
binaryMessenger, new ObserverHostApiImpl(binaryMessenger, instanceManager));
imageAnalysisHostApiImpl = new ImageAnalysisHostApiImpl(binaryMessenger, instanceManager);
imageAnalysisHostApiImpl =
new ImageAnalysisHostApiImpl(binaryMessenger, instanceManager, context);
Copy link
Contributor Author

@camsim99 camsim99 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR also makes a fix to this implementation by adding a Context to the ImageAnalysisHostApiImpl constructor to remove an unnecessary call to update the context for the Host API implementations after running this method also setting the Context for those implementations (see line 131).

Copy link
Contributor Author

@camsim99 camsim99 Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camsim99 camsim99 marked this pull request as ready for review September 13, 2023 21:50
@camsim99
Copy link
Contributor Author

After revisiting https://developer.android.com/training/camerax/configuration#torch, I realize I could get the torch state as well as whether or not the device has a flash unit and use that info in this PR versus tracking the torch state myself and not throwing an error/warning when torch state is not possible. If folks have strong opinions, I can fix in this PR; otherwise, I'll file an issue and fix this after I finish feature parity.

@HostApi(dartHostTestHandler: 'TestCameraControlHostApi')
abstract class CameraControlHostApi {
@async
void enableTorch(int identifier, bool torch);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bparrishMines If the Result this returns ends up with an error, would I be able to catch this from the Dart side and throw an error there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, an exception on the Java side should cause a PlatformException error on the Dart side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to modify this PR to catch PlatformExceptions that may be cause by CameraX failing to turn on torch mode.

@camsim99 camsim99 mentioned this pull request Sep 19, 2023
11 tasks
Copy link
Member

@gmackall gmackall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, just have one question about how flash modes and torch modes interact with each other.

@camsim99
Copy link
Contributor Author

@gmackall Made the fix on your comment and added a test for it and also added error handling for failure to change torch state in the latest commit.

Copy link
Member

@gmackall gmackall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minus some requests for explanation in the documentation of setFlashMode!

@@ -491,6 +500,14 @@ class AndroidCameraCameraX extends CameraPlatform {
/// Sets the flash mode for the selected camera.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Might be worth adding to the documentation here that this method combines the notion of flash and torch from

https://developer.android.com/reference/androidx/camera/core/ImageCapture and
https://developer.android.com/reference/androidx/camera/core/CameraControl#enableTorch(boolean).

// Turn off torch mode if it is enabled and not being redundantly set.
if (mode != FlashMode.torch && torchEnabled) {
cameraControl = await camera!.getCameraControl();
await cameraControl.enableTorch(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it still might be worth adding a note to the documentation here that points out the difference between this method and the underlying CameraX apis (specifically just that torch and flash can't be set independently in this plugin, like they can with the CameraX apis).

So something like

setFlashMode(id, FlashMode.torch)
takePicture(id)
setFlashMode(id, FlashMode.auto)
takePicture(id)

would behave differently in the plugin (torch disabled, flash mode on auto) from what someone familiar with the native apis would expect (torch enabled, flash mode on auto).

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 10, 2023
@auto-submit auto-submit bot merged commit bcfa15d into flutter:main Oct 10, 2023
70 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 11, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 11, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 12, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Oct 12, 2023
flutter/packages@4b483f2...93c3f69

2023-10-12 goderbauer@google.com [shared_preferences] update file version constraints (flutter/packages#5121)
2023-10-12 supercionci@hotmail.it [cross_file] Improved documentation about ignored parameters in IO module. (flutter/packages#4416)
2023-10-11 41873024+droidbg@users.noreply.github.com [in_app_purchase] [#135759] Fix. doc reference finishPurchase to completePurchase. (flutter/packages#5081)
2023-10-10 zeucxb@gmail.com Update repo README issues link (flutter/packages#5106)
2023-10-10 43643339+nohli@users.noreply.github.com [flutter_markdown] Fix typo in readme (flutter/packages#5096)
2023-10-10 43054281+camsim99@users.noreply.github.com [camerax] Implements torch mode (flutter/packages#4903)
2023-10-10 25266387+Leptopoda@users.noreply.github.com [shared_preferences] fix documentation wording (flutter/packages#4986)
2023-10-10 katelovett@google.com [two_dimensional_scrollables] Fix paint bug when rows/columns are pinned and axes are reversed (flutter/packages#5038)
2023-10-10 engine-flutter-autoroll@skia.org Roll Flutter from f52fe4f to 83134ac (25 revisions) (flutter/packages#5104)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
HugoOlthof pushed a commit to moneybird/packages that referenced this pull request Dec 13, 2023
Implements the torch flash mode. Also wraps classes necessary for the implementation (a method in `Camera`, `CameraControl`).

Fixes flutter/flutter#120715.
Fixes flutter/flutter#115846.
Part of flutter/flutter#115847.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[camerax] Implement flash mode configuration [camera] Add Camera class to CameraX plugin
3 participants