当前位置:   article > 正文

iOS 屏幕亮度和闪光灯控制_ios didoutputsamplebuffer 像素点 亮度

ios didoutputsamplebuffer 像素点 亮度

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

  1. 读取屏幕亮度:[UIScreen mainScreen].brightness;
  2. 设置屏幕亮度:[[UIScreen mainScreen] setBrightness:0.5];

获取环境亮度主要代码:

  1. - (void)getTorch {
  2. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  3. AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
  4. AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
  5. [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  6. self.session = [[AVCaptureSession alloc]init];
  7. [self.session setSessionPreset:AVCaptureSessionPresetHigh];
  8. if ([self.session canAddInput:input]) {
  9. [self.session addInput:input];
  10. }
  11. if ([self.session canAddOutput:output]) {
  12. [self.session addOutput:output];
  13. }
  14. [self.session startRunning];
  15. }
  16. - (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection {
  17. CFDictionaryRef metadataDict =CMCopyDictionaryOfAttachments(NULL,sampleBuffer,
  18. kCMAttachmentMode_ShouldPropagate);
  19. NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:
  20. (__bridgeNSDictionary*)metadataDict];
  21. CFRelease(metadataDict);
  22. NSDictionary *exifMetadata = [[metadata objectForKey:(NSString*)kCGImagePropertyExifDictionary] mutableCopy];
  23. float brightnessValue = [[exifMetadata objectForKey:(NSString*)kCGImagePropertyExifBrightnessValue] floatValue];
  24. NSLog(@"%f",brightnessValue);
  25. // 根据brightnessValue的值来打开和关闭闪光灯
  26. AVCaptureDevice*device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  27. BOOL result = [device hasTorch];// 判断设备是否有闪光灯
  28. if((brightnessValue <0) && result) {
  29. // 打开闪光灯
  30. [device lockForConfiguration:nil];
  31. [device setTorchMode:AVCaptureTorchModeOn];//开
  32. [device unlockForConfiguration];
  33. }else if((brightnessValue >0) && result) {
  34. // 关闭闪光灯
  35. [device lockForConfiguration:nil];
  36. [device setTorchMode:AVCaptureTorchModeOff];//关
  37. [device unlockForConfiguration];
  38. }
  39. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/229439
推荐阅读
相关标签
  

闽ICP备14008679号