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

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

作者: Jany_4a9a | 来源:发表于2017-04-22 15:02 被阅读129次

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

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

}

得到这个值就可以

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/tseszttx.html