查看: 1504|回复: 0

[IOS开发教程] iOS利用摄像头获取环境光感参数的方法

发表于 2017-12-27 08:00:00

本文介绍了iOS利用摄像头获取环境光感参数的方法,分享给大家,具体如下:

不多说,代码如下:

  1. #import "LightSensitiveViewController.h"
  2. @import AVFoundation;
  3. #import <ImageIO/ImageIO.h>
  4. @interface LightSensitiveViewController ()< AVCaptureVideoDataOutputSampleBufferDelegate>
  5. @property (nonatomic, strong) AVCaptureSession *session;
  6. @end
  7. @implementation LightSensitiveViewController
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10. // Do any additional setup after loading the view.
  11. self.view.backgroundColor = [UIColor whiteColor];
  12. self.navigationItem.title = @"光感";
  13. [self lightSensitive];
  14. }
  15. - (void)didReceiveMemoryWarning {
  16. [super didReceiveMemoryWarning];
  17. // Dispose of any resources that can be recreated.
  18. }
  19. #pragma mark- 光感
  20. - (void)lightSensitive {
  21. // 1.获取硬件设备
  22. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  23. // 2.创建输入流
  24. AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
  25. // 3.创建设备输出流
  26. AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
  27. [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  28. // AVCaptureSession属性
  29. self.session = [[AVCaptureSession alloc]init];
  30. // 设置为高质量采集率
  31. [self.session setSessionPreset:AVCaptureSessionPresetHigh];
  32. // 添加会话输入和输出
  33. if ([self.session canAddInput:input]) {
  34. [self.session addInput:input];
  35. }
  36. if ([self.session canAddOutput:output]) {
  37. [self.session addOutput:output];
  38. }
  39. // 9.启动会话
  40. [self.session startRunning];
  41. }
  42. #pragma mark- AVCaptureVideoDataOutputSampleBufferDelegate的方法
  43. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
  44. CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,sampleBuffer, kCMAttachmentMode_ShouldPropagate);
  45. NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(__bridge NSDictionary*)metadataDict];
  46. CFRelease(metadataDict);
  47. NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
  48. float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
  49. NSLog(@"%f",brightnessValue);
  50. // 根据brightnessValue的值来打开和关闭闪光灯
  51. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  52. BOOL result = [device hasTorch];// 判断设备是否有闪光灯
  53. if ((brightnessValue < 0) && result) {// 打开闪光灯
  54. [device lockForConfiguration:nil];
  55. [device setTorchMode: AVCaptureTorchModeOn];//开
  56. [device unlockForConfiguration];
  57. }else if((brightnessValue > 0) && result) {// 关闭闪光灯
  58. [device lockForConfiguration:nil];
  59. [device setTorchMode: AVCaptureTorchModeOff];//关
  60. [device unlockForConfiguration];
  61. }
  62. }
  63. @end
复制代码

注意点:

  1. 首先引入AVFoundation框架和ImageIO/ImageIO.h声明文件
  2. 遵循AVCaptureVideoDataOutputSampleBufferDelegate协议
  3. AVCaptureSession对象要定义为属性,确保有对象在一直引用AVCaptureSession对象;否则如果在lightSensitive方法中定义并初始化AVCaptureSession对象,会造成AVCaptureSession对象提前释放, [self.session startRunning];会失效
  4. 实现AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,参数brightnessValue就是周围环境的亮度参数了,范围大概在-5~~12之间,参数数值越大,环境越亮

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



回复

使用道具 举报