iOS 二维码扫描相关

写在前面

最近项目要实现相机扫描二维码功能,具体要求:1、扫描框 2、扫描动画 3、相册识别二维码 4、声音反馈。
记得之前用过三方库做过类似功能,但是也是知其然不知其所以然,然后今天自己用原生api简单封装了一个二维码扫描控件。

项目结构介绍

控件封装后主要结构如图:


屏幕快照 2018-09-07 上午9.01.52.png

如图中代码目录,vender里面放的是UIView+Frame分类,Resource里面放的是图片声音资源,TZImagePickerController是第三方相册,用来获取相册中的二维码识别的。主要的就是以QR开头的文件,我们具体说一说。

QRCode.h

这个文件主要放的是各个文件的头文件,方便在各处调用

#import "QRCodeScanManager.h"
#import <AVFoundation/AVFoundation.h>
#import "QRLightManager.h"
#import "QRCodeScanView.h"
#import "QRCodeHelper.h"
QRLightManager

这个类是用来开启关闭闪光灯的

/**
 打开手电筒
 */
+ (void)openFlashLight;


/**
 关闭手电筒
 */
+ (void)closeFlashLight;
#pragma mark 打开手电筒
+ (void)openFlashLight {
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    if ([captureDevice hasTorch]) {
        BOOL locked = [captureDevice lockForConfiguration:&error];
        if (locked) {
            captureDevice.torchMode = AVCaptureTorchModeOn;
            [captureDevice unlockForConfiguration];
        }
    }
}

#pragma mark 关闭手电筒
+ (void)closeFlashLight {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}
QRCodeScanView

这个类是将这个界面单独封装出来,便于自定义
屏幕快照 2018-09-07 上午9.07.03.png

在.h文件中有个枚举用来标识二维码扫描四周角标的位置:

typedef enum : NSUInteger {
    CornerLoactionDefault,//默认与边框同中心点
    CornerLoactionInside,//在边框线内部
    CornerLoactionOutside,//在边框线外部
} CornerLoaction;

自定义view各个属性:

@property (nonatomic, strong) UIColor *borderColor;/** 边框颜色*/
@property (nonatomic, assign) CornerLoaction cornerLocation;/** 边角位置 默认default*/
@property (nonatomic, strong) UIColor *cornerColor;/** 边角颜色 默认正保蓝#32d2dc*/
@property (nonatomic, assign) CGFloat cornerWidth;/** 边角宽度 默认2.f*/
@property (nonatomic, assign) CGFloat backgroundAlpha;/** 扫描区周边颜色的alpha 默认0.5*/
@property (nonatomic, assign) CGFloat timeInterval;/** 扫描间隔 默认0.02*/
@property (nonatomic, strong) UIButton *lightBtn;/** 闪光灯*/

暴露外部调用的方法:

/**
 添加定时器
 */
- (void)addTimer;

/**
 移除定时器
 */
- (void)removeTimer;

/**
 根据灯光判断
 */
- (void)lightBtnChangeWithBrightnessValue:(CGFloat)brightnessValue;

初始化默认值:

- (void)initilize {
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.borderColor = [UIColor whiteColor];
    _cornerLocation = CornerLoactionDefault;
    _cornerColor = [UIColor colorWithRed:50/255.0f green:210/255.0f blue:220/255.0f alpha:1.0];
    _cornerWidth = 2.0;
    self.timeInterval = 0.02;
    _backgroundAlpha = 0.5;
    
    [self addSubview:self.lightBtn];
}

重写view的drawRect方法,在这个方法里面绘制scanView的边框和边角

//空白区域设置
    [[[UIColor blackColor] colorWithAlphaComponent:self.backgroundAlpha] setFill];
    UIRectFill(rect);
    //获取上下文,并设置混合模式 -> kCGBlendModeDestinationOut
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
    //设置空白区
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX + 0.5 * borderLineW, borderY+ 0.5 * borderLineW, borderW - borderLineW, borderH - borderLineW)];
    [bezierPath fill];
    //执行混合模式
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    
    //边框设置
    UIBezierPath *borderPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX, borderY, borderW, borderH)];
    borderPath.lineCapStyle = kCGLineCapButt;
    borderPath.lineWidth = borderLineW;
    [self.borderColor set];
    [borderPath stroke];
    
    CGFloat cornerLength = 20;
    //左上角小图标
    UIBezierPath *leftTopPath = [UIBezierPath bezierPath];
    leftTopPath.lineWidth = self.cornerWidth;
    [self.cornerColor set];
    
    CGFloat insideExcess = fabs(0.5 * (self.cornerWidth - borderLineW));
    CGFloat outsideExcess = 0.5 * (borderLineW + self.cornerWidth);
    if (self.cornerLocation == CornerLoactionInside) {
        [leftTopPath moveToPoint:CGPointMake(borderX + insideExcess, borderY + cornerLength + insideExcess)];
        [leftTopPath addLineToPoint:CGPointMake(borderX + insideExcess, borderY + insideExcess)];
        [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength + insideExcess, borderY + insideExcess)];
    } else if (self.cornerLocation == CornerLoactionOutside) {
        [leftTopPath moveToPoint:CGPointMake(borderX - outsideExcess, borderY + cornerLength - outsideExcess)];
        [leftTopPath addLineToPoint:CGPointMake(borderX - outsideExcess, borderY - outsideExcess)];
        [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength - outsideExcess, borderY - outsideExcess)];
    } else {
        [leftTopPath moveToPoint:CGPointMake(borderX, borderY + cornerLength)];
        [leftTopPath addLineToPoint:CGPointMake(borderX, borderY)];
        [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength, borderY)];
    }
    [leftTopPath stroke];

增加定时器以及开始动画:

- (void)addTimer {
    [self addSubview:self.scanningLine];
    self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(beginAnimaiton) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
#pragma mark 动画
- (void)beginAnimaiton {
    static BOOL isOrignPostion = YES;
    
    if (isOrignPostion) {
        _scanningLine.y = 0;
        isOrignPostion = NO;
        [UIView animateWithDuration:self.timeInterval animations:^{
            self->_scanningLine.y += 2;
        } completion:nil];
    } else {
        if (_scanningLine.frame.origin.y >= 0) {
            CGFloat scanContent_MaxY = self.frame.size.width;
            if (_scanningLine.y >= scanContent_MaxY - 10) {
                _scanningLine.y = 0;
                isOrignPostion = YES;
            } else {
                [UIView animateWithDuration:0.02 animations:^{
                    self->_scanningLine.y += 2;
                    
                } completion:nil];
            }
        } else {
            isOrignPostion = !isOrignPostion;
        }
    }
}

闪光灯按钮点击事件,开启关闭闪光灯:

- (void)lightBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (btn.selected) {
        [QRLightManager openFlashLight];
    } else {
        [QRLightManager closeFlashLight];
    }
}
QRCodeScanManager

这个单例用来控制所有的关于二维码扫描的事件

初始开启session 会话

//设置二维码读取率 数据类型 当前控制器
- (void)setupSessionPreset:(NSString *)sessionPreset metadataObjectTypes:(NSArray *)metadataObjectTypes currentController:(UIViewController *)currentController {
    if (sessionPreset == nil || metadataObjectTypes == nil || currentController == nil) {
        NSException *excp = [NSException exceptionWithName:@"excp" reason:@"setupSessionPreset:metadataObjectTypes:currentController: 方法中的 sessionPreset 参数不能为空"  userInfo:nil];
        [excp raise];
    }
    
    //1、获取摄像设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //2、创建设备输入流
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    //3、创建数据输出流
    AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //3(1)、创建设备输出流
    self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    
    // 设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)
    // 注:微信二维码的扫描范围是整个屏幕,这里并没有做处理(可不用设置); 如需限制扫描范围,打开下一句注释代码并进行相应调试
    //    metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);
    
    //4、创建会话对象
    _session = [[AVCaptureSession alloc] init];
    //会话采集率:AVCaptureSessionPresetHigh
    _session.sessionPreset = sessionPreset;
    
    //5、添加设备输出流到会话对象
    [_session addOutput:metadataOutput];
    //5(1)添加设备输出流到会话对象;与3(1)构成识别光纤强弱
    [_session addOutput:_videoDataOutput];
    
    //6、添加设备输入流到会话对象
    [_session addInput:deviceInput];
    
    //7、设置数据输出类型,需要将数据输出添加到会话后,才能指定元数据类型,否则会报错
    // 设置扫码支持的编码格式(如下设置条形码和二维码兼容)
    // @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code,  AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]
    metadataOutput.metadataObjectTypes = metadataObjectTypes;
    
    //8、实例化预览图层,传递_session是为了告诉图层将来显示什么内容
    _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    //保持纵横比;填充层边界
    _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    CGFloat h = [UIScreen mainScreen].bounds.size.height;
    _videoPreviewLayer.frame = CGRectMake(x, y, w, h);
    [currentController.view.layer insertSublayer:_videoPreviewLayer atIndex:0];
    
    //9、启动会话
    [_session startRunning];
    
}

block:

typedef void(^GetBrightnessBlock)(CGFloat brightness);//用来向外部传递捕获到的亮度值以便于识别何时开启闪光灯,
typedef void(^ScanBlock)(NSArray *metadataObjects);//捕获到的结果集合

//亮度回调
- (void)brightnessChange:(GetBrightnessBlock)getBrightnessBlock;

//扫描结果
- (void)scanResult:(ScanBlock)scanBlock;
- (void)startRunning {
    [_session startRunning];
}

- (void)stopRunning {
    [_session stopRunning];
}

需要遵循:<AVCaptureMetadataOutputObjectsDelegate, AVCaptureVideoDataOutputSampleBufferDelegate>

/**
 重置根据光线强弱值打开手电筒 delegate方法
 */
- (void)resetSampleBufferDelegate;

/**
 取消根据光线强弱值打开手电筒的delegate方法
 */
- (void)cancelSampleBufferDelegate;

- (void)resetSampleBufferDelegate {
    [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
}

- (void)cancelSampleBufferDelegate {
    [_videoDataOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];
}
#pragma mark 播放扫描提示音
- (void)playSoundName:(NSString *)name {
    NSString *audioFile = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *fileUrl = [NSURL fileURLWithPath:audioFile];
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    AudioServicesPlaySystemSound(soundID); // 播放音效
}

void soundCompleteCallback(SystemSoundID soundID, void *clientData){
    
}

注:这里只是截取部分重要代码,具体功能我会将代码放到GitHub上,代码里注释也写的很明白

使用功能

开启、结束定时器
开启、关闭session会话
启用、移除sampleBuffer代理

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.scanView addTimer];
    [_scanManager startRunning];
    [_scanManager resetSampleBufferDelegate];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.scanView removeTimer];
    [_scanManager stopRunning];
    [_scanManager cancelSampleBufferDelegate];
}

初始化scanManager

- (void)setupScanManager {
    self.scanManager = [QRCodeScanManager sharedManager];
    
    NSArray *arr = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    [_scanManager setupSessionPreset:AVCaptureSessionPreset1920x1080 metadataObjectTypes:arr currentController:self];
    
    __weak __typeof(self)weakSelf = self;
    //光扫描结果回调
    [_scanManager scanResult:^(NSArray *metadataObjects) {
        if (metadataObjects != nil && metadataObjects.count > 0) {
            [weakSelf.scanManager playSoundName:@"sound.caf"];
            //obj 为扫描结果
            AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
            NSString *url = [obj stringValue];
            NSLog(@"---url = :%@", url);
        } else {
            NSLog(@"暂未识别出扫描的二维码");
        }
    }];
    
    //光纤变化回调
    [_scanManager brightnessChange:^(CGFloat brightness) {
        [weakSelf.scanView lightBtnChangeWithBrightnessValue:brightness];
    }];
    
}

从相册识别二维码:

//借助第三方相册
- (void)albumBtnClick {
    TZImagePickerController *pickerController = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    
    __weak __typeof(self)weakSelf = self;
    
    [pickerController setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        UIImage *image = photos[0];
        // CIDetector(CIDetector可用于人脸识别)进行图片解析,从而使我们可以便捷的从相册中获取到二维码
        // 声明一个 CIDetector,并设定识别类型 CIDetectorTypeQRCode
        // 识别精度
        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
        
        //取得识别结果
        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
        
        NSString *resultStr;
        if (features.count == 0) {
            NSLog(@"暂未识别出二维码");
        } else {
            for (int index = 0; index < [features count]; index++) {
                CIQRCodeFeature *feature = [features objectAtIndex:index];
                resultStr = feature.messageString;
            }
            NSLog(@"---url:%@", resultStr);
        }
    }];
    [self presentViewController:pickerController animated:YES completion:nil];
}
最后

时间有些紧,因为在代码里大部分地方都有写了注释的,里面也有借鉴的其他大牛的代码,部分功能只是简单写了出来,还没有细化,会再继续完善一下,最后放上GitHub链接:https://github.com/irembeu/QRCodeDemo.git 希望有问题可以评论留言私信,一起交流

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容