• ios---二维码的扫描


    二维码扫描

    • 使用ios的AVFoundation框架实现二维码扫描
    • 第一步:设置相机访问权限;在Info.plist添加Privacy - Camera Usage Description权限
    • 第二步:导入AVFoundation框架 #import <AVFoundation/AVFoundation.h>
    • 第三步:签订AVCaptureMetadataOutputObjectsDelegate 协议
    • 第四步:设置输入输出的中间桥梁
    @property(nonatomic, strong)AVCaptureSession *session;/
    
    • 第五步:扫描方法
     -(void)startScanWithSize:(CGFloat)sizeValue{
        AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        AVCaptureDeviceInput *input=[AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        if(input){
            AVCaptureMetadataOutput *output=[[AVCaptureMetadataOutput alloc]init];
            [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
            self.session=[[AVCaptureSession alloc]init];
            [_session setSessionPreset:AVCaptureSessionPresetHigh];
            [_session addInput:input];
            [_session addOutput:output];
            output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code];
            AVCaptureVideoPreviewLayer *layer=[AVCaptureVideoPreviewLayer layerWithSession:_session];
            layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
            CGFloat xx=(self.view.bounds.size.width-sizeValue)/2;
            CGFloat yy=(self.view.bounds.size.height-sizeValue)/2;
            layer.frame=CGRectMake(xx, yy, sizeValue, sizeValue);
            [self.view.layer insertSublayer:layer atIndex:0];
            [_session startRunning];
        }
    }
    
    • 第六步:处理扫描结果
    -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
        if(metadataObjects.count>0){
            [_session stopRunning];
            AVMetadataMachineReadableCodeObject *metadataobject=[metadataObjects objectAtIndex:0];
            NSLog(@"%@",metadataobject.stringValue);
            AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *)[[self.view.layer sublayers] objectAtIndex:0];
            [layer removeFromSuperlayer];
        }
    }
    
    
  • 相关阅读:
    css 重新学习系列(1)
    sublime Text 使用
    值得收藏的前端大牛博客
    javascript中最常用的方法
    ie6,ie7兼容性总结
    jQuery学习笔记(二)
    jQuery学习笔记(一)
    php smarty
    javascript DOM(2) 一个网页上切换显示不同的图片或文本
    effective c++ 8: Prevent exceptions from leaving destrctors
  • 原文地址:https://www.cnblogs.com/frankltf/p/7389107.html
Copyright © 2020-2023  润新知