我使用 Swift 3并使用相机AVFoundation 谁知道有没有办法知道光的容量? 我知道其中一种方法是使用环境光传感器,但它不鼓励,最终应用程序不允许在市场上 我发现问题非常接近我需要的问
谁知道有没有办法知道光的容量?
我知道其中一种方法是使用环境光传感器,但它不鼓励,最终应用程序不允许在市场上
我发现问题非常接近我需要的问题
detecting if iPhone is in a dark room
那个人解释说我可以使用ImageIO框架,读取视频源的每一帧进来的元数据
- (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]; }
但我是iOS的新手,不知道如何在Swift中转换这段代码
提前致谢!
以下代码实现在Swift 3.x中可以使用相机的EXIF数据获得近似光度值(以单位勒克斯为单位).请参考以下链接. Using a camera as a lux meter
这里AVFoundation中captureOutput方法的sampleBuffer值用于从相机帧中提取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)}
请注意,CalibrationConstant的值可以根据应用进行校准,如参考文献中所述.