功能类:二维码扫描(相机扫描,相册扫描)
2016-07-18 22:15
246 查看
#import <UIKit/UIKit.h> @interface SaoMiaoViewController : UIViewController @end #import "SaoMiaoViewController.h" #import <AVFoundation/AVFoundation.h> #define IS_VAILABLE_IOS8 ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) @interface SaoMiaoViewController ()<AVCaptureMetadataOutputObjectsDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate> @property (strong, nonatomic) AVCaptureSession *captureSession; typedef void (^XMSweepBlock)(NSString *result); @property(nonatomic,copy)XMSweepBlock didRecoiveBlock; -(void)setDidRecoiveBlock:(XMSweepBlock)didRecoiveBlock; @end @implementation SaoMiaoViewController - (void)viewDidLoad { [super viewDidLoad]; // UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 15, 20)]; // [btn setTitle:@"相册" forState: UIControlStateNormal]; // [btn addTarget:self action:@selector(clickToXiangCe:) forControlEvents:UIControlEventTouchUpOutside]; // UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn]; // self.navigationItem.rightBarButtonItem = rightItem; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (error) { NSLog(@"启动镜头失败"); return; }; if ([self.captureSession canAddInput:deviceInput]) { [self.captureSession addInput:deviceInput]; } AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc]init]; if ([self.captureSession canAddOutput:metadataOutput]) { [self.captureSession addOutput:metadataOutput]; } [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // NSArray *type = metadataOutput.availableMetadataObjectTypes; // NSLog(@"%@",type); metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; [self.view.layer addSublayer:layer]; layer.frame = self.view.bounds; // 解析范围, (输出), 有效的扫描范围 CGFloat wh = 300; // 有效范围 300 * 300, 居中 // 参数值是 0.0 ~ 1.0, Rect:(y,x,height,width) CGFloat width = wh / self.view.bounds.size.width; CGFloat height = wh / self.view.bounds.size.height; CGFloat x = (self.view.bounds.size.width - wh) * 0.5 / self.view.bounds.size.width; CGFloat y = (self.view.bounds.size.height - wh) * 0.5 / self.view.bounds.size.height; // Rect:(y,x,height,width) metadataOutput.rectOfInterest = CGRectMake(y, x, height, width); [self.captureSession startRunning]; [self addOtherLay:CGRectMake((self.view.bounds.size.width - wh) * 0.5, (self.view.bounds.size.height - wh) * 0.5, wh, wh)]; } - (IBAction)clicktoxiangce:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc]init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; } //选中单张照片 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { [picker dismissViewControllerAnimated:YES completion:nil]; __block UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; if (!image) { image = [info objectForKey:UIImagePickerControllerOriginalImage]; } //系统自带的识别方法 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }]; CGImageRef ref = (CGImageRef)image.CGImage; CIImage *cii = [CIImage imageWithCGImage:ref]; NSArray *feacture = [detector featuresInImage:cii]; if (feacture.count >= 1) { CIQRCodeFeature *feature = [feacture objectAtIndex:0]; NSString *scanResult = feature.messageString; if (_didRecoiveBlock) { self.didRecoiveBlock(scanResult); // [self selfRemoveFromSuperview]; } else { [self.captureSession stopRunning]; NSURL *url = [NSURL URLWithString:scanResult]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame]; [self.view addSubview:webView]; [webView loadRequest:request]; } } } #pragma mark - AVCaptureMetadataOutputObjectsDelegate -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.lastObject; NSString *resultStr = metadataObject.stringValue; NSLog(@"%@",resultStr); [self.captureSession stopRunning]; NSURL *url = [NSURL URLWithString:resultStr]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame]; [self.view addSubview:webView]; [webView loadRequest:request]; } - (void)addOtherLay:(CGRect)rect { // Rect为保留的矩形frame值 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; CGFloat leftWidth = (screenWidth - rect.size.width) / 2; CAShapeLayer* layerTop = [[CAShapeLayer alloc] init]; layerTop.fillColor = [UIColor blackColor].CGColor; layerTop.opacity = 0.5; layerTop.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, screenWidth, rect.origin.y)].CGPath; [self.view.layer addSublayer:layerTop]; CAShapeLayer* layerLeft = [[CAShapeLayer alloc] init]; layerLeft.fillColor = [UIColor blackColor].CGColor; layerLeft.opacity = 0.5; layerLeft.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, rect.origin.y, leftWidth, rect.size.height)].CGPath; [self.view.layer addSublayer:layerLeft]; CAShapeLayer* layerRight = [[CAShapeLayer alloc] init]; layerRight.fillColor = [UIColor blackColor].CGColor; layerRight.opacity = 0.5; layerRight.path = [UIBezierPath bezierPathWithRect:CGRectMake(screenWidth - leftWidth, rect.origin.y, rect.size.width, rect.size.height)].CGPath; [self.view.layer addSublayer:layerRight]; CAShapeLayer* layerBottom = [[CAShapeLayer alloc] init]; layerBottom.fillColor = [UIColor blackColor].CGColor; layerBottom.opacity = 0.5; layerBottom.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, CGRectGetMaxY(rect), screenWidth, screenHeight - CGRectGetMaxY(rect))].CGPath; [self.view.layer addSublayer:layerBottom]; } - (AVCaptureSession *)captureSession { if (_captureSession == nil) { _captureSession = [[AVCaptureSession alloc]init]; } return _captureSession; } @end
相关文章推荐
- ServletContext 总结
- 33 把数组排成最小数
- python核心编程学习笔记-2016-07-1 4000 8-01-print
- POJ 1961 (KMP)
- 佛家辞典、基本概念
- 通俗易懂的理解卷积
- 校验身份证号码的有效性【JS】
- 【功能-java-函数】生成随机数的函数(返回字节)
- javamail异常发送邮件出现Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/ut
- UVa725-Division
- java+mysql实现保存图片到数据库,以及读取数据库存储的图片
- Linux第五周
- 洛谷P1160 队列安排
- Redis简介,安装和配置,停止,卸载(图解方式)
- poj1458最长公共子序列问题
- Android开发——利用productFlavors实现一套代码打包多版本
- 我理解的语法糖
- (转)大型网站的系统架构
- 一个实用的Eclipse快捷键
- HDU 4798 - Skycity