怎么在iOS中利用摄像头获取环境光感参数-创新互联

这篇文章给大家介绍怎么在iOS中利用摄像头获取环境光感参数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

在昭化等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、网站设计 网站设计制作定制开发,公司网站建设,企业网站建设,品牌网站制作,营销型网站建设,成都外贸网站建设,昭化网站建设费用合理。
#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.获取硬件设备
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

  // 2.创建输入流
  AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];

  // 3.创建设备输出流
  AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
  [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];


  // AVCaptureSession属性
  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];
  }

  // 9.启动会话
  [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);


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

  }

}

@end

注意点:

  1. 首先引入AVFoundation框架和ImageIO/ImageIO.h声明文件

  2. 遵循AVCaptureVideoDataOutputSampleBufferDelegate协议

  3. AVCaptureSession对象要定义为属性,确保有对象在一直引用AVCaptureSession对象;否则如果在lightSensitive方法中定义并初始化AVCaptureSession对象,会造成AVCaptureSession对象提前释放, [self.session startRunning];会失效

  4. 实现AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,参数brightnessValue就是周围环境的亮度参数了,范围大概在-5~~12之间,参数数值越大,环境越亮

关于怎么在iOS中利用摄像头获取环境光感参数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享标题:怎么在iOS中利用摄像头获取环境光感参数-创新互联
分享路径:https://www.cdcxhl.com/article28/joejp.html

成都网站建设公司_创新互联,为您提供定制网站网站设计品牌网站制作做网站面包屑导航移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司