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

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. 网页生命周期-PageLoad事件

    PageLoad事件的作用 l 页面载入是执行的处理命令 l 可以动态创建控件 l 可以设置现有控件的属性和状态 l 常用来设置数据库的连接 l 每次页面载入都会执行

  2. mysql应用学习-windows(64位)安装和配置mysql(5.6.20)

    下载安装包MySQL Installer 下载地址1:http://dev.mysql.com/downloads/windows/installer/ 说明:官网当前版本 5.6.22:虽然只有32 ...

  3. A bug about RecipientEditTextView

    - Steps to reproduce the problem. Pre-condition:the threshold of the RecipientEditTextView is set to ...

  4. 菜鸟学配置vim

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

  5. OLEDB 静态绑定和数据转化接口

    OLEDB 提供了静态绑定和动态绑定两种方式,相比动态绑定来说,静态绑定在使用上更加简单,而在灵活性上不如动态绑定,动态绑定在前面已经介绍过了,本文主要介绍OLEDB中的静态,以及常用的数据类型转化接 ...

  6. java 判断图片的类型

    // 这种方法如果遇到不是图片类型的文件会reader抛异常! public static void main(String[] args) throws IOException { // get i ...

  7. SQLAlchemy的使用---M2M多对多关系

    from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, Stri ...

  8. CSS基础语法与选择器

    CSS基础 语法 : <head> <style type="text/css"> 选择器(即修饰对象){ 修饰属性:属性值; 修饰属性:属性值; } &l ...

  9. 通用的popupwindow底部弹出框

    前段时间做项目的时候,有几个底部弹出框,当时因为忙着赶进度所有就单独写了好几个popupwindow.后来就想着怎么实现一个通用的PopupWindow工具类 就是在要用到的时候创建该工具类的对象,并 ...

  10. 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述

    1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...