iOS 开发AVFoundation系统原生二维码扫描实现
生活随笔
收集整理的這篇文章主要介紹了
iOS 开发AVFoundation系统原生二维码扫描实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*
先在項(xiàng)目中導(dǎo)入AVFoundation.framework框架
*/
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface QRViewController : UIViewController
{/*** AVCaptureSession:AVFoundation中的核心類,用于通過硬件獲取、處理和輸出視頻。一個(gè)Capture Session由多個(gè)輸入和多個(gè)輸出組成,并控制輸出幀的格式和分辨率。AVCaptureVideoPreviewLayer:用于顯示攝像頭捕捉到得視頻到UI。*/AVCaptureSession *_captureSession;AVCaptureVideoPreviewLayer *_previewLayer;
}#import "QRViewController.h"
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@interface QRViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{int num;BOOL upOrdown;NSTimer *timer;//自定義界面UIImageView *imageView;UIButton *scanButton;UILabel *labIntroudction;UIImageView *line;
}
@end@implementation QRViewController- (void)viewDidLoad {[super viewDidLoad];_previewLayer.frame = self.view.frame;[self initWith];[self setupCaptureSession];[self.view addSubview:scanButton];[self.view addSubview:labIntroudction];[self.view addSubview:imageView];upOrdown = NO;num =0;timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];}//自定義界面- (void)initWith
{scanButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[scanButton setTitle:@"取消" forState:UIControlStateNormal];scanButton.frame = CGRectMake(50, Height - 200, Width - 100, 50);scanButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];[scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(50, 100, Width - 100, 50)];labIntroudction.numberOfLines = 0;labIntroudction.textColor=[UIColor whiteColor];labIntroudction.text=@"將二維碼圖像置于矩形方框內(nèi),離手機(jī)攝像頭10CM左右,系統(tǒng)會(huì)自動(dòng)識(shí)別。";labIntroudction.font = [UIFont systemFontOfSize:16.0];labIntroudction.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];imageView.frame = CGRectMake(40, 170, Width - 80, Width - 80);_line = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, imageView.frame.size.width - 20, 2)];_line.image = [UIImage imageNamed:@"line.png"];[imageView addSubview:_line];self.view.backgroundColor = [UIColor clearColor];}-(void)animation
{if (upOrdown == NO) {num ++;_line.frame = CGRectMake(10, 10 + 2*num, imageView.frame.size.width - 20, 2);if (2*num == CGRectGetHeight(imageView.frame) - 20) {upOrdown = YES;}}else {num --;_line.frame = CGRectMake(10, 10 + 2*num, imageView.frame.size.width - 20, 2);if (num == 0) {upOrdown = NO;}}}
-(void)backAction
{[self dismissViewControllerAnimated:YES completion:^{[timer invalidate];}];
}/*** 這個(gè)方法用于建立capture session。如果session已經(jīng)存在,則直接返回。初始化video device,若設(shè)備沒有攝像頭,則直接返回。初始化capture session。通過video device創(chuàng)建video input。查詢session是否接受一個(gè)輸入,如果接受,添加輸入到session中。最后,創(chuàng)建預(yù)覽層并為其指定要預(yù)覽的capture session。*/
- (void)setupCaptureSession {if (_captureSession) return;/*AVCaptureDevice:封裝設(shè)備上的物理攝像頭。對iPhone而言有前后兩個(gè)攝像頭。AVCaptureDeviceInput:要添加一個(gè)AVCaptureDevice到session中,需要用AVCaptureDeviceInput來包裹一下。*/// 首先我們應(yīng)該判斷當(dāng)前設(shè)備是否有捕獲數(shù)據(jù)流的設(shè)備。AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];if (!videoDevice) {NSLog(@"No video camera on this device!");return;}AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];//初始化一個(gè) CaptureSession對象_captureSession = [[AVCaptureSession alloc] init];//設(shè)置會(huì)話的輸入設(shè)備if ([_captureSession canAddInput:input]){[_captureSession addInput:input];}//對應(yīng)輸出AVCaptureMetadataOutput *captureMetadataOutput = [[ AVCaptureMetadataOutput alloc] init];[_captureSession addOutput:captureMetadataOutput];//設(shè)置掃描區(qū)域[captureMetadataOutput setRectOfInterest:CGRectMake(imageView.frame.origin.y/ Height ,imageView.frame.origin.x/ Width , imageView.frame.size.width / Height, imageView.frame.size.height / Width)];//創(chuàng)建一個(gè)隊(duì)列dispatch_queue_t metadataQueue = dispatch_queue_create("myQueue", 0);[captureMetadataOutput setMetadataObjectsDelegate:self queue:metadataQueue];[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];if([_captureSession canAddOutput:captureMetadataOutput]){[_captureSession addOutput:captureMetadataOutput];}// 降捕獲的數(shù)據(jù)流展現(xiàn)出來_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;[_previewLayer setFrame:self.view.layer.bounds];[self.view.layer addSublayer:_previewLayer];// 開始捕獲[ _captureSession startRunning];}- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{// 判斷是否有數(shù)據(jù),是否是二維碼數(shù)據(jù)if (metadataObjects != nil && [metadataObjects count] > 0) {AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];//獲得掃描的數(shù)據(jù),并結(jié)束掃描[self dismissViewControllerAnimated:YES completion:^{[timer invalidate];if(metadataObj.stringValue && [metadataObj.stringValue rangeOfString:@"http:"].location != NSNotFound){NSString *regex = @"http+:[^\\s]*";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];// 正則表達(dá)式判斷 是否包含 http:if ([predicate evaluateWithObject:metadataObj.stringValue]){// 判斷是不是我們自己的二維碼if ([metadataObj.stringValue rangeOfString:@"http://m.pinlehuo.com/api.php"].location != NSNotFound) {NSLog(@"-------%@", metadataObj.stringValue);}else{[[UIApplication sharedApplication]openURL:[NSURL URLWithString:metadataObj.stringValue]];}}} else {NSLog(@"symbol.data = %@", metadataObj.stringValue);}}]; }
}@end
總結(jié)
以上是生活随笔為你收集整理的iOS 开发AVFoundation系统原生二维码扫描实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宁海中考政策计算机考试合格,宁海中考指南
- 下一篇: POI事件模式读取Excel 2003文