IOS 掃二維碼

jopen 9年前發布 | 9K 次閱讀 Objective-C IOS

注意:本篇文章采用了IOS7的新特性來掃二維碼,所以系統支持要IOS7以上,如果要兼容IOS7之前的版本,自行找庫來支持。

為了方便,我把掃二維碼簡單封裝到了一個UIView中,用代理的方式返回值

在使用之前,應當為工程添加AVFoundation.framework

最后實現的效果如圖

完整的實現代碼

頭文件


//
//  HwcScanQRView.h
//  HwcAnimationExample
//
//  Created by huangwenchen on 15/1/7.
//  Copyright (c) 2015年 huangwenchen. All rights reserved.
//

import <UIKit/UIKit.h>

@protocol HwcScanQRDelegate <NSObject> /*!

  • @discussion Delegate method when scan QR successed
  • @param QRContent Scan result
  • @return Void */

-(void)DidGetScanWithResult:(NSString )QRContent; /!

  • @discussion Delegate method when scan QR failed
  • @param error Error message
  • @return Void / -(void)DidFailToScanWithError:(NSError )error; @end

@interface HwcScanQRView : UIView @property id<HwcScanQRDelegate> delegate; @property(nonatomic,readonly) bool isScaning; -(BOOL)startScaning; -(void)stop; @end </pre> .m文件



//
//  HwcScanQRView.m
//  HwcAnimationExample
//
//  Created by huangwenchen on 15/1/7.
//  Copyright (c) 2015年 huangwenchen. All rights reserved.
//

import "HwcScanQRView.h"

import <AVFoundation/AVFoundation.h>

@interface HwcScanQRView()<AVCaptureMetadataOutputObjectsDelegate>

@property(nonatomic,readwrite) bool isScaning; @property(strong,nonatomic)AVCaptureSession captureSession; @property(strong,nonatomic)AVCaptureVideoPreviewLayer vedioPreviewLayer; @end

@implementation HwcScanQRView

pragma mark - Propertys

-(AVCaptureSession *)captureSession{ if (!_captureSession) { _captureSession = [[AVCaptureSession alloc] init]; } return _captureSession; }

-(AVCaptureVideoPreviewLayer *)vedioPreviewLayer{ if (!_vedioPreviewLayer) { _vedioPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession]; } return _vedioPreviewLayer; }

pragma mark - QRScan function

-(BOOL)startScaning{ NSError error; AVCaptureDevice captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; if (!deviceInput) { if ([self.delegate respondsToSelector:@selector(DidFailToScanWithError:)]) { [self.delegate DidFailToScanWithError:error]; } return NO; } [self.captureSession addInput:deviceInput]; AVCaptureMetadataOutput captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init]; [self.captureSession addOutput:captureMetadataOutput]; dispatch_queue_t scanQRqueue; scanQRqueue = dispatch_queue_create("scanQRqueue",DISPATCH_QUEUE_SERIAL); [captureMetadataOutput setMetadataObjectsDelegate:self queue:scanQRqueue]; [captureMetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; [self.vedioPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; [self.vedioPreviewLayer setFrame:self.layer.bounds]; [self.layer addSublayer:self.vedioPreviewLayer]; [self.captureSession startRunning]; return YES; } -(void)stop{ [self.captureSession stopRunning]; self.isScaning = NO; self.captureSession = nil; [_vedioPreviewLayer removeFromSuperlayer]; }

pragma mark - AVFoundation delegate method

-(void)captureOutput:(AVCaptureOutput )captureOutput didOutputMetadataObjects:(NSArray )metadataObjects fromConnection:(AVCaptureConnection )connection { if (metadataObjects != nil && metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject metadataObject = [metadataObjects firstObject]; if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode]) { NSString * scanResult = [metadataObject stringValue]; dispatch_async(dispatch_get_main_queue(), ^{ if ([self.delegate respondsToSelector:@selector(DidGetScanWithResult:)]) { [self.delegate DidGetScanWithResult:scanResult]; } }); } } }

@end </pre>
在使用的類中



//
//  ViewController.m
//  HwcAnimationExample
//
//  Created by huangwenchen on 15/1/6.
//  Copyright (c) 2015年 huangwenchen. All rights reserved.
//

import "ViewController.h"

import "HwcScanQRView.h"

@interface ViewController ()<HwcScanQRDelegate>//這里實現代理 @property (strong,nonatomic)UILabel * scanresultLabel; @end

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad]; HwcScanQRView scanView = [[HwcScanQRView alloc] initWithFrame:CGRectMake(100,100,200, 200)]; scanView.delegate = self;//這一步必須的 [self.view addSubview:scanView]; self.scanresultLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 320, 200, 30)]; [self.view addSubview:self.scanresultLabel]; [scanView startScaning];//不要忘記開始掃描 } //兩個代理方法 -(void)DidGetScanWithResult:(NSString )QRContent{ self.scanresultLabel.text = QRContent; } -(void)DidFailToScanWithError:(NSError *)error{ NSLog(@"%@",error.localizedDescription); }

@end</pre>
來自: blog.csdn.net/hello_hwc

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!