长按图片识别图中二维码

长按图片识别图中二维码

长按图片识别图中二维码:

 // 长按图片识别二维码

     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];

     //截图 再读取

     //第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,获取当前屏幕分辨率[UIScreen mainScreen].scale

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

     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: Title:nil detailText:nil cancelTitle:nil otherTitles:[NSMutableArray arrayWithObjects:@"保存图片",@"识别图中二维码",nil]];

         alertview.tag = ;

         // 设置弹框的样式

         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 == ) {

         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];

             }

         }

     }

 }
05-11 11:22