你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS自帶原生二維碼掃描的完成

iOS自帶原生二維碼掃描的完成

編輯:IOS開發綜合

前言

首先闡明的是:原生的二維碼掃描有一個坑,那就是掃描范圍確實定。只需記得掃描范圍是X與Y互換地位,W與H互換地位,就沒有什麼問題了。

上面進入正題:

1.由於運用原生二維碼掃描,所以需求參加頭文件添加delegate

#import <AVFoundation/AVFoundation.h>
<AVCaptureMetadataOutputObjectsDelegate>

2.接著是運用到的類

@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (weak, nonatomic) IBOutlet UIView *outputView;//xib中掃描的View
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
@property (strong, nonatomic) NSTimer * timer;//為了做掃描動畫的定時器
@property (strong, nonatomic) UIImageView * lineImage;//掃描動畫的橫線

3.懶加載一個掃描動畫的圖片

-(UIImageView *)lineImage{
 if (!_lineImage) {
  CGFloat outputW = self.outputView.frame.size.width;
  _lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,outputW, 2)];
  _lineImage.image = [UIImage imageNamed:@"ray"];
 }
 return _lineImage;
}

4.運用前的設置,我將它設置在了viewDidLoad當中

-viewDidLoad{
[super viewDidLoad];
 // Device
 _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

 // Input
 _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

 // Output
 _output = [[AVCaptureMetadataOutput alloc]init];
 [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

 // Session
 _session = [[AVCaptureSession alloc]init];
 [_session setSessionPreset:AVCaptureSessionPresetHigh];
 //銜接輸出和輸入
 if ([_session canAddInput:self.input])
 {
  [_session addInput:self.input];
 }

 if ([_session canAddOutput:self.output])
 {
  [_session addOutput:self.output];
 }
//設置條碼類型
 _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];

 //設置條碼地位
 CGFloat X = (ScreenW/2-100)/ScreenW;
 CGFloat Y = (ScreenH/2-100)/ScreenH;
 CGFloat W = 200/ScreenW;
 CGFloat H = 200/ScreenH;
 //設置掃描范圍(留意,X與Y交互,W與H交流)
 [_output setRectOfInterest:CGRectMake(Y, X, H, W)];
//添加掃描畫面
 _preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
 _preview.videoGravity =AVLayerVideoGravityResizeaspectFill;
 _preview.frame = CGRectMake(0, 0, ScreenW, ScreenH);//self.view.layer.bounds;
 [self.view.layer insertSublayer:_preview atIndex:0];
 //開端掃描
 [_session startRunning];

//添加掃描動畫定時器
[self.outputView addSubview:self.lineImage];
 // Do any additional setup after loading the view from its nib.
 _timer = [NSTimer scheduledTimerWithTimeInterval:2.5f
           target:self
           selector:@selector(lineAction)
           userInfo:nil
           repeats:YES];
}

5.二維碼掃描的代理事情

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
 NSString *stringValue;
 if ([metadataObjects count] >0){
  //中止掃描
  [_session stopRunning];
  AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
  stringValue = metadataObject.stringValue;//stringValue是掃描拿到的內容,更具內容停止後續任務。
 }
}

6.添加掃描動畫的事情

- (void)lineAction{
 CGFloat outputW = self.outputView.frame.size.width;
 CGFloat outputH = self.outputView.frame.size.height;
 [UIView animateWithDuration:2.4f animations:^{
  CGRect frame = CGRectMake(0, outputH, outputW, 2);
  self.lineImage.frame = frame;
 } completion:^(BOOL finished) {
  CGRect frame = CGRectMake(0, 0, outputW, 2);
  self.lineImage.frame = frame;
 }];
}

搞定......最後放上一張效果圖

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或許任務能帶來一定的協助,假如有疑問大家可以留言交流。

【iOS自帶原生二維碼掃描的完成】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved