博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios实现二维码扫描
阅读量:5292 次
发布时间:2019-06-14

本文共 3029 字,大约阅读时间需要 10 分钟。

1 包含

#import <AVFoundation/AVFoundation.h>

2 导入AVFoundation库文件

1 #import "ViewController.h" 2 #import 
3 @interface ViewController ()
4 - (IBAction)scan:(UIButton *)sender; 5 @property (strong,nonatomic)AVCaptureDevice *device; 6 @property (strong,nonatomic)AVCaptureDeviceInput *input; 7 @property (strong,nonatomic)AVCaptureMetadataOutput *output; 8 @property (strong,nonatomic)AVCaptureSession *session; 9 @property (strong,nonatomic)AVCaptureVideoPreviewLayer *preview;10 @end11 12 @implementation ViewController13 14 - (void)viewDidLoad {15 [super viewDidLoad];16 // Do any additional setup after loading the view, typically from a nib.17 18 }19 20 - (void)setupCamera21 {22 // Device23 _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];24 25 // Input26 _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];27 28 // Output29 _output = [[AVCaptureMetadataOutput alloc]init];30 [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];31 32 // Session33 _session = [[AVCaptureSession alloc]init];34 [_session setSessionPreset:AVCaptureSessionPresetHigh];35 if ([_session canAddInput:self.input])36 {37 [_session addInput:self.input];38 }39 40 if ([_session canAddOutput:self.output])41 {42 [_session addOutput:self.output];43 }44 45 // 条码类型 AVMetadataObjectTypeQRCode46 _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];47 48 // Preview49 _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];50 _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;51 _preview.frame =CGRectMake(20,110,280,280);52 [self.view.layer insertSublayer:self.preview atIndex:0];53 54 55 56 // Start57 [_session startRunning];58 }59 #pragma mark AVCaptureMetadataOutputObjectsDelegate60 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection61 {62 NSString *stringValue = [[NSString alloc] init];63 64 if (metadataObjects.count >0) {65 AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];66 stringValue = metadataObject.stringValue;67 }68 69 [_session stopRunning];70 NSLog(@"%@",stringValue);71 [self dismissViewControllerAnimated:YES completion:^{72 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil73 message:stringValue74 delegate:nil75 cancelButtonTitle:@"OK"76 otherButtonTitles:nil,nil];77 [alert show];78 }];79 }80 - (IBAction)scan:(UIButton *)sender {81 [self setupCamera];82 }

有待完善。。。。。

转载于:https://www.cnblogs.com/txios/p/4383543.html

你可能感兴趣的文章
Day21 Json & pickle 数据序列化
查看>>
内存结构。
查看>>
洛谷 [FJOI2014]最短路径树问题 解题报告
查看>>
欲望都市游戏设计 背景图层和UI图层的设计
查看>>
2-2 groovy基础知识-理论介绍
查看>>
Null Object Design Pattern (Python recipe)
查看>>
bootstrap学习笔记(6)
查看>>
leetcode : Valid Sudoku
查看>>
浅谈-Lambda
查看>>
storm 批处理(窗口)
查看>>
洛谷 P1052 过河
查看>>
Python3 从零单排28_线程队列&进程池&线程池
查看>>
java resources 红叉 Cannot change version of project facet Dynamic Web Module to 2.5
查看>>
阿里云 CentOS7.2 配置FTP+Node.js环境
查看>>
HttpWebRequest 发送简单参数
查看>>
Eclipse启动JVM机制
查看>>
一年的第几天
查看>>
leetcode 223: Rectangle Area
查看>>
Blender插件编写指南
查看>>
二次重建基本完成辣!
查看>>