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


 

 長按圖片識別圖中二維碼:

  1 // 長按圖片識別二維碼 
  2 
  3     UILongPressGestureRecognizer *QrCodeTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(QrCodeClick:)];
  4 
  5     [self.view addGestureRecognizer:QrCodeTap];
  6 
  7  
  8 
  9 - (void)QrCodeClick:(UILongPressGestureRecognizer *)pressSender {
 10 
 11     
 12 
 13     if (pressSender.state != UIGestureRecognizerStateBegan) {
 14 
 15         return;//長按手勢只會響應一次
 16 
 17     }
 18 
 19     
 20 
 21 //    MJPhoto *photo = _photos[_currentPhotoIndex];
 22 
 23     //截圖 再讀取
 24 
 25  
 26 
 27     //第一個參數表示區域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數就是屏幕密度了,獲取當前屏幕分辨率[UIScreen mainScreen].scale
 28 
 29     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 2.2);
 30 
 31     
 32 
 33     CGContextRef context = UIGraphicsGetCurrentContext();
 34 
 35     
 36 
 37     [self.view.layer renderInContext:context];
 38 
 39     
 40 
 41     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 42 
 43     UIGraphicsEndImageContext();
 44 
 45     
 46 
 47     CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil];
 48 
 49     CIContext *ciContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 軟件渲染
 50 
 51     CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:ciContext options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二維碼識別
 52 
 53     
 54 
 55     NSArray *features = [detector featuresInImage:ciImage];
 56 
 57     
 58 
 59     if (features.count) {
 60 
 61         
 62 
 63         for (CIQRCodeFeature *feature in features) {
 64 
 65             NSLog(@"qrCodeUrl = %@",feature.messageString); // 打印二維碼中的信息
 66 
 67             qrCodeUrl = feature.messageString;
 68 
 69         }
 70 
 71         
 72 
 73         // 初始化彈框 第一個參數是設置距離底部的邊距
 74 
 75         alertview = [[RomAlertView alloc] initWithMainAlertViewBottomInset:0 Title:nil detailText:nil cancelTitle:nil otherTitles:[NSMutableArray arrayWithObjects:@"保存圖片",@"識別圖中二維碼",nil]];
 76 
 77         alertview.tag = 10002;
 78 
 79         // 設置彈框的樣式
 80 
 81         alertview.RomMode = RomAlertViewModeBottomTableView;
 82 
 83         // 設置彈框從什么位置進入 當然也可以設置什么位置退出
 84 
 85         [alertview setEnterMode:RomAlertEnterModeBottom];
 86 
 87         // 設置代理
 88 
 89         [alertview setDelegate:self];
 90 
 91         // 顯示 必須調用 和系統一樣
 92 
 93         [alertview show];
 94 
 95     } else {
 96 
 97         NSLog(@"圖片中沒有二維碼");
 98 
 99     }
100 
101  
102 
103 }
104 
105  
106 
107 #pragma mark -- RomAlertViewDelegate 彈框識別圖中二維碼
108 
109 - (void)alertview:(RomAlertView *)alertview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
110 
111 {
112 
113     if (alertview.tag == 10002) {
114 
115         if ([alertview.otherTitles[indexPath.row]  isEqualToString:@"保存圖片"]) {
116 
117             NSLog(@"保存圖片");
118 
119             [self saveButtonPressed];
120 
121         }else if ([alertview.otherTitles[indexPath.row] isEqualToString:@"識別圖中二維碼"]){
122 
123             NSLog(@"識別圖中二維碼");
124 
125  
126 
127             // 隱藏
128 
129             [alertview hide];
130 
131             [self leftBackButtonPressed];
132 
133             
134 
135             AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
136 
137             if([delegate.window.rootViewController isKindOfClass:[UITabBarController class]]){
138 
139                 UITabBarController *tabBarController = (UITabBarController *)delegate.window.rootViewController;
140 
141                 UINavigationController *navigationController = [tabBarController selectedViewController];
142 
143                 UIViewController *vc = navigationController.topViewController;
144 
145                 //對結果進行處理跳轉網頁
146 
147                 ADWebViewViewController *controller = [[ADWebViewViewController alloc] init];
148 
149                 controller.m_url = qrCodeUrl;
150 
151                 controller.hidesBottomBarWhenPushed = YES;
152 
153                 [vc.navigationController pushViewController:controller animated:YES];
154 
155             }
156 
157         }
158 
159     }
160 
161 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM