iOS利用摄像头获取环境光感参数
来源:互联网 发布:深圳软件开发外包 编辑:程序博客网 时间:2024/06/05 23:51
不多说,代码如下:
#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之间,参数数值越大,环境越亮
阅读全文
0 0
- iOS利用摄像头获取环境光感参数
- iOS 获取摄像头视频
- 利用Video4Linux获取摄像头数据
- 利用Video4Linux获取摄像头数据
- iOS直接获取摄像头数据
- iOS直接获取摄像头数据
- iOS直接获取摄像头数据
- iOS获取摄像头是否允许
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 如何利用Video4Linux获取摄像头数据
- 利用Javacv获取摄像头,并且保存照片
- 摄像头获取图像和相关参数
- 《OpenCV设置和获取摄像头参数》
- Xcode报Invalid bitcode signature错误的解决方案
- 函数的上下文
- GreenDao的简单使用
- 简单图书管理系统
- 双击 表格 td 变成 可以编辑的input框 失去焦点提交
- iOS利用摄像头获取环境光感参数
- Unity3d之表情动画--眨眼
- Ubuntu终端常用的快捷键
- easyUI整合富文本编辑器KindEditor详细教程
- React使用心得
- 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例
- 正排索引(forward index)与倒排索引(inverted index)
- 淘宝卖家的潜在风险
- Leecode179Largest Number