美文网首页
扫描二维码

扫描二维码

作者: CaptainRoy | 来源:发表于2018-09-07 14:15 被阅读6次
    • 导入媒体类
    #import <AVFoundation/AVFoundation.h>
    
    • 然后在 viewDidLoad 中 判断权限
    - (void)viewDidLoad {
        [super viewDidLoad];
        // 判断权限
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!granted) {
                    NSLog(@"请在iPhone的”设置-隐私-相机“选项中,允许App访问你的相机");
                    return ;
                }
                [self initQRCode];
            });
        }];
    }
    
    • 初始化扫描二维码
    -(void)initQRCode
    {
        //1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo
        AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        //2.用captureDevice创建输入流
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:NULL];
        //3.创建媒体数据输出流
        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
        //4.实例化捕捉会话
        self.captureSession = [[AVCaptureSession alloc] init];
        [self.captureSession addInput:input]; // 将输入流添加到会话
        [self.captureSession addOutput:output]; // 将媒体输出流添加到会话中
        //5.设置代理 在主线程里刷新
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [output setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]]; // 设置输出媒体数据类型为QRCode
        // 6.实例化预览图层
        self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
        [self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; // 设置预览图层填充方式
        [self.videoPreviewLayer setFrame:self.view.bounds];
        // 7. 将图层添加到预览view的图层上
        [self.view.layer addSublayer:self.videoPreviewLayer];
        // 8. 设置扫描范围
        output.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);
        
        self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        self.boxView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.2f, self.view.frame.size.height * 0.3f, self.view.frame.size.width - self.view.frame.size.width * 0.4f, self.view.frame.size.width - self.view.frame.size.width * 0.4f)];
        self.boxView.image = [UIImage imageNamed:@"scan_circle"];
        self.boxView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.boxView];
        
        // 扫描线
        self.scanLine = [[UIImageView alloc] initWithFrame:CGRectMake(self.boxView.frame.size.width * 0.1, 4.0f, self.boxView.frame.size.width * 0.8, 3.0f)];
        self.scanLine.image = [UIImage imageNamed:@"scan_line"];
        [self.boxView addSubview:self.scanLine];
        
        [self startRunning]; // 开始扫描
    }
    
    • 开始扫描
    #pragma mark - 开始扫描
    -(void)startRunning
    {
        if (self.captureSession) {
            self.isReading = YES;
            [self.captureSession startRunning];
            [self moveUpAndDownLine];
        }
    }
    
    -(void)stopRunning
    {
        [self.captureSession stopRunning];
        [self.scanLine removeFromSuperview];
        [self.videoPreviewLayer removeFromSuperlayer];
    }
    
    // 扫描线上下移动
    -(void)moveUpAndDownLine
    {
        [UIView animateWithDuration:3.0f animations:^{
            
            self.scanLine.frame = CGRectMake(self.boxView.frame.size.width * 0.1, self.boxView.frame.size.height - 8.0f, self.boxView.frame.size.width * 0.8, 3.0f);
            
        } completion:^(BOOL finished) {
            
            self.scanLine.frame =  CGRectMake(self.boxView.frame.size.width * 0.1f, 4.0f, self.boxView.frame.size.width * 0.8, 3.0f);
            [self moveUpAndDownLine];
            
        }];
    }
    
    • AVCaptureMetadataOutputObjectsDelegate 代理方法中获取扫描结果
    - (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
        //判断是否有数据
        if (!_isReading) {
            return;
        }
        if (metadataObjects.count > 0) {
            _isReading = NO;
            AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects[0];
            NSString *result = metadataObject.stringValue;
            NSLog(@"result - %@",result);
        }
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [self stopRunning];
    }
    
    -(void)dealloc
    {
        self.captureSession = nil;
    }
    

    相关文章

      网友评论

          本文标题:扫描二维码

          本文链接:https://www.haomeiwen.com/subject/fjrcgftx.html