iOS AV Foundation 二维码扫描 03 为扫描到的二维码添加可视化效果
2015-03-21 18:08
381 查看
上一节,我们已经实现了二维码的扫描,这一节,我们利用元数据的bounds和corners实现一个可视化的扫描效果。
bounds定义了包含二维码图像的矩形,corners定义了二维码图像的实际坐标:
当摄像头和二维码图片完全对齐时,bounds和corners就是相同的。但是通常来说,几乎不可能让摄像头和二维码完全对齐。
打开ViewController.m,添加以下实例变量,用于存放所有检测到得二维码,以二维码的内容为索引。
查询Barcode对象字典,看是否有相同内容的Barcode已经存在。
如果没有,创建一个Barcode对象并将其加入到字典中。
存储二维码的元数据到新创建的Barcode对象中。
创建用于存储绘制二维码四个角路径的cornersPath。
使用CoreGraphics转换第一个角的坐标为CGPoint实例。
从第五步构造的角开始绘制路径。
循环遍历其它三个角,创建相应的路径。
绘制第四个点到第一个点路径后,关闭路径。
通过cornersPath创建UIBezierPath对象并将其存储到Barcode对象中。
通过bezierPathWithRect:方法创建边框块。
返回Barcode对象。
修改captureOutput:didOutputMetadataObjects:fromConnection方法:
创建用于遍历检测到的二维码的NSMutableSet。
处理类型为AVMetadataMachineReadableCodeObject的对象。
转换图像的bounds和corner坐标。将相对坐标转换为容器view的坐标。
处理二维码数据,将其加入到字典中。
移除预览view中的所有子层。
遍历所有检测到的二维码,为它们添加边界路径和角路径。这些layer有着不同的颜色,alpha值也被设置为0.5,这样我们可以透过叠加层看到原始二维码图片。
编译运行,效果如下:
下一节,我们将为程序添加语音合成功能,自动朗读二维码的内容。
转载请注明出处:/article/9358090.html
bounds定义了包含二维码图像的矩形,corners定义了二维码图像的实际坐标:
当摄像头和二维码图片完全对齐时,bounds和corners就是相同的。但是通常来说,几乎不可能让摄像头和二维码完全对齐。
打开ViewController.m,添加以下实例变量,用于存放所有检测到得二维码,以二维码的内容为索引。
NSMutableDictionary *_barcodes;在viewDidLoad方法中初始化这个字典:
_barcodes = [NSMutableDictionary new];定义一个Barcode类,用于存放已识别的二维码的元数据。
@interface Barcode : NSObject @property (nonatomic, strong) AVMetadataMachineReadableCodeObject *metadataObject; @property (nonatomic, strong) UIBezierPath *cornersPath; @property (nonatomic, strong) UIBezierPath *boundingBoxPath; @end @implementation Barcode @end添加processMetadataObject :
- (Barcode *)processMetadataObject:(AVMetadataMachineReadableCodeObject *)code { // 1 Barcode *barcode = _barcodes[code.stringValue]; // 2 if (!barcode) { barcode = [Barcode new]; _barcodes[code.stringValue] = barcode; } // 3 barcode.metadataObject = code; // Create the path joining code's corners // 4 CGMutablePathRef cornersPath = CGPathCreateMutable(); // 5 CGPoint point; CGPointMakeWithDictionaryRepresentation((CFDictionaryRef)code.corners[0], &point); // 6 CGPathMoveToPoint(cornersPath, nil, point.x, point.y); // 7 for (int i = 1; i < code.corners.count; i++) { CGPointMakeWithDictionaryRepresentation((CFDictionaryRef)code.corners[i], &point); CGPathAddLineToPoint(cornersPath, nil, point.x, point.y); } // 8 CGPathCloseSubpath(cornersPath); // 9 barcode.cornersPath =[UIBezierPath bezierPathWithCGPath:cornersPath]; CGPathRelease(cornersPath); // Create the path for the code's bounding box // 10 barcode.boundingBoxPath = [UIBezierPath bezierPathWithRect:code.bounds]; // 11 return barcode; }
查询Barcode对象字典,看是否有相同内容的Barcode已经存在。
如果没有,创建一个Barcode对象并将其加入到字典中。
存储二维码的元数据到新创建的Barcode对象中。
创建用于存储绘制二维码四个角路径的cornersPath。
使用CoreGraphics转换第一个角的坐标为CGPoint实例。
从第五步构造的角开始绘制路径。
循环遍历其它三个角,创建相应的路径。
绘制第四个点到第一个点路径后,关闭路径。
通过cornersPath创建UIBezierPath对象并将其存储到Barcode对象中。
通过bezierPathWithRect:方法创建边框块。
返回Barcode对象。
修改captureOutput:didOutputMetadataObjects:fromConnection方法:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { // 1 NSMutableSet *foundBarcodes = [NSMutableSet new]; [metadataObjects enumerateObjectsUsingBlock: ^(AVMetadataObject *obj, NSUInteger idx, BOOL *stop) { NSLog(@"Metadata: %@", obj); // 2 if ([obj isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) { // 3 AVMetadataMachineReadableCodeObject *code = (AVMetadataMachineReadableCodeObject*) [_previewLayer transformedMetadataObjectForMetadataObject:obj]; // 4 Barcode *barcode = [self processMetadataObject:code]; [foundBarcodes addObject:barcode]; } }]; dispatch_sync(dispatch_get_main_queue(), ^{ // Remove all old layers // 5 NSArray *allSublayers = [_previewView.layer.sublayers copy]; [allSublayers enumerateObjectsUsingBlock: ^(CALayer *layer, NSUInteger idx, BOOL *stop) { if (layer != _previewLayer) { [layer removeFromSuperlayer]; } }]; // Add new layers // 6 [foundBarcodes enumerateObjectsUsingBlock: ^(Barcode *barcode, BOOL *stop) { CAShapeLayer *boundingBoxLayer = [CAShapeLayer new]; boundingBoxLayer.path = barcode.boundingBoxPath.CGPath; boundingBoxLayer.lineWidth = 2.0f; boundingBoxLayer.strokeColor = [UIColor greenColor].CGColor; boundingBoxLayer.fillColor = [UIColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:0.5f].CGColor; [_previewView.layer addSublayer:boundingBoxLayer]; CAShapeLayer *cornersPathLayer = [CAShapeLayer new]; cornersPathLayer.path = barcode.cornersPath.CGPath; cornersPathLayer.lineWidth = 2.0f; cornersPathLayer.strokeColor = [UIColor blueColor].CGColor; cornersPathLayer.fillColor = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.5f].CGColor; [_previewView.layer addSublayer:cornersPathLayer]; }]; }); }
创建用于遍历检测到的二维码的NSMutableSet。
处理类型为AVMetadataMachineReadableCodeObject的对象。
转换图像的bounds和corner坐标。将相对坐标转换为容器view的坐标。
处理二维码数据,将其加入到字典中。
移除预览view中的所有子层。
遍历所有检测到的二维码,为它们添加边界路径和角路径。这些layer有着不同的颜色,alpha值也被设置为0.5,这样我们可以透过叠加层看到原始二维码图片。
编译运行,效果如下:
下一节,我们将为程序添加语音合成功能,自动朗读二维码的内容。
转载请注明出处:/article/9358090.html
相关文章推荐
- iOS 7 What’s New in AV Foundation之二维码扫描(下)
- 38.微信二维码扫描效果--设定扫描区域,周边是半透明--iOS原生二维码扫描
- iOS 7 What’s New in AV Foundation之二维码扫描(中)
- iOS AV Foundation 二维码扫描 02 扫码
- iOS 为CALayer添加可动画的属性(以二维码切换扫描区域为例)
- iOS 7 What’s New in AV Foundation之二维码扫描(上)
- iOS中使用ZBar扫描二维码自定义扫描界面
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android基于google Zxing实现各类二维码扫描效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- iOS 为视图添加抖动效果
- iOS 二维码生成 改变颜色 添加中心图
- IOS为文字和图片添加阴影效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- iOS开发-原生二维码的扫描和生成
- IOS 原生二维码、条形码扫描for IOS7 (八)
- iOS开发中给图片添加高斯模糊效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- iOS开发之 二维码生成/二维码扫描