上一篇說到了光感監(jiān)測汽抚,由于攝像頭沒有光感監(jiān)測祟印,也沒有感光孔,所以只能是同檢測拍到的圖片亮度來達(dá)到識別是否需要照明歉井,下面看一下實現(xiàn)過程:
#import
#import
@property(nonatomic,strong)AVCaptureVideoDataOutput*myVideoOutPut;
@property(nonatomic,strong)AVCaptureSession*mySession;
- (AVCaptureSession*)mySession
{
if(!_mySession) {
_mySession= [[AVCaptureSessionalloc]init];
[_mySessionsetSessionPreset:AVCaptureSessionPresetMedium];
}
return_mySession;
}
- (AVCaptureVideoDataOutput*)myVideoOutPut
{
if(!_myVideoOutPut) {
_myVideoOutPut= [[AVCaptureVideoDataOutputalloc]init];
[_myVideoOutPutsetAlwaysDiscardsLateVideoFrames:YES];
[_myVideoOutPutsetSampleBufferDelegate:(id)selfqueue:dispatch_get_main_queue()];
}
return_myVideoOutPut;
}
if([self.mySessioncanAddOutput:self.myVideoOutPut]) {
[self.mySessionaddOutput:self.myVideoOutPut];
}
[_mySessionstartRunning];
然后下面就是代理方法送挑,就會得到圖片亮度
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
CFDictionaryRefmetadataDict =CMCopyDictionaryOfAttachments(NULL,
sampleBuffer,kCMAttachmentMode_ShouldPropagate);
NSDictionary*metadata = [[NSMutableDictionaryalloc]
initWithDictionary:(__bridgeNSDictionary*)metadataDict];
CFRelease(metadataDict);
NSDictionary*exifMetadata = [[metadataobjectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
floatbrightnessValue = [[exifMetadataobjectForKey:(NSString*)kCGImagePropertyExifBrightnessValue]floatValue];
NSLog(@"%f",brightnessValue);
}
得到這個值就可以