您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在ios中利用AVFoundation讀取二維碼,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
1 二維碼的讀取
讀取二維碼也就是通過(guò)掃描二維碼圖像以獲取其所包含的數(shù)據(jù)信息。需要知道的是,任何條形碼(包括二維碼)的掃描都是基于視頻采集(video capture),因此需要使用AVFoundation框架。
掃描二維碼的過(guò)程即從攝像頭捕獲二維碼圖像(input)到解析出字符串內(nèi)容(output)的過(guò)程,主要是通過(guò)AVCaptureSession對(duì)象來(lái)實(shí)現(xiàn)的。該對(duì)象用于協(xié)調(diào)從輸入到輸出的數(shù)據(jù)流,在執(zhí)行過(guò)程中,需要先將輸入和輸出添加到AVCaptureSession對(duì)象中,然后通過(guò)發(fā)送startRunning或stopRunning消息來(lái)啟動(dòng)或停止數(shù)據(jù)流,最后通過(guò)AVCaptureVideoPreviewLayer對(duì)象將捕獲的視頻顯示在屏幕上。在這里,輸入對(duì)象通常是AVCaptureDeviceInput對(duì)象,主要是通過(guò)AVCaptureDevice的實(shí)例來(lái)獲得,而輸出對(duì)象通常是AVCaptureMetaDataOutput對(duì)象,它是讀取二維碼的核心部分,與AVCaptureMetadataOutputObjectsDelegate協(xié)議結(jié)合使用,可以捕獲在輸入設(shè)備中找到的任何元數(shù)據(jù),并將其轉(zhuǎn)換為可讀的格式。下面是具體步驟:
1、導(dǎo)入AVFoundation框架。
#import <AVFoundation/AVFoundation.h>
2、創(chuàng)建一個(gè)AVCaptureSession對(duì)象。
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
3、為AVCaptureSession對(duì)象添加輸入和輸出。
// add input NSError *error; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; [captureSession addInput:deviceInput]; // add output AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [captureSession addOutput:metadataOutput];
4、配置AVCaptureMetaDataOutput對(duì)象,主要是設(shè)置代理和要處理的元數(shù)據(jù)對(duì)象類型。
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL); [metadataOutput setMetadataObjectsDelegate:self queue:queue]; [metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
需要注意的是,一定要在輸出對(duì)象被添加到captureSession之后才能設(shè)置要處理的元數(shù)據(jù)類型,否則會(huì)出現(xiàn)下面的錯(cuò)誤:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes'
5、創(chuàng)建并設(shè)置AVCaptureVideoPreviewLayer對(duì)象來(lái)顯示捕獲到的視頻。
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession]; [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; [previewLayer setFrame:self.view.bounds]; [self.view.layer addSublayer:previewLayer];
6、給AVCaptureSession對(duì)象發(fā)送startRunning消息以啟動(dòng)視頻捕獲。
[captureSession startRunning];
7、實(shí)現(xiàn)AVCaptureMetadataOutputObjectsDelegate的captureOutput:didOutputMetadataObjects:fromConnection:方法來(lái)處理捕獲到的元數(shù)據(jù),并將其讀取出來(lái)。
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects != nil && metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject; if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode]) { NSString *message = [metadataObject stringValue]; [self.label performSelectorOnMainThread:@selector(setText:) withObject:message waitUntilDone:NO]; } } }
需要提醒的是,由于AVCaptureMetaDataOutput對(duì)象代理的設(shè)置,該代理方法會(huì)在setMetadataObjectsDelegate:queue:指定的隊(duì)列上調(diào)用,如果需要更新用戶界面,則必須在主線程中進(jìn)行。
2 應(yīng)用示例
下面,我們就做一個(gè)如下圖所示的二維碼閱讀器:
其中主要實(shí)現(xiàn)的功能有:
通過(guò)攝像頭實(shí)時(shí)掃描并讀取二維碼。
解析從相冊(cè)中選擇的二維碼圖片。
由于二維碼的掃描是基于實(shí)時(shí)的視頻捕獲,因此相關(guān)的操作無(wú)法在模擬器上進(jìn)行測(cè)試,也不能在沒(méi)有相機(jī)的設(shè)備上進(jìn)行測(cè)試。如果想要查看該應(yīng)用,需要連接自己的iPhone設(shè)備來(lái)運(yùn)行。
2.1 創(chuàng)建項(xiàng)目
打開(kāi)Xcode,創(chuàng)建一個(gè)新的項(xiàng)目(File\New\Project...),選擇iOS一欄下的Application中的Single View Application模版,然后點(diǎn)擊Next,填寫項(xiàng)目選項(xiàng)。在Product Name中填寫QRCodeReaderDemo,選擇Objective-C語(yǔ)言,點(diǎn)擊Next,選擇文件位置,并單擊Create創(chuàng)建項(xiàng)目。
2.2 構(gòu)建界面
打開(kāi)Main.storyboard文件,在當(dāng)前控制器中嵌入導(dǎo)航控制器,并添加標(biāo)題QR Code Reader:
在視圖控制器中添加ToolBar、Flexible Space Bar Button Item、Bar Button Item、View,布局如下:
其中,各元素及作用:
ToolBar:添加在控制器視圖的最底部,其Bar Item標(biāo)題為Start,具有雙重作用,用于啟動(dòng)和停止掃描。
Flexible Space Bar Button Item:分別添加在Start的左右兩側(cè),用于固定Start 的位置使其居中顯示。
Bar Button Item:添加在導(dǎo)航欄的右側(cè),標(biāo)題為Album,用于從相冊(cè)選擇二維碼圖片進(jìn)行解析。
View:添加在控制器視圖的中間,用于稍后設(shè)置掃描框。在這里使用自動(dòng)布局固定寬高均為260,并且水平和垂直方向都是居中。
創(chuàng)建一個(gè)名為ScanView的新文件(File\New\File…),它是UIView的子類。然后選中視圖控制器中間添加的View,將該視圖的類名更改為ScanView:
打開(kāi)輔助編輯器,將storyboard中的元素連接到代碼中:
注意,需要在ViewController.m文件中導(dǎo)入ScanView.h文件。
2.3 添加代碼
2.3.1 掃描二維碼
首先在ViewController.h文件中導(dǎo)入AVFoundation框架:
#import <AVFoundation/AVFoundation.h>
切換到ViewController.m文件,添加AVCaptureMetadataOutputObjectsDelegate協(xié)議,并在接口部分添加下面的屬性:
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> // properties @property (assign, nonatomic) BOOL isReading; @property (strong, nonatomic) AVCaptureSession *captureSession; @property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayer;
在viewDidLoad方法中添加下面代碼:
- (void)viewDidLoad { [super viewDidLoad]; self.isReading = NO; self.captureSession = nil; }
然后在實(shí)現(xiàn)部分添加startScanning方法和stopScanning方法及相關(guān)代碼:
- (void)startScanning { self.captureSession = [[AVCaptureSession alloc] init]; // add input NSError *error; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error]; if (!deviceInput) { NSLog(@"%@", [error localizedDescription]); } [self.captureSession addInput:deviceInput]; // add output AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [self.captureSession addOutput:metadataOutput]; // configure output dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL); [metadataOutput setMetadataObjectsDelegate:self queue:queue]; [metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; // configure previewLayer self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; [self.previewLayer setFrame:self.view.bounds]; [self.view.layer addSublayer:self.previewLayer]; // start scanning [self.captureSession startRunning]; } - (void)stopScanning { [self.captureSession stopRunning]; self.captureSession = nil; [self.previewLayer removeFromSuperlayer]; }
找到startStopAction:并在該方法中調(diào)用上面的方法:
- (IBAction)startStopAction:(id)sender { if (!self.isReading) { [self startScanning]; [self.view bringSubviewToFront:self.toolBar]; [self.startStopButton setTitle:@"Stop"]; } else { [self stopScanning]; [self.startStopButton setTitle:@"Start"]; } self.isReading = !self.isReading; }
至此,二維碼掃描相關(guān)的代碼已經(jīng)完成,如果想要它能夠正常運(yùn)行的話,還需要在Info.plist文件中添加NSCameraUsageDescription鍵及相應(yīng)描述以訪問(wèn)相機(jī):
需要注意的是,現(xiàn)在只能掃描二維碼但是還不能讀取到二維碼中的內(nèi)容,不過(guò)我們可以連接設(shè)備,運(yùn)行試下:
2.3.2 讀取二維碼
讀取二維碼需要實(shí)現(xiàn)AVCaptureMetadataOutputObjectsDelegate協(xié)議的captureOutput:didOutputMetadataObjects:fromConnection:方法:
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects != nil && metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject; if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode]) { NSString *message = [metadataObject stringValue]; [self performSelectorOnMainThread:@selector(displayMessage:) withObject:message waitUntilDone:NO]; [self performSelectorOnMainThread:@selector(stopScanning) withObject:nil waitUntilDone:NO]; [self.startStopButton performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start" waitUntilDone:NO]; self.isReading = NO; } } } - (void)displayMessage:(NSString *)message { UIViewController *vc = [[UIViewController alloc] init]; UITextView *textView = [[UITextView alloc] initWithFrame:vc.view.bounds]; [textView setText:message]; [textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]]; textView.editable = NO; [vc.view addSubview:textView]; [self.navigationController showViewController:vc sender:nil]; }
在這里我們將掃碼結(jié)果顯示在一個(gè)新的視圖中,如果你運(yùn)行程序的話應(yīng)該可以看到掃描的二維碼內(nèi)容了。
另外,為了使我們的應(yīng)用更逼真,可以在掃描到二維碼信息時(shí)讓它播放聲音。這首先需要在項(xiàng)目中添加一個(gè)音頻文件:
然后在接口部分添加一個(gè)AVAudioPlayer對(duì)象的屬性:
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
在實(shí)現(xiàn)部分添加loadSound方法及代碼,并在viewDidLoad中調(diào)用該方法:
- (void)loadSound { NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"]; NSURL *soundURL = [NSURL URLWithString:soundFilePath]; NSError *error; self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; if (error) { NSLog(@"Could not play sound file."); NSLog(@"%@", [error localizedDescription]); } else { [self.audioPlayer prepareToPlay]; } } - (void)viewDidLoad { ... [self loadSound]; }
最后,在captureOutput:didOutputMetadataObjects:fromConnection:方法中添加下面的代碼來(lái)播放聲音:
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects != nil && metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject; if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode]) { ... self.isReading = NO; // play sound if (self.audioPlayer) { [self.audioPlayer play]; } } }
2.3.3 設(shè)置掃描框
目前點(diǎn)擊Start按鈕,整個(gè)視圖范圍都可以掃描二維碼?,F(xiàn)在,我們需要設(shè)置一個(gè)掃描框,以限制只有掃描框區(qū)域內(nèi)的二維碼被讀取。在這里,將掃描區(qū)域設(shè)置為storyboard中添加的視圖,即scanView。
在實(shí)現(xiàn)部分找到startReading方法,添加下面的代碼:
- (void)startScanning { // configure previewLayer ... // set the scanning area [[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { metadataOutput.rectOfInterest = [self.previewLayer metadataOutputRectOfInterestForRect:self.scanView.frame]; }]; // start scanning ... }
需要注意的是,rectOfInterest屬性不能在設(shè)置 metadataOutput 時(shí)直接設(shè)置,而需要在AVCaptureInputPortFormatDescriptionDidChangeNotification通知里設(shè)置,否則 metadataOutputRectOfInterestForRect:方法會(huì)返回 (0, 0, 0, 0)。
為了讓掃描框更真實(shí)的顯示,我們需要自定義ScanView,為其繪制邊框、四角以及掃描線。
首先打開(kāi)ScanView.m文件,在實(shí)現(xiàn)部分重寫initWithCoder:方法,為scanView設(shè)置透明的背景顏色:
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self; }
然后重寫drawRect:方法,為scanView繪制邊框和四角:
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); // 繪制白色邊框 CGContextAddRect(context, self.bounds); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGContextSetLineWidth(context, 2.0); CGContextStrokePath(context); // 繪制四角: CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); CGContextSetLineWidth(context, 5.0); // 左上角: CGContextMoveToPoint(context, 0, 30); CGContextAddLineToPoint(context, 0, 0); CGContextAddLineToPoint(context, 30, 0); CGContextStrokePath(context); // 右上角: CGContextMoveToPoint(context, self.bounds.size.width - 30, 0); CGContextAddLineToPoint(context, self.bounds.size.width, 0); CGContextAddLineToPoint(context, self.bounds.size.width, 30); CGContextStrokePath(context); // 右下角: CGContextMoveToPoint(context, self.bounds.size.width, self.bounds.size.height - 30); CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height); CGContextAddLineToPoint(context, self.bounds.size.width - 30, self.bounds.size.height); CGContextStrokePath(context); // 左下角: CGContextMoveToPoint(context, 30, self.bounds.size.height); CGContextAddLineToPoint(context, 0, self.bounds.size.height); CGContextAddLineToPoint(context, 0, self.bounds.size.height - 30); CGContextStrokePath(context); }
如果希望在掃描過(guò)程中看到剛才繪制的掃描框,還需要切換到ViewController.m文件,在startStopAction:方法中添加下面的代碼來(lái)顯示掃描框:
- (IBAction)startStopAction:(id)sender { if (!self.isReading) { ... [self.view bringSubviewToFront:self.toolBar]; // display toolBar [self.view bringSubviewToFront:self.scanView]; // display scanView ... } ... }
現(xiàn)在運(yùn)行,你會(huì)看到下面的效果:
接下來(lái)我們繼續(xù)添加掃描線。
首先在ScanView.h文件的接口部分聲明一個(gè)NSTimer對(duì)象的屬性:
@property (nonatomic, strong) NSTimer *timer;
然后切換到ScanView.m文件,在實(shí)現(xiàn)部分添加loadScanLine方法及代碼,并在initWithCoder:方法中調(diào)用:
- (void)loadScanLine { self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:YES block:^(NSTimer * _Nonnull timer) { UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1.0)]; lineView.backgroundColor = [UIColor greenColor]; [self addSubview:lineView]; [UIView animateWithDuration:3.0 animations:^{ lineView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, 2.0); } completion:^(BOOL finished) { [lineView removeFromSuperview]; }]; }]; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { ... if (self) { ... [self loadScanLine]; } ... }
然后切換到ViewController.m文件,在startStopAction:方法中添加下面代碼以啟用和暫停計(jì)時(shí)器:
- (IBAction)startStopAction:(id)sender { if (!self.isReading) { ... [self.view bringSubviewToFront:self.scanView]; // display scanView self.scanView.timer.fireDate = [NSDate distantPast]; //start timer ... } else { [self stopScanning]; self.scanView.timer.fireDate = [NSDate distantFuture]; //stop timer ... } ... }
最后,再在viewWillAppear:的重寫方法中添加下面代碼:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.scanView.timer.fireDate = [NSDate distantFuture]; }
可以運(yùn)行看下:
2.3.4 從圖片解析二維碼
從iOS 8開(kāi)始,可以使用Core Image框架中的CIDetector解析圖片中的二維碼。在這個(gè)應(yīng)用中,我們通過(guò)點(diǎn)擊Album按鈕,從相冊(cè)選取二維碼來(lái)解析。
在寫代碼之前,需要在Info.plist文件中添加NSPhotoLibraryAddUsageDescription鍵及相應(yīng)描述以訪問(wèn)相冊(cè):
然后在ViewController.m文件中添加UIImagePickerControllerDelegate和UINavigationControllerDelegate協(xié)議:
復(fù)制代碼 代碼如下:
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
在實(shí)現(xiàn)部分找到readingFromAlbum:方法,添加下面代碼以訪問(wèn)相冊(cè)中的圖片:
- (IBAction)readingFromAlbum:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; }
然后實(shí)現(xiàn)UIImagePickerControllerDelegate的imagePickerController:didFinishPickingMediaWithInfo:方法以解析選取的二維碼圖片:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { [picker dismissViewControllerAnimated:YES completion:nil]; UIImage *selectedImage = [info objectForKey:UIImagePickerControllerEditedImage]; CIImage *ciImage = [[CIImage alloc] initWithImage:selectedImage]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyLow}]; NSArray *features = [detector featuresInImage:ciImage]; if (features.count > 0) { CIQRCodeFeature *feature = features.firstObject; NSString *message = feature.messageString; // display message [self displayMessage:message]; // play sound if (self.audioPlayer) { [self.audioPlayer play]; } } }
以上就是怎么在ios中利用AVFoundation讀取二維碼,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。