如何从AVFoundation获得光照值?

Ale*_*nko 3 ios swift

我使用Swift 3并使用相机AVFoundation

谁知道有没有办法知道光的容量?

我知道其中一种方法是使用环境光传感器,但它不鼓励,最终应用程序不允许在市场上

我发现问题非常接近我需要的问题

检测iPhone是否在黑暗的房间里

那家伙解释说我可以用 ImageIO framework, read the metadata that's coming in with each frame of the video feed

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但我是iOS的新手,不知道如何在Swift中转换这段代码

提前致谢!

Anu*_*haH 8

以下代码实现在Swift 3.x中

可以使用相机的EXIF数据获得近似光度值(以单位勒克斯为单位).请参考以下链接.使用相机作为勒克斯计

这里AVFoundation sampleBuffercaptureOutput方法的值用于从相机帧中提取EXIF数据.

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

    //Retrieving EXIF data of camara frame buffer
    let rawMetadata = CMCopyDictionaryOfAttachments(nil, sampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))
    let metadata = CFDictionaryCreateMutableCopy(nil, 0, rawMetadata) as NSMutableDictionary
    let exifData = metadata.value(forKey: "{Exif}") as? NSMutableDictionary

    let FNumber : Double = exifData?["FNumber"] as! Double
    let ExposureTime : Double = exifData?["ExposureTime"] as! Double
    let ISOSpeedRatingsArray = exifData!["ISOSpeedRatings"] as? NSArray
    let ISOSpeedRatings : Double = ISOSpeedRatingsArray![0] as! Double
    let CalibrationConstant : Double = 50 

    //Calculating the luminosity
    let luminosity : Double = (CalibrationConstant * FNumber * FNumber ) / ( ExposureTime * ISOSpeedRatings )  

    print(luminosity)}
Run Code Online (Sandbox Code Playgroud)

请注意,CalibrationConstant可以根据申请中的说明校准值.