上一篇说到了光感监测,由于摄像头没有光感监测,也没有感光孔,所以只能是同检测拍到的图片亮度来达到识别是否需要照明,下面看一下实现过程:
#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);
}
得到这个值就可以
网友评论