欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现屏幕亮度和闪光灯控制的实例代码

程序员文章站 2023-12-19 19:03:04
这两天学习了ios屏幕亮度和闪光灯控制,所以,今天添加一点小笔记。 所用涉及框架:avfoundation框架和imageio 读取屏幕亮度:[uiscreen mai...

这两天学习了ios屏幕亮度和闪光灯控制,所以,今天添加一点小笔记。

所用涉及框架:avfoundation框架和imageio

读取屏幕亮度:[uiscreen mainscreen].brightness;

设置屏幕亮度:[[uiscreen mainscreen] setbrightness:0.5];

获取环境亮度主要代码:

- (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);
  // 根据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];  
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: