Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Added support for flash/torch mode in iOS and Android #2837

Closed
wants to merge 9 commits into from
Closed

[camera] Added support for flash/torch mode in iOS and Android #2837

wants to merge 9 commits into from

Conversation

acoutts
Copy link
Contributor

@acoutts acoutts commented Jun 18, 2020

Description

This PR implements the ability to toggle torch mode on/off in iOS and Android. You can use this to run the camera's flash continuously as a torch and can use it as a flash for photo capture (by turning it on/off before/after your capture), and during video capture as a continuous light.

It adds the following new methods to the CameraController:

  • Future<bool> hasTorch()
  • Future<void> disableTorch()
  • Future<void> enableTorch()

It has been tested on an iPhone 8 running iOS 13 and a Pixel 4 running Android 29. It works during video and photo capture as well. The example app has been updated with a new toggle switch for enabling/disabling the torch. The toggle switch will be disabled (20% opacity and non-interactive) if the current selected camera does not support torch mode.

Related Issues

Closes flutter/flutter#66252
flutter/flutter#31225
flutter/flutter#58218
flutter/flutter#57307
flutter/flutter#54548
flutter/flutter#41785

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@orestesgaolin
Copy link

Unfortunately on Android it doesn't work properly because you don't handle the precapture measurements. The photos on Samsung S8 are oversaturated.
2020-06-18 18 33 20

@acoutts
Copy link
Contributor Author

acoutts commented Jun 18, 2020

Unfortunately on Android it doesn't work properly because you don't handle the precapture measurements. The photos on Samsung S8 are oversaturated.

Just to clarify, all this PR does is enable torch mode which you can use as a video flash and is somewhat functional for a photo flash but as you point out it's not the same as firing the flash at the time of the photo.

I didn't have any luck setting this:

captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON_ALWAYS_FLASH);

I hope someone else can implement full-featured flash eventually, but for now this is the best we can do.

edit
After looking at https://github.com/gummie4444/flutter-better-camera where they did do the proper flash implementation, that is definitely way outside of the scope of what I wanted to accomplish here.
I am simply adding torch mode and not intending it to be a replacement for the full flash routine for photos.

@orestesgaolin
Copy link

After looking at https://github.com/gummie4444/flutter-better-camera where they did do the proper flash implementation, that is definitely way outside of the scope of what I wanted to accomplish here.

Yeah, and I tried to improve on that but frankly there are some edge cases like some LG or Huawei phones that still work differently than what you'd expect

@acoutts
Copy link
Contributor Author

acoutts commented Jun 18, 2020

After looking at https://github.com/gummie4444/flutter-better-camera where they did do the proper flash implementation, that is definitely way outside of the scope of what I wanted to accomplish here.

Yeah, and I tried to improve on that but frankly there are some edge cases like some LG or Huawei phones that still work differently than what you'd expect

I think all we can do is follow the Android API docs and implement it to that spec, and if any phone manufacturers don't implement the API correctly then there isn't much we can do about it.

@ghunkins
Copy link

@acoutts Huge thanks for putting this out. Really looking forward to seeing this in action, there's a lot of interest in the Flutter community for shipping this feature.

@acoutts
Copy link
Contributor Author

acoutts commented Jun 24, 2020

@bparrishMines is there anything I can do to help speed this along for merging?

@malikdoksoz
Copy link

malikdoksoz commented Jun 24, 2020

@bparrishMines is there anything I can do to help speed this along for merging?

İf you want check this library for focus and many flash attributes (you can find ios part on the main section):
https://github.com/react-native-community/react-native-camera/tree/master/android/src/main/java/com/google/android/cameraview

@acoutts
Copy link
Contributor Author

acoutts commented Jun 28, 2020

@bparrishMines is there anything I can do to help speed this along for merging?

İf you want check this library for focus and many flash attributes (you can find ios part on the main section):
https://github.com/react-native-community/react-native-camera/tree/master/android/src/main/java/com/google/android/cameraview

For the scope of this PR I do not want to do anything else except torch mode like I've done here.

@acoutts
Copy link
Contributor Author

acoutts commented Jul 2, 2020

@ghunkins do you know anyone I can tag here to get this reviewed / merged? Seems like it is not maintained so far..

@ghunkins
Copy link

ghunkins commented Jul 7, 2020

Tagging reviewers on #1969 here to hopefully bring this across the finish line. Can anyone please add themselves to help review and merge this?

@orestesgaolin @JoseGeorges8 @GustavoFigueira @victoralves @cyanglaz @mklim @mehmetf @bparrishMines . Thanks in advance! cc: @acoutts @NeKoFu

@orestesgaolin
Copy link

As a user of the plugin I wouldn't like to see this feature implemented without proper handling of precapture measurements. It wasn't tested on variety of devices and I know that there are slight differences across manufacturers that will cause the output images to look incorrectly.

Merging this to the plugin repo would mean that now the Flutter team would have to handle all the issues related to this implementation and I guess they won't be investing into that in the nearest future. Sorry to say that, but I suspect that this PR won't land in the repo as there was no interaction from the team at all.

@acoutts
Copy link
Contributor Author

acoutts commented Jul 7, 2020

As a user of the plugin I wouldn't like to see this feature implemented without proper handling of precapture measurements. It wasn't tested on variety of devices and I know that there are slight differences across manufacturers that will cause the output images to look incorrectly.

Merging this to the plugin repo would mean that now the Flutter team would have to handle all the issues related to this implementation and I guess they won't be investing into that in the nearest future. Sorry to say that, but I suspect that this PR won't land in the repo as there was no interaction from the team at all.

Just to reiterate, torch mode != flash mode. While you can indeed get by using the torch as a flash by simply turning it on/off before capturing an image, torch mode is simply the flash running constantly. The primary use case is when you need the flash turned on for the camera preview, such as for scanning QR codes, or really if you just want to build a simple flutter flashlight app.

I do hope someone can add the proper flash mode in the future for image capture, but I simply want to get torch mode merged here. Let's not hold it up for something outside of the scope of the original PR.

@acoutts
Copy link
Contributor Author

acoutts commented Jul 14, 2020

Hello is anyone out there? What can I do to get this merged sooner?

@acoutts
Copy link
Contributor Author

acoutts commented Jul 14, 2020

cc @TahaTesser do you know anyone that can help take a look at this? Seems like there's been no activity on this plugin for the last month..

@malikdoksoz
Copy link

cc @TahaTesser do you know anyone that can help take a look at this? Seems like there's been no activity on this plugin for the last month..

The Flutter team is making efforts to avoid this feature :)

@jovinho
Copy link

jovinho commented Jul 17, 2020

@acoutts Thanks for the contribution, is there any example code of what you did? I'd like to use the code in your branch until it gets merged.

@acoutts
Copy link
Contributor Author

acoutts commented Jul 17, 2020

@acoutts Thanks for the contribution, is there any example code of what you did? I'd like to use the code in your branch until it gets merged.

You can see the files changed tab here and it will show the diff of this PR:
https://github.com/flutter/plugins/pull/2837/files

As for how to use it, that is as simple as calling await controller.enableTorch(); on the camera controller now.

@bparrishMines
Copy link
Contributor

cc @cyanglaz

Hi @acoutts

Thanks for the contribution! Unfortunately, this feature is non-trivial and we don't have anyone focused on adding new features to this plugin at the moment. We typically follow issue hygiene when prioritizing issues and PRs. We'll keep this on our radar, but I can't give a time frame when we'll be able to work on it.

Everyone is also free to use this PR by adding a git dependency to their pubspec.yaml.

@felipesses
Copy link

Hello, could you help me on how do I use this PR on my project? I don't know how to import it in my pubspec.yaml file, I have used by copying your code and it's working really well, thanks for that PR by the way, you saved my life :D hope the Flutter community accept your this PR soon.

@lukepighetti
Copy link

Thanks for the contribution! Unfortunately, this feature is non-trivial and we don't have anyone focused on adding new features to this plugin at the moment.

I'm a little confused. Is camera is no longer supported?

@AhmedNourJamalElDin
Copy link

hmmmm

@TahaTesser
Copy link
Member

TahaTesser commented Sep 21, 2020

Hi @acoutts
I edited your OP to add flutter/flutter#66252
Let me know if you disagree
Thank you

@nombrekeff
Copy link

I appreciate the effort done to get this done, but I think a camera plugin without the option to use the flash/lamp is kinda weird. I think this is a critical feature that the plugin should have...

Is the PR code stable enough for use in a production APP or should we wait until it's merged?

@acoutts
Copy link
Contributor Author

acoutts commented Sep 21, 2020

Hi @acoutts
I edited your OP to add flutter/flutter#66252
Let me know if you disagree
Thank you

Thanks!

@acoutts
Copy link
Contributor Author

acoutts commented Sep 21, 2020

I appreciate the effort done to get this done, but I think a camera plugin without the option to use the flash/lamp is kinda weird. I think this is a critical feature that the plugin should have...

Is the PR code stable enough for use in a production APP or should we wait until it's merged?

It's been stable enough for me to use without issue so far since I created this PR months ago.

@nombrekeff
Copy link

I appreciate the effort done to get this done, but I think a camera plugin without the option to use the flash/lamp is kinda weird. I think this is a critical feature that the plugin should have...
Is the PR code stable enough for use in a production APP or should we wait until it's merged?

It's been stable enough for me to use without issue so far since I created this PR months ago.

Okay, thanks! I will give it a go and if no issue arises I will make use of it. In case I find any weird behavior or bug I will let you know.

I will be keeping an eye on this, let me know if I can do anything to get this moving quicker.

@acoutts
Copy link
Contributor Author

acoutts commented Sep 21, 2020

I appreciate the effort done to get this done, but I think a camera plugin without the option to use the flash/lamp is kinda weird. I think this is a critical feature that the plugin should have...
Is the PR code stable enough for use in a production APP or should we wait until it's merged?

It's been stable enough for me to use without issue so far since I created this PR months ago.

Okay, thanks! I will give it a go and if no issue arises I will make use of it. In case I find any weird behavior or bug I will let you know.

I will be keeping an eye on this, let me know if I can do anything to get this moving quicker.

Unfortunately this plugin is not being updated for new features, so I would suggest looking to this community one for something more actively maintained: https://pub.dev/packages/camerawesome

@nombrekeff
Copy link

Nice thanks a lot! I will check that out. It's a shame it's not supported in the official plugin though 😐

@Rossdex
Copy link

Rossdex commented Nov 20, 2020

I have tested this on about 7 devices. There seems to be some oddities upon taking a photo, where the torch on the device flashes while taking a photo, causing the image to be under exposed.

This happens on every device I have tested so far but only seems to affect some (e.g. Samsung A71 running Android 10).

The best test of this is if you are in a completely dark environment you can see the preview is fully lit, however the photo that comes out of the 'takePicture' function comes out completely black.

@mvanbeusekom
Copy link
Contributor

Closing this PR in favor of #3314

@szuwest
Copy link

szuwest commented Jan 14, 2021

Description

This PR implements the ability to toggle torch mode on/off in iOS and Android. You can use this to run the camera's flash continuously as a torch and can use it as a flash for photo capture (by turning it on/off before/after your capture), and during video capture as a continuous light.

It adds the following new methods to the CameraController:

  • Future<bool> hasTorch()
  • Future<void> disableTorch()
  • Future<void> enableTorch()

It has been tested on an iPhone 8 running iOS 13 and a Pixel 4 running Android 29. It works during video and photo capture as well. The example app has been updated with a new toggle switch for enabling/disabling the torch. The toggle switch will be disabled (20% opacity and non-interactive) if the current selected camera does not support torch mode.

Related Issues

Closes flutter/flutter#66252
flutter/flutter#31225
flutter/flutter#58218
flutter/flutter#57307
flutter/flutter#54548
flutter/flutter#41785

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

how to use your branch in a flutter project?

@mvanbeusekom
Copy link
Contributor

@szuwest flash and torch features on Android and iOS is available since version 0.6.3+2 of the camera plugin and is available on pub.dev. I recommend updating to the latest version 0.6.4+5.

@szuwest
Copy link

szuwest commented Jan 15, 2021

@szuwest flash and torch features on Android and iOS is available since version 0.6.3+2 of the camera plugin and is available on pub.dev. I recommend updating to the latest version 0.6.4+5.

Thank you, I found it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Camera] Added support for flash/torch mode in iOS and Android