现在好多应有都具备扫码功能,为了减少用户操作,一般会在光线比较暗的时候,自动打开闪光灯:

1、导入头文件

#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>

2、创建设备、输入输出流

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

3、实现代理方法

#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];
// brightnessValue 值代表光线强度,值越小代表光线越暗
if (brightnessValue <= - && !_isAutoOpen) {
_isAutoOpen = YES;
[self.torchBtn setSelected:YES];
[self turnTorchOn:YES];
}
}

4、开启关闭闪光灯

// 打开/关闭手电筒
- (void)turnTorchOn:(BOOL)on{
if ([self.device hasTorch] && [self.device hasFlash]){ [self.device lockForConfiguration:nil];
if (on) {
[self.device setTorchMode:AVCaptureTorchModeOn];
[self.device setFlashMode:AVCaptureFlashModeOn];
} else {
[self.device setTorchMode:AVCaptureTorchModeOff];
[self.device setFlashMode:AVCaptureFlashModeOff];
}
[self.device unlockForConfiguration];
} else {
[SSAlertView showWithTitle:@"提示" message:@"当前设备没有闪光灯,不能提供手电筒功能" commitTitle:@"我知道了" cancelTitle:nil commitAction:nil cancelBlock:nil];
}
}
转自:http://www.cenzhijun.top/2017/07/自动打开闪光灯/

iOS自动打开闪光灯的更多相关文章

  1. MAC上搭建Jenkins + Android + IOS自动开发部署环境

    因为MAC是大小写不敏感的操作系统,很多Linux命令不支持,所以首先要创建大小写敏感的操作系统. 设置静态IP 打开"System Preferences..." 点击" ...

  2. 微信中打开链接,自动打开外部浏览器打开指定URL页面

    上半年公司有一个新的APP项目上线,我们在项目首页做个二维码,然后用户用手机扫一扫就能下载了.但是很多用户反映扫一扫之后下载不了,了解之后才知道这些用户都是使用的微信的扫一扫,而我们开发测试人员一般使 ...

  3. 40、IOS自动打包-Python脚本

    第一种:基于编译的打包 编译工程--找到.app文件--新建Payload文件夹--拷贝.app到Payload文件夹--压缩成zip--更改后缀名为ipa--完成! 第二种(有问题,暂时不需要看) ...

  4. Office OneNote 自动打开问题

    你可能遇到如下问题: 在输入某些文本的时候,突然onenote自动打开 切换输入法,onenote自动打开 使用某些快捷键,onenote自动打开 最近我就遇到类似诡异的问题,使用了各种搜索引擎,都没 ...

  5. inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效

    inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...

  6. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...

  7. Centos自动登录系统并自动打开VNC Server

    系统自动登录 修改配置文件 sudo vim /etc/gdm/custom.conf 增加配置 [daemon] AutomaticLogin=spark AutomaticLoginEnable= ...

  8. iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”

    iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...

  9. nau8822 codec driver 录音时mic bias 无法自动打开问题

    nau8822 codec driver 录音时mic bias 无法自动打开问题 问题描述: kernel版本:3.10 在nuc970上测试nau8822驱动时发现,虽然驱动中有如下定义: SND ...

随机推荐

  1. Java根据byte数组,生成文件

    原文出自:https://blog.csdn.net/seesun2012 根据byte数组,生成文件 自己写的小案例,找个地方记录一下 package com.seesun2012.utils; i ...

  2. 数组和矩阵(1)——Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  3. 菜鸟学配置vim

    看啥都不会的菜鸟怎么进行vim配置 如果你想让你的vim和VS差不多你一定需要这个网址 http://www.open-open.com/lib/view/open1429884437588.html ...

  4. sql:无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲突。

    --无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲 ...

  5. Django——model进阶(待完成)

    https://www.cnblogs.com/yuanchenqi/articles/7570003.html 一.QuerySet 1.可切片 使用Python 的切片语法来限制查询集记录的数目  ...

  6. gsap

    TweenMax借助于css,轻量级的js库 下面是简单的demo <!DOCTYPE html> <html lang="en"> <head> ...

  7. js简单实现表格排序

    昨天看到一篇关于表格排序的随笔,鉴于本人还只会简单的js,不会使用封装,所以自己也试了一下写这个效果.可能不太优化,原理思路是:获取行对象tr,排序tr中要比较的对象td,排序后添加回tbody.如下 ...

  8. 类数组arguments

    var isArray = function(){ return arguments; } isArray(1,2,3); // 返回[1,2,3] isArray.call(null,1,2,3); ...

  9. 【Linux】 静态函数库设计

    一.外部函数来源-- 函数库&系统调用 二.函数库分类 静态函数库 --多份拷贝 动态函数库 --单份拷贝 区别 链接方式区别 三.函数库存放位置 Linux应用程序使用的主要函数库均存放于/ ...

  10. java实现12306的45分钟内支付,45分钟后取消订单功能?

    java实现12306的45分钟内支付,45分钟后取消订单功能? - 回答作者: 匿名用户 https://zhihu.com/question/27254071/answer/35948645