iOS 扫描相册内二维码.

作者: KumLight | 来源:发表于2016-11-13 23:15 被阅读320次

    在各类App 当中 二维码的应用已经很普遍了 , 从分享扫描链接 到 支付通道 .

    二维码付款

    生成/识别 二维码的 相关文章和第三方控件都很多 , 在这里就不多说明了 , 今天分享一个小的功能点 , 就是扫描相册二维码的方法 .
    推荐文章 : 原生二维码的扫描和生成 , 二维码 生成/识别 Demo

    //设置NavigationItem
    - (void)setNavigationItem{
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
        self.navigationItem.title = @"二维码/条形码";
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]
                                     initWithTitle:@"相册"
                                     style:UIBarButtonItemStylePlain
                                     target:self
                                     action:@selector(openPhoto)];
        self.navigationItem.rightBarButtonItem = rightBtn;
    }
    
    //调用相册
    - (void)openPhoto {
        //1.判断相册是否可以打开
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            return;
        }
        //2.创建图片选择控制器
        UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        ipc.delegate = self;
        //选中之后大图编辑模式
        ipc.allowsEditing = YES;
        [self presentViewController:ipc animated:YES completion:nil];
    }
    

    UIImagePickerController的协议方法

    //相册获取的照片进行处理
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
        // 1.取出选中的图片
        UIImage *pickImage = info[UIImagePickerControllerOriginalImage];
        //转成Data格式
        NSData *imageData = UIImagePNGRepresentation(pickImage);
        
        CIImage *ciImage = [CIImage imageWithData:imageData];
        
        //2.从选中的图片中读取二维码数据
        //2.1创建一个探测器
        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
        
        // 2.2利用探测器探测数据
        NSArray *feature = [detector featuresInImage:ciImage];
        
        // 2.3取出探测到的数据
        for (CIQRCodeFeature *result in feature) {
            NSString *urlStr = result.messageString;
            
            if([self isPureNumandCharacters:urlStr]){
                //跳转网页
                [SystemFunctions showInSafariWithURLMessage:urlStr Success:^(NSString *token) {
                    
                } Failure:^(NSError *error) {
                    //            [self showAlertWithTitle:@"该信息无法跳转,详细信息为:" Message:urlStr OptionalAction:@[@"确定"]];
                }];
            
                [picker dismissViewControllerAnimated:YES completion:nil];
                
            }else{
                [UIAlertController createAlertWithTitle:@"" Message:@"二维码信息有误,请重新扫描." currentController:picker OKAction:^(UIAlertAction *action) {
                    
                    [picker dismissViewControllerAnimated:YES completion:nil];
                    
                }];
            }
            //二维码信息回传
            self.block(urlStr);
        }
        if (feature.count == 0) {
    
            [UIAlertController createAlertWithTitle:@"扫描结果" Message:@"没有扫描到有效二维码" currentController:picker];
    
        }
    }
    

    注意 :

    • 代码中的UIAlertController 是我自己封装的类目 . 下载地址

    相关文章

      网友评论

        本文标题:iOS 扫描相册内二维码.

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