iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法
本文介紹了iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法,分享給大家,具體如下:
不多說,代碼如下:
#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.獲取硬件設(shè)備 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 2.創(chuàng)建輸入流 AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil]; // 3.創(chuàng)建設(shè)備輸出流 AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; // AVCaptureSession屬性 self.session = [[AVCaptureSession alloc]init]; // 設(shè)置為高質(zhì)量采集率 [self.session setSessionPreset:AVCaptureSessionPresetHigh]; // 添加會(huì)話輸入和輸出 if ([self.session canAddInput:input]) { [self.session addInput:input]; } if ([self.session canAddOutput:output]) { [self.session addOutput:output]; } // 9.啟動(dòng)會(huì)話 [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); // 根據(jù)brightnessValue的值來打開和關(guān)閉閃光燈 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; BOOL result = [device hasTorch];// 判斷設(shè)備是否有閃光燈 if ((brightnessValue < 0) && result) {// 打開閃光燈 [device lockForConfiguration:nil]; [device setTorchMode: AVCaptureTorchModeOn];//開 [device unlockForConfiguration]; }else if((brightnessValue > 0) && result) {// 關(guān)閉閃光燈 [device lockForConfiguration:nil]; [device setTorchMode: AVCaptureTorchModeOff];//關(guān) [device unlockForConfiguration]; } } @end
注意點(diǎn):
- 首先引入AVFoundation框架和ImageIO/ImageIO.h聲明文件
- 遵循AVCaptureVideoDataOutputSampleBufferDelegate協(xié)議
- AVCaptureSession對(duì)象要定義為屬性,確保有對(duì)象在一直引用AVCaptureSession對(duì)象;否則如果在lightSensitive方法中定義并初始化AVCaptureSession對(duì)象,會(huì)造成AVCaptureSession對(duì)象提前釋放, [self.session startRunning];會(huì)失效
- 實(shí)現(xiàn)AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,參數(shù)brightnessValue就是周圍環(huán)境的亮度參數(shù)了,范圍大概在-5~~12之間,參數(shù)數(shù)值越大,環(huán)境越亮
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
UIPageViewController實(shí)現(xiàn)的左右滑動(dòng)界面
這篇文章主要為大家詳細(xì)介紹了UIPageViewController實(shí)現(xiàn)的左右滑動(dòng)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06iOS之?dāng)?shù)據(jù)解析之XML解析詳解
本篇文章主要介紹了iOS之?dāng)?shù)據(jù)解析之XML解析詳解,XML解析常見的兩種方式:DOM解析和SAX解析,有興趣的可以了解一下。2016-12-12僅幾行iOS代碼限制TextField輸入長(zhǎng)度
這篇文章主要為大家詳細(xì)介紹了通過幾行iOS代碼限制TextField輸入長(zhǎng)度的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09解析iOS應(yīng)用開發(fā)中對(duì)設(shè)計(jì)模式中的抽象工廠模式的實(shí)現(xiàn)
這篇文章主要介紹了解析iOS應(yīng)用開發(fā)中對(duì)設(shè)計(jì)模式中的抽象工廠模式的實(shí)現(xiàn),示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS 11 UINavigationItem 去除左右間隙的方法
本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07