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

[camera] Add support for Torch and Auto Exposure (#19845) #1969

Closed
wants to merge 47 commits into from
Closed

[camera] Add support for Torch and Auto Exposure (#19845) #1969

wants to merge 47 commits into from

Conversation

NeKoFu
Copy link

@NeKoFu NeKoFu commented Aug 11, 2019

Description

  • Add new callable methods torchOn and torchOff
  • Add new callable methods aeOn and aeOff
  • Torch and AE mode could be activated at initialize

Related Issues

Camera flash functionality during image and/or video capture #19845
Future of the Camera Plugin (Refactor/Rework) #31225

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.

@NeKoFu
Copy link
Author

NeKoFu commented Aug 11, 2019

To reviewers, sorry for the quantity of commits to review. I don't have a macbook to validate the iOS part and flutter_plugin_tools doesn't works on my windows. I used the CI to fix iOS code and code format. This feature works pretty well on android so I hope it will works also without any issues on iOS.

@mklim mklim self-assigned this Aug 12, 2019
Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! I didn't thoroughly review the whole PR, but I gave this an initial pass with some first feedback. Thanks for the heads up about the iOS part needing additional testing and for doing as much as you could.

I'm following the initial PR review policy.I'm labeling this with "backlog" for now because this PR isn't trivial to review. We'll prioritize reviewing it according to the issue's priority.

Relevant issues:
flutter/flutter#19845

@NeKoFu
Copy link
Author

NeKoFu commented Oct 8, 2019

@mikerockett, thanks for your feedbacks. The flash turns on momentarily because the auto exposure mode is set to CONTROL_AE_MODE_ON_ALWAYS_FLASH. With this mode, auto exposure controls the flash and use it once immediately to adjust colors and light. Without this AE setting, I have no flash at all when I take a picture. It simply doesn't works on my phone without this AE mode and I have tested a lot of combinations. 😞

I also saw one mistake with the continuous autofocus mode so I have fixed it.

For now, I finally have an old macbook so I can work on the iOS version more easily. 😋

@malikdoksoz
Copy link

@mikerockett, thanks for your feedbacks. The flash turns on momentarily because the auto exposure mode is set to CONTROL_AE_MODE_ON_ALWAYS_FLASH. With this mode, auto exposure controls the flash and use it once immediately to adjust colors and light. Without this AE setting, I have no flash at all when I take a picture. It simply doesn't works on my phone without this AE mode and I have tested a lot of combinations. 😞

I also saw one mistake with the continuous autofocus mode so I have fixed it.

For now, I finally have an old macbook so I can work on the iOS version more easily. 😋

I tried iPhone 6-7-8( and plus) with iOS 12 and 13. It's working no problem :) I'm using this repo https://gitlab.com/mikerockett/flutter-camera . Can you give me recently updated repo (git link)

…ter-master

# Conflicts:
#	packages/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java
#	packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java
# Conflicts:
#	packages/camera/CHANGELOG.md
#	packages/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java
#	packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java
#	packages/camera/pubspec.yaml
@mikerockett
Copy link

@NeKoFu – You’re most welcome. I do understand it can be quite difficult to get the right combination; I battled with this myself, which was extraordinarily frustrating given that I have very limited knowledge of how it all works. Thanks for working so hard on this.

@malikdoksoz – my repo is out of date, given the changes made here. I'm not going to be updating the repo at this point, as there is a commercial app in circulation that relies on it, and the flash behaviour used (always on / always off) is perfect for our needs at the moment.

@jovinho
Copy link

jovinho commented Nov 12, 2019

Any idea when this feature is gonna be merged into master?

Copy link

@JoseGeorges8 JoseGeorges8 left a comment

Choose a reason for hiding this comment

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

I've forked the camera plugin and applied your PR changes and tested them out. The android code seems to be working fine, however your iOS implementation is broken.

I don't have much experience with Objective C code but I got it to work by doing some changes.

Essentially on the handleMethodCallAsync you're checking for the invoking methods "flashOn" and "flashOff" but the method you really added was "setFlash", so there's really no bridge to access those methods from the dart code.

I've changed the code and got it running with a couple file changes.

Notice the changes I've done are molded towards my team needs:

in the Camera.java file:

  public  void turnFlashOn(@NonNull final Result result){
    setFlash(result, 3);
  }

  public  void turnFlashOff(@NonNull final Result result){
    setFlash(result, 0);

  }

in the MethodCallHandlerImpl.java:

      case "turnFlashOn":
      {	      
        camera.turnFlashOn(result);
        break;
      }
      case "turnFlashOff":
      {
        camera.turnFlashOff(result);
        break;	       
      }	      

in the CameraPlugin.m:

} else if ([@"turnFlashOn" isEqualToString:call.method]) {
  [_camera setFlashMode:true];
  result(nil);
} else if ([@"turnFlashOff" isEqualToString:call.method]) {
   [_camera setFlashMode:false];
   result(nil);
}

lastly in the camera.dart :

/// Turn flash light off
  /// See [FlashMode] enum for available options
  Future<void> turnFlashOff() async {
    if (!value.isInitialized || _isDisposed) {
      throw CameraException(
        'Uninitialized CameraController.',
        'flashMode was called on uninitialized CameraController',
      );
    }
    try {
      await _channel.invokeMethod<void>('turnFlashOff');
    } on PlatformException catch (e) {
      throw CameraException(e.code, e.message);
    }
  }

  /// Turn flash light on
  /// See [FlashMode] enum for available options
  Future<void> turnFlashOn() async {
    if (!value.isInitialized || _isDisposed) {
      throw CameraException(
        'Uninitialized CameraController.',
        'flashMode was called on uninitialized CameraController',
      );
    }
    try {
      await _channel.invokeMethod<void>('turnFlashOn');
    } on PlatformException catch (e) {
      throw CameraException(e.code, e.message);
    }
 }

@ghunkins
Copy link

Echoing @LeonardoDavila, this is a high priority feature for many ongoing initiatives. Can this feature be prioritized? It seems as though only small tweaks and a re-base are necessary.

@gisinator
Copy link

Cant wait for a working flash solution :)

@Ahmadre
Copy link

Ahmadre commented Jan 4, 2020

@NeKoFu any updates?

@jovinho
Copy link

jovinho commented Jan 8, 2020

Is this code working for android and ios? Can i copy it?

@NeKoFu
Copy link
Author

NeKoFu commented Jan 13, 2020

Hi everyone, I was very busy at the end of the year but I didn't forgot this package. At my company, I work now to porting our application to iOS and obviously, I need to finish the iOS part of this feature.

I come back very soon with an update.

@malikdoksoz
Copy link

malikdoksoz commented Feb 12, 2020

Can you give us time to finish? Lack of flash is a big problem. Thank you

@felipebueno
Copy link

Hi, @NeKoFu!

Do you have any updates on this PR?

It's been 2 months since your last reply. We really need the flash feature ASAP 😄

@GustavoFigueira
Copy link

Hi @NeKoFu!

Would be great to have this feature incorporated soon! My company needs tha flash function in our camera app.

Thanks in advance.

Conflicts:
	packages/camera/CHANGELOG.md
	packages/camera/lib/camera.dart
	packages/camera/pubspec.yaml
	packages/google_sign_in/lib/google_sign_in.dart
	packages/google_sign_in/pubspec.yaml
@orestesgaolin
Copy link

It seems like the title of the PR should include Auto Focus not Auto Exposure


try {
await _channel.invokeMethod<void>(
'setAutoFocus', <String, dynamic>{'mode': mode.index});

Choose a reason for hiding this comment

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

This method is called setAutoExposureMode:enableAutoExposure in iOS project

@piotrek1543
Copy link

piotrek1543 commented Apr 27, 2020

It seems like the title of the PR should include Auto Focus not Auto Exposure

The name is right according to this commit (sorry I am noob in iOS): 1d8d34a

@malikdoksoz
Copy link

@NeKoFu When will I be able to use this?

@acoutts
Copy link
Contributor

acoutts commented Jun 18, 2020

I just tried this out - it seems it is broken in iOS and a bit buggy:

  • If you toggle the flash button it will eventually turn the torch on, but toggling the button again to flash off won't disable the torch unless you tap the torch button twice.
  • When on the auto setting and the flash is turned on, it never turns off unless you again toggle the torch button twice.
  • iOS is not working. Another issue is I see you are setting the torch setting on AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; and not using the actual active camera in use.
  • Why is google sign on included in the PR?
  • The dart code calls a method named setFlash on the platform channel but this isn't implemented on iOS, iOS only has explicit cases like flashOn, flashOff which is probably why it's broken in iOS.
  • Throughout the code there are conflicting uses of the words auto exposure and auto focus, which are two very different functions of a camera.

I made a new PR before finding this one where I only implemented torch mode. It is working in iOS and Android and works with photos/video recording. I suggest anyone who needs the torch functionality now please try mine here: #2837

@mvanbeusekom
Copy link
Contributor

closing as duplicate of #3338 and #3346

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

Successfully merging this pull request may close these issues.