美文网首页
iOS 扫描二维码

iOS 扫描二维码

作者: feng1991 | 来源:发表于2017-02-16 16:58 被阅读0次

    没有用三方,因为要调用摄像头,所以首先就是导入#import <AVFoundation/AVFoundation.h>框架。下面是要用到的几个最主要的:
    // 摄像头
    @property (nonatomic, strong) AVCaptureDevice *device;
    // 设备输入
    @property (nonatomic, strong) AVCaptureDeviceInput *input;
    // 设备输出
    @property (nonatomic, strong) AVCaptureMetadataOutput *output;
    // 创建会话对象
    @property (nonatomic, strong) AVCaptureSession *session;
    // 呈现数据的图层
    @property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
    然后是初始化这些属性,我写的是一个简易的demo,所以全部写在viewDidLoad里面的,因为要调用AVCaptureMetadataOutput的一个代理方法所以要遵守<AVCaptureMetadataOutputObjectsDelegate>代理协议。

    // 初始化
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
        
        _output = [[AVCaptureMetadataOutput alloc] init];
        // 设置好扫描成功回调代理以及回调线程<AVCaptureMetadataOutputObjectsDelegate>
        [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        
        _session = [[AVCaptureSession alloc] init];
        [_session setSessionPreset:AVCaptureSessionPresetHigh];// 高质量采集
        
        // 加入输入输出对象
        if ([_session canAddInput:self.input]) {
            [_session addInput:self.input];
        }
        if ([_session canAddOutput:self.output]) {
            [_session addOutput:self.output];
        }
        
        // 设置扫描的条码类型
        _output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
        
        //增加条形码扫描
        //    _output.metadataObjectTypes = @[AVMetadataObjectTypeEAN13Code,
        //                                    AVMetadataObjectTypeEAN8Code,
        //                                    AVMetadataObjectTypeCode128Code,
        //                                    AVMetadataObjectTypeQRCode];
        
        _previewLayer =[AVCaptureVideoPreviewLayer layerWithSession:_session];
        _previewLayer.videoGravity =AVLayerVideoGravityResize;
        _previewLayer.frame =self.view.layer.bounds;
        [self.view.layer insertSublayer:_previewLayer atIndex:0];
        
        
        
        // 创建一个view展示
        UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
        view.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
        view.backgroundColor = [UIColor clearColor];
        [self.view addSubview:view];
        //修正扫描区域
        CGFloat screenHeight = self.view.frame.size.height;
        CGFloat screenWidth = self.view.frame.size.width;
        CGRect cropRect = CGRectMake((screenWidth - 200) / 2,(screenHeight - 200) / 2,200,200);
        
        [_output setRectOfInterest:CGRectMake(cropRect.origin.y / screenHeight,
                                              cropRect.origin.x / screenWidth,
                                              cropRect.size.height / screenHeight,
                                              cropRect.size.width / screenWidth)];
        // 开启会话
        [_session startRunning];
    

    下面就是获得扫描的结果,上面提到的遵守的<AVCaptureMetadataOutputObjectsDelegate>协议里面有一个代理方法,能够得到扫描的结果

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        NSString *stringValue;
        if ([metadataObjects count] >0)
        {
            //停止扫描
    //        [_session stopRunning];
            AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
            stringValue = metadataObject.stringValue;
        }
    

    stringValue就是扫描后得到的一个网址,是一个网址字符串。拿到字符串后通过自己的需求就可以将链接打开。我是用的系统的#import <SafariServices/SafariServices.h>创建的一个控制器跳转过去的。如果你想跳转后返回扫描界面还能扫描,那个停止扫描就不要,不是你返回去就不能再次扫描。

    相关文章

      网友评论

          本文标题:iOS 扫描二维码

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