Question

I am writing an app that automatically turns on the torch on the back of an iOS device depending on lighting conditions. The app renders a live camera view, and does not record the video.

I have tried using AVCaptureTorchModeAuto, but it only seems to measure the brightness of the image at the start of the capture session, and set the torch accordingly. The setting then does not change afterwards, regardless of the brightness of the camera image.

It is possible to have the system adjust the torch continuously, like stated in the documentation?

The capture device continuously monitors light levels and uses the torch when necessary.

Available in iOS 4.0 and later.

Was it helpful?

Solution

With current set of APIs, the brightness sampling is only done at the start of the recording. So AVCaptureTorchModeAuto does not work as you expect.


Now about the use case discussed in the question:

Possibility 1: Use Rear camera for brightness detection:

Once torch comes up, analysis of the captured stream will not tell the current situation of room brightness, as Torch will cause cosmetic brightness.

So in order to get the real brightness value would have to require to switch on-off on timely basis, which is not very generic and convenient in most of the cases.

Getting brightness value to decide enabling of Torch or not.

- (void)captureOutput:(AVCaptureOutput *)captureOutput
      didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
             fromConnection:(AVCaptureConnection *)connection
{
  CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, 
    sampleBuffer, kCMAttachmentMode_ShouldPropagate);
  NSDictionary *metadata = [[NSMutableDictionary alloc] 
    initWithDictionary:(__bridge NSDictionary*)metadataDict];
  CFRelease(metadataDict);
  NSDictionary *exifMetadata = [[metadata 
    objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
  float brightnessValue = [[exifMetadata 
    objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
}

Possibility 2: Use Front Camera for brightness detection:

Torch is not permitted with front camera.

OTHER TIPS

You can refer to this question for different ways of turning on the flash. I believe that AVCaptureTorchModeAuto works only while the video is being captured. However, you can can try something different:

1) capture a still image every second using NSTimer (if you find out a way to do it without a shutter sound)

2) convert that image to HSB and get its average brightness by getting B component of every pixel and calculating an average

3) calculate the required brightness level and set it by calling setTorchModeOnWithLevel:error: method.

To try and answer my own question:

It makes sense for the system to only set the torch mode once, at the beginning of the video capture. Constantly monitoring the brightness level would lead to the torch switching on and off endlessly, since lighting conditions would always be perfect with the torch on and too dark with the torch off. The system has no idea of knowing how bright the picture would be without the torch, since the measurement and action (turning on the torch) influence each other.

Hence, this seems to be perfectly fine behaviour and my original question was wrongly phrased.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top