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 }

 

转载于:https://www.cnblogs.com/gongyuhonglou/p/8777847.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS开发中获取图片二维码的定位,可以使用CoreImage框架。具体步骤如下: 1. 使用CIImage加载图片: ``` CIImage *image = [CIImage imageWithCGImage:image.CGImage]; ``` 2. 创建CIDetector并设置识别类型: ``` CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; ``` 3. 识别二维码: ``` NSArray *features = [detector featuresInImage:image]; ``` 4. 遍历识别结果,获取二维码定位: ``` for (CIQRCodeFeature *feature in features) { NSArray *corners = feature.corners; // 获取定位四个角的坐标 // 在图像上绘制定位 UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, corners[0].x, corners[0].y); CGContextAddLineToPoint(context, corners[1].x, corners[1].y); CGContextAddLineToPoint(context, corners[2].x, corners[2].y); CGContextAddLineToPoint(context, corners[3].x, corners[3].y); CGContextAddLineToPoint(context, corners[0].x, corners[0].y); CGContextStrokePath(context); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } ``` 这样就可以在原图上绘制出二维码的定位了。需要注意的是,CIDetector只能识别二维码,如果要识别其他类型的码,需要设置不同的detector类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值