iOS利用摄像头获取环境光感参数的方法
程序员文章站
2023-12-20 10:52:04
本文介绍了ios利用摄像头获取环境光感参数的方法,分享给大家,具体如下:
不多说,代码如下:
#import "lightsensitiveviewcont...
本文介绍了ios利用摄像头获取环境光感参数的方法,分享给大家,具体如下:
不多说,代码如下:
#import "lightsensitiveviewcontroller.h" @import avfoundation; #import <imageio/imageio.h> @interface lightsensitiveviewcontroller ()< avcapturevideodataoutputsamplebufferdelegate> @property (nonatomic, strong) avcapturesession *session; @end @implementation lightsensitiveviewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. self.view.backgroundcolor = [uicolor whitecolor]; self.navigationitem.title = @"光感"; [self lightsensitive]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } #pragma mark- 光感 - (void)lightsensitive { // 1.获取硬件设备 avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; // 2.创建输入流 avcapturedeviceinput *input = [[avcapturedeviceinput alloc]initwithdevice:device error:nil]; // 3.创建设备输出流 avcapturevideodataoutput *output = [[avcapturevideodataoutput alloc] init]; [output setsamplebufferdelegate:self queue:dispatch_get_main_queue()]; // avcapturesession属性 self.session = [[avcapturesession alloc]init]; // 设置为高质量采集率 [self.session setsessionpreset:avcapturesessionpresethigh]; // 添加会话输入和输出 if ([self.session canaddinput:input]) { [self.session addinput:input]; } if ([self.session canaddoutput:output]) { [self.session addoutput:output]; } // 9.启动会话 [self.session startrunning]; } #pragma mark- avcapturevideodataoutputsamplebufferdelegate的方法 - (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); // 根据brightnessvalue的值来打开和关闭闪光灯 avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; bool result = [device hastorch];// 判断设备是否有闪光灯 if ((brightnessvalue < 0) && result) {// 打开闪光灯 [device lockforconfiguration:nil]; [device settorchmode: avcapturetorchmodeon];//开 [device unlockforconfiguration]; }else if((brightnessvalue > 0) && result) {// 关闭闪光灯 [device lockforconfiguration:nil]; [device settorchmode: avcapturetorchmodeoff];//关 [device unlockforconfiguration]; } } @end
注意点:
- 首先引入avfoundation框架和imageio/imageio.h声明文件
- 遵循avcapturevideodataoutputsamplebufferdelegate协议
- avcapturesession对象要定义为属性,确保有对象在一直引用avcapturesession对象;否则如果在lightsensitive方法中定义并初始化avcapturesession对象,会造成avcapturesession对象提前释放, [self.session startrunning];会失效
- 实现avcapturevideodataoutputsamplebufferdelegate的代理方法,参数brightnessvalue就是周围环境的亮度参数了,范围大概在-5~~12之间,参数数值越大,环境越亮
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。