美文网首页
根据环境的明暗程度提示闪光灯开关

根据环境的明暗程度提示闪光灯开关

作者: 阿小大人 | 来源:发表于2017-10-11 11:07 被阅读24次

微信在扫描二维码时会根据环境的明暗程度提示用户是否打开闪关灯,这样的细节还是不错的。那我们应该怎么实现呢?

我的思路是利用摄像头获取环境光感参数,小于一定值后提示用户打开闪光灯。不过微信、支付宝这些可能也结合摄像头的图像来做分析吧。

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

@interface LightSensitiveVC () <AVCaptureVideoDataOutputSampleBufferDelegate>

@property (nonatomic, strong) AVCaptureSession *session;

@property (weak, nonatomic) IBOutlet UIButton *torchSwitchBtn;

@end

@implementation LightSensitiveVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"光感";
    // 设置光感
    [self lightSensitiveSetting];
}

#pragma mark - 设置光感
- (void)lightSensitiveSetting
{
    // 获取硬件设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    // 创建输入流
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
    // 创建设备输出流
    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];
    }
    // 启动会话
    [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];
    // 根据brightnessValue的值来现实隐藏闪光灯开关按钮
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    BOOL result = [device hasTorch];// 判断设备是否有闪光灯
    if (result && device.torchMode == AVCaptureTorchModeOff) {
        if (brightnessValue < 0 && _torchSwitchBtn.hidden == YES) {
            [UIView animateWithDuration:0.4 animations:^{
                _torchSwitchBtn.hidden = NO;
            }];
        } else if(brightnessValue > 0 && _torchSwitchBtn.hidden == NO) {
            [UIView animateWithDuration:0.4 animations:^{
                _torchSwitchBtn.hidden = YES;
            }];
        }
    }
}

- (IBAction)torchSwitchAction:(UIButton *)sender
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    BOOL result = [device hasTorch];
    if (result) {
        if (sender.selected == NO) {
            // 打开闪光灯
            [device lockForConfiguration:nil];
            [device setTorchMode: AVCaptureTorchModeOn];
            [device unlockForConfiguration];
            [sender setTitle:@"关闭照明" forState:UIControlStateNormal];
            sender.selected = YES;
        } else if(sender.selected == YES) {
            // 关闭闪光灯
            [device lockForConfiguration:nil];
            [device setTorchMode: AVCaptureTorchModeOff];
            [device unlockForConfiguration];
            [sender setTitle:@"环境较暗 开启照明" forState:UIControlStateNormal];
            sender.selected = NO;
        }
    }
}

实现AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,参数brightnessValue就是环境的光感参数,范围大概在 -5 ~ 12 之间,参数数值越大,代表环境越亮。

项目代码:https://github.com/thunder0513/LightSensitive

相关文章

  • 根据环境的明暗程度提示闪光灯开关

    微信在扫描二维码时会根据环境的明暗程度提示用户是否打开闪关灯,这样的细节还是不错的。那我们应该怎么实现呢? 我的思...

  • 反光板

    反光板作为拍摄中的辅助设备,他的常见程度不亚于闪光灯。 根据环境需要用好反光板,可以让平淡的画面变得更加饱满、体现...

  • iOS自定义相机

    本文是iOS自定义相机主要功能,包括前后置摄像头,闪光灯,检测环境光线明暗等功能

  • 科普-手机环境光强度检测的两种方案

    应用场景 例如: 一 根据环境光亮度自动调节屏幕亮度; 二 根据环境光亮度自动打开闪光灯; 三 根绝环境光亮度自动...

  • 【广告机更新配置】

    总开关开始【ON】总开关结束 总开关提示开始【程序出现异常错误!】总开关提示结束 QQ授权列表开始【1146044...

  • 【打印助手更新配置】

    总开关开始【ON】总开关结束 总开关提示开始【程序出现异常错误!】总开关提示结束 版本号开始【2.0】版本号结束 ...

  • iOS 扫码(闪光灯、自动拉近放大)

    需求:产品要实现类似微信扫码一样效果的功能,光线暗的时候,能够自动打开闪光灯(并出现闪光灯控制开关图标),然后用户...

  • 远程公告

    【开关】关【开关】【标题内容】温馨提示:【标题内容】【公告内容】【公告内容】【按钮】关闭提示【按钮】【弹送时间】0...

  • 通过手机一个按键打开和关闭手电筒

    今天接到一个需求手机Fn按键需要进行打开关闭闪光灯坑爹的是打开关闭是同一个按键 其实并不难由于今天直接在广播打开关...

  • Swift Code Block Backups

    简单粗暴 不定期更新(始于161128)...... 打开关闭闪光灯 UISearchBar 去除背景灰色 某一页...

网友评论

      本文标题:根据环境的明暗程度提示闪光灯开关

      本文链接:https://www.haomeiwen.com/subject/pewiyxtx.html