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

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

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

2020-07-26 02:37:04
字體:
供稿:網(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.獲取硬件設(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):

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 托里县| 临沧市| 大理市| 区。| 交城县| 娄烦县| 南涧| 盐山县| 基隆市| 鄢陵县| 清徐县| 潮安县| 嘉峪关市| 平度市| 大新县| 麻江县| 新源县| 景泰县| 林甸县| 无极县| 澜沧| 东莞市| 金山区| 蒲江县| 滕州市| 梁山县| 衡水市| 昌平区| 南阳市| 永新县| 盐山县| 尉氏县| 灵宝市| 枝江市| 崇仁县| 托里县| 岢岚县| 海口市| 潼关县| 锡林郭勒盟| 兴和县|