国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios原生二維碼掃描的相關介紹

2020-02-19 15:57:20
字體:
來源:轉載
供稿:網友

做iOS的二維碼掃描,有兩個第三方庫可以選擇,ZBar和ZXing。今天要介紹的是iOS7.0后AVFoundation框架提供的原生二維碼掃描。

首先需要添加AVFoundation.framework框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以開始了。

一、做好準備工作,搭建UI

UI效果如圖

?

IBOutlet、IBAction如下:

@property (weak, nonatomic) IBOutlet UIView *viewPreview;@property (weak, nonatomic) IBOutlet UILabel *lblStatus;@property (weak, nonatomic) IBOutlet UIButton *startBtn;- (IBAction)startStopReading:(id)sender;

接下來就都是代碼的事情了

二、控制器ViewController.h

首先導入AVFoundation框架

#import 

然后控制器實現 AVCaptureMetadataOutputObjectsDelegate協議

@interface ViewController ()

整體property如下:

@property (strong, nonatomic) UIView *boxView;@property (nonatomic) BOOL isReading;@property (strong, nonatomic) CALayer *scanLayer;-(BOOL)startReading;-(void)stopReading;

//捕捉會話

@property (nonatomic, strong) AVCaptureSession *captureSession;

//展示layer

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

然后在ViewDidLoad方法中初始化

- (void)viewDidLoad {  [super viewDidLoad];  _captureSession = nil;   _isReading = NO;  }

接下來實現startReading方法(這可就是重點咯)

- (BOOL)startReading { NSError *error; //1.初始化捕捉設備(AVCaptureDevice),類型為AVMediaTypeVideo AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //2.用captureDevice創建輸入流 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; if (!input) {  NSLog(@"%@", [error localizedDescription]);  return NO; } //3.創建媒體數據輸出流 AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init]; //4.實例化捕捉會話 _captureSession = [[AVCaptureSession alloc] init]; //4.1.將輸入流添加到會話 [_captureSession addInput:input]; //4.2.將媒體輸出流添加到會話中 [_captureSession addOutput:captureMetadataOutput]; //5.創建串行隊列,并加媒體輸出流添加到隊列當中 dispatch_queue_t dispatchQueue; dispatchQueue = dispatch_queue_create("myQueue", NULL); //5.1.設置代理 [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue]; //5.2.設置輸出媒體數據類型為QRCode [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]]; //6.實例化預覽圖層 _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession]; //7.設置預覽圖層填充方式 [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; //8.設置圖層的frame [_videoPreviewLayer setFrame:_viewPreview.layer.bounds]; //9.將圖層添加到預覽view的圖層上 [_viewPreview.layer addSublayer:_videoPreviewLayer]; //10.設置掃描范圍 captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f); //10.1.掃描框 _boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.height - _viewPreview.bounds.size.height * 0.4f)]; _boxView.layer.borderColor = [UIColor greenColor].CGColor; _boxView.layer.borderWidth = 1.0f; [_viewPreview addSubview:_boxView]; //10.2.掃描線 _scanLayer = [[CALayer alloc] init]; _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1); _scanLayer.backgroundColor = [UIColor brownColor].CGColor; [_boxView.layer addSublayer:_scanLayer]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES]; [timer fire]; //10.開始掃描 [_captureSession startRunning]; return YES;}

實現AVCaptureMetadataOutputObjectsDelegate協議方法

#pragma mark - AVCaptureMetadataOutputObjectsDelegate- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ //判斷是否有數據 if (metadataObjects != nil && [metadataObjects count] > 0) {  AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];  //判斷回傳的數據類型  if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {   [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];   [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];   _isReading = NO;  } }}

實現計時器方法moveScanLayer:(NSTimer *)timer

- (void)moveScanLayer:(NSTimer *)timer{ CGRect frame = _scanLayer.frame; if (_boxView.frame.size.height 

實現開始和停止方法

- (IBAction)startStopReading:(id)sender {  if (!_isReading) {   if ([self startReading]) {    [_startBtn setTitle:@"Stop" forState:UIControlStateNormal];    [_lblStatus setText:@"Scanning for QR Code"];   }  }  else{   [self stopReading];   [_startBtn setTitle:@"Start!" forState:UIControlStateNormal];  }  _isReading = !_isReading;}-(void)stopReading{ [_captureSession stopRunning]; _captureSession = nil; [_scanLayer removeFromSuperlayer]; [_videoPreviewLayer removeFromSuperlayer];}

以上所述就是ios原生二維碼掃描的相關介紹,希望大家能夠喜歡,更多內容請繼續關注武林技術頻道網站!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清水河县| 普陀区| 宿松县| 漠河县| 喀喇沁旗| 务川| 定州市| 义马市| 清涧县| 二手房| 车险| 鄯善县| 疏勒县| 罗城| 丹巴县| 内乡县| 囊谦县| 南澳县| 通河县| 永胜县| 礼泉县| 亳州市| 黄大仙区| 博白县| 阿城市| 宁海县| 噶尔县| 呼伦贝尔市| 张家港市| 枝江市| 蕉岭县| 武汉市| 平山县| 新和县| 扶沟县| 宁强县| 类乌齐县| 威宁| 华宁县| 隆化县| 宁武县|