iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法
更新時間:2017年11月23日 09:01:38 作者:郝嘉律
本篇文章主要介紹了iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹了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]; // 添加會話輸入和輸出 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); // 根據(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
注意點:
- 首先引入AVFoundation框架和ImageIO/ImageIO.h聲明文件
- 遵循AVCaptureVideoDataOutputSampleBufferDelegate協(xié)議
- AVCaptureSession對象要定義為屬性,確保有對象在一直引用AVCaptureSession對象;否則如果在lightSensitive方法中定義并初始化AVCaptureSession對象,會造成AVCaptureSession對象提前釋放, [self.session startRunning];會失效
- 實現(xiàn)AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,參數(shù)brightnessValue就是周圍環(huán)境的亮度參數(shù)了,范圍大概在-5~~12之間,參數(shù)數(shù)值越大,環(huán)境越亮
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
UIPageViewController實現(xiàn)的左右滑動界面
這篇文章主要為大家詳細介紹了UIPageViewController實現(xiàn)的左右滑動界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06解析iOS應(yīng)用開發(fā)中對設(shè)計模式中的抽象工廠模式的實現(xiàn)
這篇文章主要介紹了解析iOS應(yīng)用開發(fā)中對設(shè)計模式中的抽象工廠模式的實現(xiàn),示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS 11 UINavigationItem 去除左右間隙的方法
本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10