2017-01-29 41 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