博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二维码快速扫描工具
阅读量:5085 次
发布时间:2019-06-13

本文共 4937 字,大约阅读时间需要 16 分钟。

演示.gif

YHQRCode

二维码快速扫描工具

项目简介:

  1. 该项目中只有一个控制器,路由效果由动画组成。
  2. 实际应用中可以按需求进行改动。

项目主要应用技术点

调用系统相机设备 AVCaptureDevice 指定输出与输入:

AVCaptureDevice *device = [AVCaptureDevice  defaultDeviceWithMediaType:AVMediaTypeVideo];NSError *error;AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

设置输出元数据格式:

output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

配置显示图层:

_previewlayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];        //   显示方式  --  填充_previewlayer.videoGravity  = AVLayerVideoGravityResizeAspectFill;    _previewlayer.frame = _qrView.layer.bounds;

绘制二维码页面:

- (void)drawRect:(CGRect)rect {    // Drawing code    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetRGBFillColor(context, 191/255.0, 191/255.0, 191/255.0, 0.5);    CGContextMoveToPoint(context, 0, 0);        CGContextAddLineToPoint(context, rect.size.width, 0);    CGContextAddLineToPoint(context, rect.size.width, rect.size.height);    CGContextAddLineToPoint(context, 0, rect.size.height);        CGContextFillPath(context);    CGSize size = rect.size;    CGContextClearRect(context, CGRectMake(QRRECT_ORIGN_X, QRRECT_ORIGN_Y, QRRECT_ORIGN_WIDTH,QRRECT_ORIGN_WIDTH));            CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);    CGContextSetLineWidth(context, 4);            //    左上角    CGContextMoveToPoint(context,rect.size.width/2 -100 , 110);    CGContextAddLineToPoint(context, rect.size.width/2 -100, 100);    CGContextAddLineToPoint(context, rect.size.width/2 -90, 100);        //    右上角    CGContextMoveToPoint(context,rect.size.width/2 +90 , 100);    CGContextAddLineToPoint(context, rect.size.width/2 +100, 100);    CGContextAddLineToPoint(context, rect.size.width/2 +100, 110);            //    左下角    CGContextMoveToPoint(context,rect.size.width/2 -100 , 290);    CGContextAddLineToPoint(context, rect.size.width/2 -100, 300);    CGContextAddLineToPoint(context, rect.size.width/2 -90, 300);        //   右下角    CGContextMoveToPoint(context,rect.size.width/2 +90 , 300);    CGContextAddLineToPoint(context, rect.size.width/2 +100, 300);    CGContextAddLineToPoint(context, rect.size.width/2 +100, 290);        CGContextStrokePath(context);}

扫描动画:

[UIView beginAnimations:nil context:nil];    [UIView setAnimationRepeatCount:1000];    [UIView   setAnimationBeginsFromCurrentState:YES];    [UIView setAnimationRepeatAutoreverses:YES];    [UIView setAnimationDuration:4.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [_middleBar setTransform:CGAffineTransformTranslate(_middleBar.transform ,0,200)];    [UIView commitAnimations];

照明设置

//照明- (void)torchSwitch:(UITapGestureRecognizer *)sender{    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    _torchBar.selected = !_torchBar.selected;    [_torchBar setTitle:@"关闭照明" forState:UIControlStateSelected];    if ([device hasTorch]) {        [device lockForConfiguration:nil];//开始配置        if (_torchBar.selected) {            [device setTorchMode:AVCaptureTorchModeOn];        }else{            [device setTorchMode:AVCaptureTorchModeOff];        }        [device unlockForConfiguration];//结束配置    }}

懒加载

//lazy load- (QRView *)qrView{    if (!_qrView) {        _qrView = [[QRView alloc]initWithFrame:self.view.bounds];        _qrView.backgroundColor = [UIColor clearColor];    }    return _qrView;}

自定义相应方法

- (void)showTipsAlertWithTitle:(NSString *)title subTitle:(NSString *)subTitle  cAction:(SEL)cAction hasCancel:(BOOL)hasCancel{        UIAlertController *alertVC =[UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleAlert];    __block  YHQVIewViewController *selfVC = self;    __block SEL myAction = cAction;        UIAlertAction *okBtn = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault  handler:^(UIAlertAction * _Nonnull action) {        if ( [selfVC respondsToSelector:cAction]) {            [selfVC performSelector:cAction];        }else{            myAction = nil;        }            }];        if (hasCancel) {        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault  handler:nil];        [alertVC addAction:cancelBtn];    }    [alertVC addAction:okBtn];        [self presentViewController:alertVC animated:YES completion:nil];}

播放指示声音

NSError *error;        _audioPalyer = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"蜂鸣器声音" ofType:@"mp3"]] error:&error];        BOOL ready = [_audioPalyer prepareToPlay];        NSLog(@"audioPlayer ready = %@  error=%@",ready?@"YES":@"NO",error);  if ([_audioPalyer prepareToPlay]) {            [_audioPalyer play];        }

转载于:https://www.cnblogs.com/tig666666/p/7298765.html

你可能感兴趣的文章
Educational Codeforces Round 56 Editorial
查看>>
BCD码(二-十进制)
查看>>
使用JavaScript扫描端口
查看>>
qdtuling.xyz 7.8
查看>>
Java工程师成神之路
查看>>
Java常用工具方法
查看>>
【canvas】先绘制标准图形,在进行图形变换
查看>>
两个命令:hdparm和iozone参数解释
查看>>
angular设置全局变量,修改监听变量
查看>>
alter column和modify column
查看>>
线性代数矩阵知识
查看>>
uni-app教程入门视频资料
查看>>
PHP 语法
查看>>
java程序在linux上持续运行方法 nohup 和 tmux
查看>>
Tomcat组件梳理—Service组件
查看>>
图解 HTTP 笔记(二)——简单的 HTTP 协议
查看>>
跟踪mqttv3源码(一)
查看>>
selenium点击(click)页面元素没有反应(报element not interactable)的一个案例
查看>>
c# 在windows服务中 使用定时器
查看>>
获取配置文件头信息
查看>>