掃描二維碼控件的封裝iOS實現(xiàn)
更新時間:2016年08月10日 10:03:57 作者:YouXianMing
這篇文章主要為大家詳細介紹了iOS實現(xiàn)掃描二維碼控件的封裝,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下
掃描二維碼效果

源碼:https://github.com/YouXianMing/Animations
// // QRCodeView.h // QRCode // // Created by YouXianMing on 16/7/7. // Copyright © 2016年 XianMing You. All rights reserved. // #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @class QRCodeView; @protocol QRCodeViewDelegate <NSObject> @optional /** * 獲取QR的掃描結(jié)果 * * @param codeView QRCodeView實體對象 * @param codeString 掃描字符串 */ - (void)QRCodeView:(QRCodeView *)codeView codeString:(NSString *)codeString; @end @interface QRCodeView : UIView /** * 代理 */ @property (nonatomic, weak) id <QRCodeViewDelegate> delegate; /** * 燈的狀態(tài),默認(rèn)為關(guān)閉 */ @property (nonatomic) AVCaptureTorchMode torchMode; /** * 敏感區(qū)域,如果不設(shè)置,則為全部掃描區(qū)域 */ @property (nonatomic) CGRect interestArea; /** * 你用來添加自定義控件的view,尺寸與當(dāng)前初始化的view一致 */ @property (nonatomic, strong) UIView *contentView; /** * 正在運行當(dāng)中 */ @property (nonatomic, readonly) BOOL isRunning; /** * 開始掃描 * * @return 如果成功,則返回YES,否則返回NO */ - (BOOL)start; /** * 結(jié)束掃描 */ - (void)stop; @end
//
// QRCodeView.m
// QRCode
//
// Created by YouXianMing on 16/7/7.
// Copyright © 2016年 XianMing You. All rights reserved.
//
#import "QRCodeView.h"
@interface QRCodeView () <AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic) BOOL isRunning;
@property (nonatomic, strong) UIView *videoView;
@property (nonatomic, strong) AVCaptureDeviceInput *deviceInput;
@property (nonatomic, strong) AVCaptureDevice *captureDevice;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (nonatomic, strong) AVCaptureMetadataOutput *captureMetadataOutput;
@end
@implementation QRCodeView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.videoView = [[UIView alloc] initWithFrame:self.bounds];
[self addSubview:self.videoView];
self.contentView = [[UIView alloc] initWithFrame:self.bounds];
[self addSubview:self.contentView];
self.captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_torchMode = AVCaptureTorchModeOff;
[self addNotificationCenter];
}
return self;
}
#pragma mark - NSNotificationCenter related.
- (void)addNotificationCenter {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationCenterEvent:)
name:AVCaptureInputPortFormatDescriptionDidChangeNotification
object:nil];
}
- (void)removeNotificationCenter {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVCaptureInputPortFormatDescriptionDidChangeNotification
object:nil];
}
- (void)notificationCenterEvent:(NSNotification *)sender {
if (self.interestArea.size.width && self.interestArea.size.height) {
self.captureMetadataOutput.rectOfInterest = [self.videoPreviewLayer metadataOutputRectOfInterestForRect:self.interestArea];
} else {
self.captureMetadataOutput.rectOfInterest = CGRectMake(0, 0, 1, 1);
}
}
#pragma mark - Start & Stop.
- (BOOL)start {
// 初始化輸入流
BOOL result = NO;
NSError *error = nil;
self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
if (self.deviceInput == nil) {
NSLog(@"%@", error);
return result;
}
// 創(chuàng)建會話
self.captureSession = [[AVCaptureSession alloc] init];
// 添加輸入流
[self.captureSession addInput:self.deviceInput];
// 初始化輸出流
self.captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
// 添加輸出流
[self.captureSession addOutput:self.captureMetadataOutput];
// 創(chuàng)建queue.
[self.captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatch_queue_create(nil, nil)];
self.captureMetadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
// 創(chuàng)建輸出對象
self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPreviewLayer.frame = self.contentView.bounds;
[self.videoView.layer addSublayer:self.videoPreviewLayer];
// 開始
[self.captureSession startRunning];
self.isRunning = YES;
result = YES;
return result;
}
- (void)stop {
[self.captureSession stopRunning];
self.isRunning = NO;
self.captureSession = nil;
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection {
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *metadata = metadataObjects.firstObject;
NSString *result = nil;
if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
result = metadata.stringValue;
if (_delegate && [_delegate respondsToSelector:@selector(QRCodeView:codeString:)]) {
[_delegate QRCodeView:self codeString:result];
}
}
}
}
#pragma mark - Setter & Getter.
- (void)setTorchMode:(AVCaptureTorchMode)torchMode {
_torchMode = torchMode;
if (_deviceInput && [self.captureDevice hasTorch]) {
[self.captureDevice lockForConfiguration:nil];
[self.captureDevice setTorchMode:torchMode];
[self.captureDevice unlockForConfiguration];
}
}
#pragma mark - System method.
- (void)dealloc {
[self stop];
[self removeNotificationCenter];
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS自定義UIBarButtonItem的target和action示例代碼
這篇文章主要給大家介紹了關(guān)于iOS自定義UIBarButtonItem的target和action的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
ios7中UIViewControllerBasedStatusBarAppearance作用詳解
這篇文章主要介紹了 ios7中UIViewControllerBasedStatusBarAppearance作用詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
簡介Objective-C解析XML與JSON數(shù)據(jù)格式的方法
這篇文章主要介紹了Objective-C解析XML與JSON數(shù)據(jù)格式的方法,文中解析JSON包括拼接JSON字符串用到了SBJson這個解析器,需要的朋友可以參考下2016-01-01
Flutter?ScrollController滾動監(jiān)聽及控制示例詳解
這篇文章主要為大家介紹了Flutter?ScrollController滾動監(jiān)聽及控制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

