我們在使用攝像頭掃描或者其他功能時,有時候需要根據環(huán)境自動打開攝像頭來補光,這樣使用起來更加人性化。AVCaptureDevice其實是可以捕獲并輸出光線信息的。
見下:
在代理方法AVCaptureVideoDataOutputSampleBufferDelegate中,可以看到一些緩存數據,里面包含各種獲取的數據,其中包含光線數據。
首先
#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>
然后設置代理
//檢測光源的代理
_videoOutput = [[AVCaptureVideoDataOutput alloc]init];
[_videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
這樣最后就可以在代理方法中獲取到光線參數并做一些事情
-(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];
NSLog(@"光源%f",brightnessValue);
}
(完