国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法

2019-10-21 18:43:40
字體:
來源:轉載
供稿:網(wǎng)友

本文介紹了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.獲取硬件設備  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

注意點:

  1. 首先引入AVFoundation框架和ImageIO/ImageIO.h聲明文件
  2. 遵循AVCaptureVideoDataOutputSampleBufferDelegate協(xié)議
  3. AVCaptureSession對象要定義為屬性,確保有對象在一直引用AVCaptureSession對象;否則如果在lightSensitive方法中定義并初始化AVCaptureSession對象,會造成AVCaptureSession對象提前釋放, [self.session startRunning];會失效
  4. 實現(xiàn)AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,參數(shù)brightnessValue就是周圍環(huán)境的亮度參數(shù)了,范圍大概在-5~~12之間,參數(shù)數(shù)值越大,環(huán)境越亮

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。

 

注:相關教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 政和县| 翼城县| 航空| 缙云县| 黔南| 哈巴河县| 嵊州市| 常山县| 乌拉特中旗| 衢州市| 芒康县| 马龙县| 黄石市| 旺苍县| 临沂市| 柘荣县| 大安市| 金沙县| 平阳县| 桂林市| 巴林右旗| 蒙山县| 涞源县| 休宁县| 渭南市| 洛阳市| 和林格尔县| 濮阳县| 当雄县| 永州市| 安远县| 石柱| 苏尼特右旗| 龙川县| 米泉市| 新干县| 荣成市| 红河县| 简阳市| 巩留县| 长阳|