二维码的读取
1.需要导入AVFoundation框架
2.步骤:
// 1). 初始化捕捉设备:AVCaptureDevice;
// 2)准备捕捉输入流:AVCaptureDeviceInput;
// 3)设置元数据输出:AVCaptureMetadataOutput
// (设置输出代理:setMetadataObjectsDelegate,设置扫描的范围:rectOfInterest);
// 4)显示捕捉会话层:AVCaptureSession
// (输入流添加到会话层,输出流添加到会话层,必须设置元数据的输出类型:metadataObjectTypes);
// 5)捕捉对象预览图层:(将预览图层插入到当前视图的layer层上 : insertSublayer: atIndex: ;
// 6)启动拍摄会话:startRunning
3.code:步骤:
1).实例化拍摄设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
2).准备输入设备
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
3).设置元输出数据
//3).1实例化拍摄元输出数据
AVCaptureMetadataOutput *dataOutput = [[AVCaptureMetadataOutput alloc]init];
//3).2设置元代理
[dataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
4).设置扫描范围为扫描框内,注意,设置范围的x,y轴是于iOS的x,y轴相反的
CGRect rect = KScreenBounds;
// containerView是扫描框控件
CGRect containerRect = _containerView.frame;
// dataOutput.rectOfInterest = CGRectMake(0, 0, 1, 1);
dataOutput.rectOfInterest = CGRectMake(containerRect.origin.y / rect.size.height , containerRect.origin.x / rect.size.width, containerRect.size.height / rect.size.height, containerRect.size.width / rect.size.width);
5).添加拍摄会话层
AVCaptureSession *session = [[AVCaptureSession alloc]init];
//5).1 输入添加到会话层 AVCaptureDeviceInput extend AVCaptureInput
[session addInput:input];
//5).2 输出也添加到会话层 AVCaptureMetadataOutput extend AVCaptureOutput
[session addOutput:dataOutput];
// 5).3 设置输出数据类型,需要将元数据输出添加到会话后,才能指定元数据类型,否则会报错
[dataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
self.session = session;
6).捕捉对象预览图层
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.frame = self.view.bounds;
self.previewLayer = previewLayer;
//6).1 将预览图层插入到当前视图的layer层上
[self.view.layer insertSublayer:previewLayer atIndex:0];
//启动拍摄会话
[session startRunning];
4.AVCaptureMetadataOutputObjectsDelegate输出的结果
//调用代理方法:会不停的调用
// 1. 如果扫描完成,停止会话
[self.session stopRunning];
// 2. 删除预览图层
// [self.previewLayer removeFromSuperlayer];
NSLog(@"%@", metadataObjects);
// 3. 设置界面显示扫描结果
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
// 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展!
NSLog(@"%@", obj.stringValue);
content = obj.stringValue;
}
5.选择相册中有效的二维码:通过代理(UIImagePickerControllerDelegate)方法获得UImage
UIImage *imge = [info valueForKey:UIImagePickerControllerEditedImage];
NSLog(@"%@",imge);
//取出选中的图片
UIImage *pickImage = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(pickImage);
CIImage *ciImage = [CIImage imageWithData:imageData];
//创建探测器
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *feature = [detector featuresInImage:ciImage];
if (feature.count == 0) {
content = @"http://www.jianshu.com/users/319658cc7821/latest_articles/";
}
//取出探测到的数据
for (CIQRCodeFeature *result in feature) {
content = result.messageString;
}
//进行处理(音效、页面跳转,返回等)
// [self playBeep];
[_pickerCtr dismissViewControllerAnimated:YES completion:nil];