二维码快速扫描工具

演示.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{
    
    UIAlertController *alertVC =[UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleAlert];
    __block  ViewController *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;
        }
        
    }];
    
    [alertVC addAction:okBtn];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、前言 最近在做一个关于扫描二维码签到的小东西,所以还是上来写一篇关于二维码的文章,网上也有一些扫描二维码的框架...
    kim逸云阅读 4,565评论 2 8
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,955评论 25 709
  • 文▕ Henry妈妈 但凡研究心理学的人,大概都知道“知识的诅咒”,说的是如果我们很熟悉某个对象的话,那么我们会很...
    0f509d3a3ec2阅读 2,403评论 6 28
  • 李白《赠汪伦》原诗、注释、翻译、赏析 【原文】:赠汪伦①【唐】 李白李白乘舟将欲行,忽闻岸上踏歌②声。桃花潭③水深...
    xcy无名阅读 439评论 0 0
  • 今天带伊参加了北京绽放花园春•茶•诵的活动,这是年后的第一次相聚。大家一直是热情不减,这次依旧聚了二三十人。 大人...
    明媚Sopy阅读 222评论 0 0