zoukankan      html  css  js  c++  java
  • IOS 二维码扫描

    复制代码
    //
    //  ViewController.m
    //  CX 二维码扫描
    //
    //  Created by ma c on 16/4/12.
    //  Copyright © 2016年 bjsxt. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //创建捕捉会话
        AVCaptureSession * session = [[AVCaptureSession alloc]init];
        //添加输入设备
        AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:@"AVMediaTypeVideo"];
        AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        [session addInput:input];
        //添加输出数据
        AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [session addOutput:output];
        //告诉元数据类型为二维码类型
        //注意该方法在add后 否则崩溃
        //测试需要真机稍有麻烦 就不截图了
        [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
        //添加扫描图层
        AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
        layer.frame = CGRectMake(10,20, self.view.frame.size.width, 400);
        [self.view.layer addSublayer:layer];
        //开始扫描
        [session startRunning];
        
        //下面的方法适当的时候操作
        //停止扫描
    //    [session stopRunning];
        //移除图层
    //    [layer removeFromSuperlayer];
    }
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
        //metadataObjects 为扫描的后的数据
        
        AVMetadataMachineReadableCodeObject * objc = [metadataObjects lastObject];
        //我们想要的结果
        NSLog(@"%@",objc.stringValue);
    }
    @end
    复制代码
  • 相关阅读:
    python查询MySQL写入Excel
    Spring BOOT的学习笔记
    后台管理系统好用的UI框架
    SSM学习笔记
    解决thinkPHP3.2.3使用Smarty模板后无法使用系统常量问题
    提交代码,SVN被锁定,提示:svn is already locked解决方案
    CentOS 7编译安装php7.0.7以及可能遇到的问题的解决方案
    thinkphp nginx配置
    php读取不到指定的php.ini配置
    phpmyadmin nginx设置
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045621.html
Copyright © 2011-2022 走看看