前言
成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、三門(mén)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為三門(mén)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
首先說(shuō)明的是:原生的二維碼掃描有一個(gè)坑,那就是掃描范圍的確定。只要記得掃描范圍是X與Y互換位置,W與H互換位置,就沒(méi)有什么問(wèn)題了。
下面進(jìn)入正題:
1.因?yàn)槭褂迷S碼掃描,所以需要加入頭文件添加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;//為了做掃描動(dòng)畫(huà)的定時(shí)器 @property (strong, nonatomic) UIImageView * lineImage;//掃描動(dòng)畫(huà)的橫線
3.懶加載一個(gè)掃描動(dòng)畫(huà)的圖片
-(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.使用前的設(shè)置,我將它設(shè)置在了viewDidLoad當(dāng)中
-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]; } //設(shè)置條碼類型 _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode]; //設(shè)置條碼位置 CGFloat X = (ScreenW/2-100)/ScreenW; CGFloat Y = (ScreenH/2-100)/ScreenH; CGFloat W = 200/ScreenW; CGFloat H = 200/ScreenH; //設(shè)置掃描范圍(注意,X與Y交互,W與H交換) [_output setRectOfInterest:CGRectMake(Y, X, H, W)]; //添加掃描畫(huà)面 _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]; //開(kāi)始掃描 [_session startRunning]; //添加掃描動(dòng)畫(huà)定時(shí)器 [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是掃描拿到的內(nèi)容,更具內(nèi)容進(jìn)行后續(xù)工作。 } }
6.添加掃描動(dòng)畫(huà)的事件
- (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; }]; }
搞定......最后放上一張效果圖
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
網(wǎng)站題目:iOS自帶原生二維碼掃描的實(shí)現(xiàn)
本文來(lái)源:http://aaarwkj.com/article12/gjgggc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號(hào)、App設(shè)計(jì)、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)