zoukankan      html  css  js  c++  java
  • 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];
        }
    }
    
    
  • 相关阅读:
    BZOJ 1412: [ZJOI2009]狼和羊的故事
    Bzoj 2443: [Usaco2011 Open]奇数度数
    Bzoj 1101: [POI2007]Zap
    BZOJ 2186: [Sdoi2008]沙拉公主的困惑
    BZOJ 4804: 欧拉心算 欧拉函数
    Luogu P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
    Luogu P3000 [USACO10DEC]牛的健美操Cow Calisthenics
    BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
    BZOJ 3297: [USACO2011 Open]forgot
    BZOJ 2456: mode
  • 原文地址:https://www.cnblogs.com/frankltf/p/7389107.html
Copyright © 2011-2022 走看看