zoukankan      html  css  js  c++  java
  • iOS7后使用AVCapture出现 unsupported type found. Use -availableMetadataObjectTypes错误的解决

    在集成扫描二维码功能时候,我使用的是系统在iOS7.0 之后才支持的扫描二维码功能类。刚开始创建代码是这么写的

     1 -(void)setUpCamera
     2 {
     3     self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     4     
     5     self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
     6     
     7     self.outPut = [[AVCaptureMetadataOutput alloc]init];
     8     self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];    
     9     [_outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    10     self.session = [[AVCaptureSession alloc]init];
    11     [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    12     if ([self.session canAddInput:self.input])
    13     {
    14         [self.session addInput:self.input];
    15         
    16     }
    17     
    18     if ([self.session canAddOutput:self.outPut])
    19     {
    20         [self.session addOutput:self.outPut];
    21     }
    22     
    23 
    24     _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    25     _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    26     _preview.frame =CGRectMake(20,110,280,280);
    27     [self.view.layer insertSublayer:self.preview atIndex:0];
    28 
    29     [self.session startRunning];
    30 }

    在其代理类里面

    -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        NSString *stringValue;
        
        if ([metadataObjects count] > 0 ) {
            AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
            stringValue = metadataObject.stringValue;
        }
        [self.session stopRunning];
        [self dismissViewControllerAnimated:YES completion:^{
            [timer invalidate];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"二维码相关内容" message:stringValue delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
            
        }];
    }

    在真机上测试,系统是iOS7.1.1

    运行时出现下面的错误:

    *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] – unsupported type found. Use -availableMetadataObjectTypes.’

    解决不了,上网上搜索也没有相关的解决方法,最后查找相关文档并且查看手册,还是不能够解决问题,请教一大神,查看系统帮助文档的 AVMetadataObjectTypes后改变了创建代码块-(void)setUpCamera如下;

    -(void)setUpCamera
    {
        self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
        
        self.outPut = [[AVCaptureMetadataOutput alloc]init];
    //    self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    //    self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
        
        [_outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        self.session = [[AVCaptureSession alloc]init];
        [self.session setSessionPreset:AVCaptureSessionPresetHigh];
        if ([self.session canAddInput:self.input])
        {
            [self.session addInput:self.input];
            
        }
        
        if ([self.session canAddOutput:self.outPut])
        {
            [self.session addOutput:self.outPut];
        }
        self.outPut.metadataObjectTypes = [NSArray arrayWithObject:AVMetadataObjectTypeQRCode];
    
        _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
        _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
        _preview.frame =CGRectMake(20,110,280,280);
        [self.view.layer insertSublayer:self.preview atIndex:0];
    
        [self.session startRunning];
    }

    这样问题就解决了

    大神解释官方文档的意思大致是:

    AVCaptureMetadataOutput类的对象(在本例里是self.outPut),self.outPut的属性metadataObjectTypes要在AVCaptureSession类对象(本例为self.session)

    [self.session addOutput:self.outPut]即添加了AVCaptureMetadataOutput的对象后,方可对其进行设置;

     

  • 相关阅读:
    蓝屏的钙,好喝的钙
    正则
    JavaScript 获取当前时间戳 (3种方式)
    react-native upoad imagepicker
    xmlhttprequest请求
    修改配置使fiddler可以查看https请求
    安装composer
    判定 android IOS
    看端口任务
    squid 缓存Internet 软件
  • 原文地址:https://www.cnblogs.com/zhaopengtao14/p/3805602.html
Copyright © 2011-2022 走看看