#pragma mark - 开始扫描 - ( void )startScanning { // 1.建立输入设备(摄像头) AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil ]; // 2.建立输入方式(Metadata) AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()]; // 3.建立会话,将输入和输出联系起来 AVCaptureSession *session = [[AVCaptureSession alloc] init]; [session addInput:input]; [session addOutput:output]; [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; // 4.建立会话图层 AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; layer.frame = self .view.bounds; [ self .view.layer insertSublayer:layer atIndex:0]; self .layer = layer; // 5.开始扫描 [session startRunning]; // 6.设置扫描的区域 CGSize size = [ UIScreen mainScreen].bounds.size; CGFloat x = self .qrCodeView.frame.origin.y / size.height; CGFloat y = self .qrCodeView.frame.origin.x / size.width; CGFloat w = self .qrCodeView.frame.size.height / size.height; CGFloat h = self .qrCodeView.frame.size.width / size.width; output.rectOfInterest = CGRectMake (x, y, w, h); } - ( void )captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { // 0.移除以前的绘制 for ( CAShapeLayer *layer in self .layers) { [layer removeFromSuperlayer]; } // 1.获取扫描结果 NSMutableString *resultMStr = [ NSMutableString string]; for (AVMetadataMachineReadableCodeObject *result in metadataObjects) { // 1.1.获取扫描到的字符串 [resultMStr appendString:result.stringValue]; // 1.2.绘制扫描到内容的边框 [ self drawEdgeBorder:result]; } // 2.显示结果 NSString *string = [resultMStr isEqualToString: @"" ] ? @"请将二维码放入输入框中" : resultMStr; self .resultLabel.text = string; } - ( void )drawEdgeBorder:(AVMetadataMachineReadableCodeObject *)resultObjc { // 0.转化object resultObjc = (AVMetadataMachineReadableCodeObject *)[ self .layer transformedMetadataObjectForMetadataObject:resultObjc]; // 1.建立绘制的图层 CAShapeLayer *shapeLayer = [[ CAShapeLayer alloc] init]; // 2.设置图层的属性 shapeLayer.fillColor = [ UIColor clearColor]. CGColor ; shapeLayer.strokeColor = [ UIColor blueColor]. CGColor ; shapeLayer.lineWidth = 5; // 3.建立贝塞尔曲线 // 3.1.建立贝塞尔曲线 UIBezierPath *path = [[ UIBezierPath alloc] init]; // 3.2.将path移动到起始位置 int index = 0; for ( id dict in resultObjc.corners) { // 3.2.1.获取点 CGPoint point = CGPointZero ; CGPointMakeWithDictionaryRepresentation (( CFDictionaryRef )dict, &point); // NSLog(@"%@", NSStringFromCGPoint(point)); // 3.2.2.判断如何使用该点 if (index == 0) { [path moveToPoint:point]; } else { [path addLineToPoint:point]; } // 3.2.3.下标值自动加1 index++; } // 3.3.关闭路径 [path closePath]; // 4.画出路径 shapeLayer.path = path. CGPath ; // 5.将layer添加到图册中 [ self .view.layer addSublayer:shapeLayer]; // 6.添加到数组中 [_layers addObject:shapeLayer]; } - ( NSMutableArray *)layers { if (_layers == nil ) { _layers = [ NSMutableArray array]; } return _layers; } |