iOS 識別二維碼及描繪二維碼邊框,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