2017-01-29 34 views
1

我与斯威夫特3一起工作,我使用相机AVFoundation如何从AVFoundation获取光照值?

谁知道有什么办法来知道光的能力?

我知道的方法之一是利用环境光传感器,但它是不鼓励,最终Apps不会允许市场

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

detecting if iPhone is in a dark room

这家伙解释说,我可以用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新秀并不知道如何转换此代码在斯威夫特

在此先感谢!

回答

0

按照以下步骤从捕获的图像中获取EXIF数据。

  • 从样本缓冲区获取图像数据。

让的imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)

  • 使用下面的辅助函数来得到你需要的EXIF数据
func getEXIFFromImage(image:NSData) -> NSDictionary { 
    let imageSourceRef = CGImageSourceCreateWithData(image, nil); 
    let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil) 
    let mutableDict = NSMutableDictionary(dictionary: currentProperties!) 
    return mutableDict 
} 
+0

有一些问题:AVCaptureStillImageOutput已被弃用,另一方面,这个方法'AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)'返回'数据',但这种方法'getEXIFFromImage(image:NSData)'需要获得'NSData' ...什么你认为? –

+0

无论如何还有一个问题......如果我尝试通过键'kCGImagePropertyExifBrightnessValue'获取值,我会得到'nil' ...你知道为什么吗? –

3

下面的代码实现在斯威夫特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的值可根据参考文献中所述的应用进行校准。

+0

环境中光线少,光线充足,光线数值偏低,是否正常?谢谢 – Cristina

+0

它不是。亮度是在给定区域内光通量分布的度量。所以它应该与表面上的照度成正比,取决于环境光量。 (https://en.wikipedia.org/wiki/Lux) – anuh91