iOS自动打开闪光灯
现在好多应有都具备扫码功能,为了减少用户操作,一般会在光线比较暗的时候,自动打开闪光灯:
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自动打开闪光灯的更多相关文章
- MAC上搭建Jenkins + Android + IOS自动开发部署环境
因为MAC是大小写不敏感的操作系统,很多Linux命令不支持,所以首先要创建大小写敏感的操作系统. 设置静态IP 打开"System Preferences..." 点击" ...
- 微信中打开链接,自动打开外部浏览器打开指定URL页面
上半年公司有一个新的APP项目上线,我们在项目首页做个二维码,然后用户用手机扫一扫就能下载了.但是很多用户反映扫一扫之后下载不了,了解之后才知道这些用户都是使用的微信的扫一扫,而我们开发测试人员一般使 ...
- 40、IOS自动打包-Python脚本
第一种:基于编译的打包 编译工程--找到.app文件--新建Payload文件夹--拷贝.app到Payload文件夹--压缩成zip--更改后缀名为ipa--完成! 第二种(有问题,暂时不需要看) ...
- Office OneNote 自动打开问题
你可能遇到如下问题: 在输入某些文本的时候,突然onenote自动打开 切换输入法,onenote自动打开 使用某些快捷键,onenote自动打开 最近我就遇到类似诡异的问题,使用了各种搜索引擎,都没 ...
- inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...
- 8、NFC技术:让Android自动打开网页
创建封装Uri的NdefRecord public NdefRecord createUri(String uriString); public NdefRecord cre ...
- Centos自动登录系统并自动打开VNC Server
系统自动登录 修改配置文件 sudo vim /etc/gdm/custom.conf 增加配置 [daemon] AutomaticLogin=spark AutomaticLoginEnable= ...
- iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”
iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...
- nau8822 codec driver 录音时mic bias 无法自动打开问题
nau8822 codec driver 录音时mic bias 无法自动打开问题 问题描述: kernel版本:3.10 在nuc970上测试nau8822驱动时发现,虽然驱动中有如下定义: SND ...
随机推荐
- node express formidable 文件上传后修改文件名
//我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...
- 01.使用File类读写文件
使用File类去读数据: 方法一:使用ReadAllBytes(); 方法 namespace _01.使用File类读写数据01 { class Program { static void Main ...
- 05.while循环的练习
练习1: namespace _05.while循环练习01 { class Program { static void Main(string[] args) { //打印100次"努力学 ...
- C# tcp udp 串口 通信
简单的实现tcp同步和异步,udp,串口通信 static List<TcpClientState> clientArray = new List<TcpClientState> ...
- PAT 1059. Prime Factors
反正知道了就是知道,不知道也想不到,很快 #include <cstdio> #include <cstdlib> #include <vector> using ...
- VMware 安装提示缺少MicrosoftRuntime DLL 问题解决办法
VMware 安装提示缺少MicrosoftRuntime DLL 问题解决办法 刚刚安装VMware失败了试了好多办法,在这总结一下. 下面是程序的截图 这是报错信息 网上的解决方法: 当出现安装失 ...
- python30题
1.执行Python 脚本的两种方式 使用python解释器(python aa.py)或在unix系统下赋值成777,执行(./aa.py) 2.简述位.字节的关系 1个byte = 8bit,在A ...
- mysql三-3:完整性约束
一.完整性约束介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIG ...
- Android 自定义ListView滚动条样式
使用ListView FastScroller,默认滑块和自定义滑块图片的样子: 设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可: <ListView android:id=&qu ...
- matlab练习程序(演化策略ES)
还是这本书上的内容,不过我看演化计算这一章是倒着看的,这里练习的算法正好和书中介绍的顺序是相反的. 演化策略是最古老的的演化算法之一,和上一篇DE算法类似,都是基于种群的随机演化产生最优解的算法. 算 ...