AVCaptureTorchModeAuto does not continuously update torch mode

和自甴很熟 提交于 2019-12-05 12:36:24

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.

bzz

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!