我们在使用摄像头扫描或者其他功能时,有时候需要根据环境自动打开摄像头来补光,这样使用起来更加人性化。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);
}
(完