您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在iOS中利用攝像頭獲取環(huán)境光感參數(shù),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
#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.創(chuàng)建輸入流 AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil]; // 3.創(chuàng)建設備輸出流 AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; // AVCaptureSession屬性 self.session = [[AVCaptureSession alloc]init]; // 設置為高質(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的值來打開和關閉閃光燈 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協(xié)議
AVCaptureSession對象要定義為屬性,確保有對象在一直引用AVCaptureSession對象;否則如果在lightSensitive方法中定義并初始化AVCaptureSession對象,會造成AVCaptureSession對象提前釋放, [self.session startRunning];會失效
實現(xiàn)AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,參數(shù)brightnessValue就是周圍環(huán)境的亮度參數(shù)了,范圍大概在-5~~12之間,參數(shù)數(shù)值越大,環(huán)境越亮
關于怎么在iOS中利用攝像頭獲取環(huán)境光感參數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。