如何从AVFoundation获得光照值? [英] How to get light value from AVFoundation?
问题描述
我使用Swift 3并使用相机AVFoundation
I work with Swift 3 and I use camera AVFoundation
谁知道有没有办法知道光的容量?
Who know is there any way to know capacity of light?
我知道其中一种方法是使用环境光传感器,但它不鼓励,最终应用程序不允许在市场上
I know that one of the approach is use ambient light sensor, but it is don't encourage and eventually apps doesn't allows in market
我发现问题非常接近我需要的问题
I found question very close to that I need
detecting if iPhone is in a dark room
那个人解释说我可以使用 ImageIO框架,阅读视频源每帧所带来的元数据
And that guy explains that I can use 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];
}
但我是iOS的新手,不知道怎么转换这个Swift中的代码
But I am a rookie in iOS and don't know how to convert this code in Swift
提前致谢!
推荐答案
以下代码实现在Swift 3.x中
Following code implementation is in Swift 3.x
可以使用相机的EXIF数据获得近似光度值(以单位勒克斯为单位)。请参考以下链接。 将相机用作勒克斯表
It is possible to get an approximate luminosity value(measured in unit lux) using the EXIF data of the camera. Please refer to the following link. Using a camera as a lux meter
这里AVFoundation中的 sampleBuffer
值 captureOutput
方法用于从相机帧中提取EXIF数据。
Here the sampleBuffer
value of captureOutput
method in AVFoundation is used to extract the EXIF data from camera frames.
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
可以根据申请进行校准,如参考文献中所述。
Please note that the value of CalibrationConstant
can be calibrated according the application as explained in the reference.
这篇关于如何从AVFoundation获得光照值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!