欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼

 更新時(shí)間:2017年06月01日 14:41:37   作者:呀咪9527  
本篇文章主要介紹了iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下

這兩天學(xué)習(xí)了iOS屏幕亮度和閃光燈控制,所以,今天添加一點(diǎn)小筆記。

所用涉及框架:AVFoundation框架和ImageIO

讀取屏幕亮度:[UIScreen mainScreen].brightness;

設(shè)置屏幕亮度:[[UIScreen mainScreen] setBrightness:0.5];

獲取環(huán)境亮度主要代碼:

- (void)getTorch {
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
  AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
  [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  self.session = [[AVCaptureSession alloc]init];
  [self.session setSessionPreset:AVCaptureSessionPresetHigh];
  if ([self.session canAddInput:input]) {
    [self.session addInput:input];
  }
  if ([self.session canAddOutput:output]) {
    [self.session addOutput:output];
  }
  [self.session startRunning];

}

- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection {
  CFDictionaryRef metadataDict =CMCopyDictionaryOfAttachments(NULL,sampleBuffer, 
  kCMAttachmentMode_ShouldPropagate);
  NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:
  (__bridgeNSDictionary*)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];  
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論