栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

扫描二维码控件的封装iOS实现

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

扫描二维码控件的封装iOS实现

扫描二维码效果

 

源码:https://github.com/YouXianMing/Animations 

//
// QRCodeView.h
// QRCode
//
// Created by YouXianMing on 16/7/7.
// Copyright © 2016年 XianMing You. All rights reserved.
//

#import 
#import 
@class QRCodeView;

@protocol QRCodeViewDelegate 

@optional


- (void)QRCodeView:(QRCodeView *)codeView codeString:(NSString *)codeString;

@end

@interface QRCodeView : UIView


@property (nonatomic, weak) id  delegate;


@property (nonatomic) AVCaptureTorchMode torchMode;


@property (nonatomic) CGRect interestArea;


@property (nonatomic, strong) UIView *contentView;


@property (nonatomic, readonly) BOOL isRunning;


- (BOOL)start;


- (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 () 

@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;
 }
 
 // 创建会话
 self.captureSession = [[AVCaptureSession alloc] init];
 
 // 添加输入流
 [self.captureSession addInput:self.deviceInput];
 
 // 初始化输出流
 self.capturemetadataOutput = [[AVCapturemetadataOutput alloc] init];
 
 // 添加输出流
 [self.captureSession addOutput:self.capturemetadataOutput];
 
 // 创建queue.
 [self.capturemetadataOutput setmetadataObjectsDelegate:self queue:dispatch_queue_create(nil, nil)];
 self.capturemetadataOutput.metadataObjectTypes = @[AVmetadataObjectTypeQRCode];
 
 // 创建输出对象
 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/167226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号