码迷,mamicode.com
首页 > 移动开发 > 详细

iOS - 长按图片识别图中二维码

时间:2018-04-10 13:27:41      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:tor   sbo   phi   state   index   click   graphic   oftype   def   

 

 

// 长按图片识别二维码 

    UILongPressGestureRecognizer *QrCodeTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(QrCodeClick:)];

    [self.view addGestureRecognizer:QrCodeTap];

 

- (void)QrCodeClick:(UILongPressGestureRecognizer *)pressSender {

    

    if (pressSender.state != UIGestureRecognizerStateBegan) {

        return;//长按手势只会响应一次

    }

    

//    MJPhoto *photo = _photos[_currentPhotoIndex];

    //截图 再读取

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    [self.view.layer renderInContext:context];

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil];

    CIContext *ciContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 软件渲染

    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:ciContext options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二维码识别

    

    NSArray *features = [detector featuresInImage:ciImage];

    

    if (features.count) {

        

        for (CIQRCodeFeature *feature in features) {

            NSLog(@"qrCodeUrl = %@",feature.messageString); // 打印二维码中的信息

            qrCodeUrl = feature.messageString;

        }

        

        // 初始化弹框 第一个参数是设置距离底部的边距

        alertview = [[RomAlertView alloc] initWithMainAlertViewBottomInset:0 Title:nil detailText:nil cancelTitle:nil otherTitles:[NSMutableArray arrayWithObjects:@"保存图片",@"识别图中二维码",nil]];

        alertview.tag = 10002;

        // 设置弹框的样式

        alertview.RomMode = RomAlertViewModeBottomTableView;

        // 设置弹框从什么位置进入 当然也可以设置什么位置退出

        [alertview setEnterMode:RomAlertEnterModeBottom];

        // 设置代理

        [alertview setDelegate:self];

        // 显示 必须调用 和系统一样

        [alertview show];

    } else {

        NSLog(@"图片中没有二维码");

    }

 

}

 

#pragma mark -- RomAlertViewDelegate 弹框识别图中二维码

- (void)alertview:(RomAlertView *)alertview didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (alertview.tag == 10002) {

        if ([alertview.otherTitles[indexPath.row]  isEqualToString:@"保存图片"]) {

            NSLog(@"保存图片");

            [self saveButtonPressed];

        }else if ([alertview.otherTitles[indexPath.row] isEqualToString:@"识别图中二维码"]){

            NSLog(@"识别图中二维码");

 

            // 隐藏

            [alertview hide];

            [self leftBackButtonPressed];

            

            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

            if([delegate.window.rootViewController isKindOfClass:[UITabBarController class]]){

                UITabBarController *tabBarController = (UITabBarController *)delegate.window.rootViewController;

                UINavigationController *navigationController = [tabBarController selectedViewController];

                UIViewController *vc = navigationController.topViewController;

                //对结果进行处理跳转网页

                ADWebViewViewController *controller = [[ADWebViewViewController alloc] init];

                controller.m_url = qrCodeUrl;

                controller.hidesBottomBarWhenPushed = YES;

                [vc.navigationController pushViewController:controller animated:YES];

            }

        }

    }

}

iOS - 长按图片识别图中二维码

标签:tor   sbo   phi   state   index   click   graphic   oftype   def   

原文地址:https://www.cnblogs.com/gongyuhonglou/p/8777847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!