iOS 识别二维码及描绘二维码边框
用OpenCV可以实现,识别二维码,并将其边框描绘出来,
如何换成苹果AVFoundation来扫描,如何描绘出二维码的边框呢?
我们知道,扫描结果AVCaptureMetadataOutputObjectsDelegate是返回了数组,而数组里面是一个个的AVMetadataMachineReadableCodeObject,而AVMetadataMachineReadableCodeObject中有个corners数组,记录二维码的坐标,文档给出的解析如下:
/*!
@property corners
@abstract
The points defining the (X,Y) locations of the corners of the machine-readable code.
@discussion
The value of this property is an NSArray of NSDictionaries, each of which has been created from a CGPoint using CGPointCreateDictionaryRepresentation(), representing the coordinates of the corners of the object with respect to the image in which it resides. If the metadata originates from video, the points may be expressed as scalar values from 0. - 1. The points in the corners differ from the bounds rectangle in that bounds is axis-aligned to orientation of the captured image, and the values of the corners reside within the bounds rectangle. The points are arranged in counter-clockwise order (clockwise if the code or image is mirrored), starting with the top-left of the code in its canonical orientation.
*/
查阅了官方文档和相关资料,我们很容易联想到,通过corners来获取二维码的坐标,大小形状。从而进行描绘。
描绘边框主要代码如下:
/*** 专门用于保存描边的图层 ***/ @property (nonatomic,strong) CALayer *containerLayer; - (void)drawLine:(AVMetadataMachineReadableCodeObject *)objc { NSArray *array = objc.corners; // 1.创建形状图层, 用于保存绘制的矩形 CAShapeLayer *layer = [[CAShapeLayer alloc] init]; // 设置线宽 layer.lineWidth = 2; // 设置描边颜色 layer.strokeColor = [UIColor greenColor].CGColor; layer.fillColor = [UIColor clearColor].CGColor; // 2.创建UIBezierPath, 绘制矩形 UIBezierPath *path = [[UIBezierPath alloc] init]; CGPoint point = CGPointZero; int index = 0; CFDictionaryRef dict = (__bridge CFDictionaryRef)(array[index++]); // 把点转换为不可变字典 // 把字典转换为点,存在point里,成功返回true 其他false CGPointMakeWithDictionaryRepresentation(dict, &point); // 设置起点 [path moveToPoint:point]; NSLog(@"X:%f -- Y:%f",point.x,point.y); // 2.2连接其它线段 for (int i = 1; i