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