iOS - 長按圖片識別圖中二維碼

來源:互聯網
上載者:User

標籤: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 - 長按圖片識別圖中二維碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.