iOS 传感器之光感监测----微信扫一扫具有检测光感功能(二)

2017-04-22  本文已影响129人  Jany_4a9a

上一篇说到了光感监测,由于摄像头没有光感监测,也没有感光孔,所以只能是同检测拍到的图片亮度来达到识别是否需要照明,下面看一下实现过程:

#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);

}

得到这个值就可以

上一篇下一篇

猜你喜欢

热点阅读