本文介绍的是系统自带的二维码扫描!方便简单实用。-----话不多说,直接上干货!
三步就搞定!!!!!!! QQ : 137026391--马尾 欢迎指教
1、首页 先创建扫描按钮、显示扫描结果的label
- (void)viewDidLoad {
[super viewDidLoad];
//扫描按钮
self.scanButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.scanButton.frame = CGRectMake((SCREEN_WIDTH - 100) / 2, SCREEN_HEIGHT - 100, 100, 30);
[self.scanButton setTitle:@"扫描" forState:UIControlStateNormal];
[self.scanButton addTarget:self action:@selector(scanAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.scanButton];
//显示扫描结果
self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 100)];
self.resultLabel.textAlignment = NSTextAlignmentCenter;
self.resultLabel.numberOfLines = 0;
self.resultLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:self.resultLabel];
}
* 扫描事件
- (void)scanAction:(UIButton *)sender{
ZFScanViewController * vc = [[ZFScanViewController alloc] init];
vc.returnScanBarCodeValue = ^(NSString * barCodeString){
self.resultLabel.text = [NSString stringWithFormat:@"扫描结果:\n%@",barCodeString];
NSLog(@"扫描结果的字符串======%@",barCodeString);
};
[self.navigationController pushViewController:vc animated:YES];
}
2、创建扫描
#import "ZFScanViewController.h"
#import#import "ZFConst.h"
#import "ZFMaskView.h"
@interface ZFScanViewController ()
/** 输入输出的中间桥梁 */
@property (nonatomic, strong) AVCaptureSession * session;
/** 扫描支持的编码格式的数组 */
@property (nonatomic, strong) NSMutableArray * metadataObjectTypes;
/** 遮罩层 */
@property (nonatomic, strong) ZFMaskView * maskView;
@end
@implementation ZFScanViewController
- (NSMutableArray *)metadataObjectTypes{
if (!_metadataObjectTypes) {
_metadataObjectTypes = [NSMutableArray arrayWithObjects:AVMetadataObjectTypeAztecCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeUPCECode, nil];
// >= iOS 8
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
[_metadataObjectTypes addObjectsFromArray:@[AVMetadataObjectTypeInterleaved2of5Code, AVMetadataObjectTypeITF14Code, AVMetadataObjectTypeDataMatrixCode]];
}
}
return _metadataObjectTypes;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.maskView removeAnimation];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self capture];
[self addUI];
}
/**
* 添加遮罩层
*/
- (void)addUI{
self.maskView = [[ZFMaskView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
[self.view addSubview:self.maskView];
}
/**
* 扫描初始化
*/
- (void)capture{
//获取摄像设备
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc] init];
//设置代理 在主线程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//初始化链接对象
self.session = [[AVCaptureSession alloc] init];
//高质量采集率
self.session.sessionPreset = AVCaptureSessionPresetHigh;
[self.session addInput:input];
[self.session addOutput:output];
AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
layer.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
layer.backgroundColor = [UIColor yellowColor].CGColor;
[self.view.layer addSublayer:layer];
//设置扫描支持的编码格式(如下设置条形码和二维码兼容)
output.metadataObjectTypes = self.metadataObjectTypes;
//开始捕获
[self.session startRunning];
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count > 0) {
[self.session stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = metadataObjects.firstObject;
self.returnScanBarCodeValue(metadataObject.stringValue);
[self.navigationController popViewControllerAnimated:YES];
}
}
3、设置扫描界面的UI
#import "ZFMaskView.h"
#import "ZFConst.h"
@interface ZFMaskView()
@property (nonatomic, strong) UIImageView * scanLineImg;
@end
@implementation ZFMaskView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addUI];
}
return self;
}
/**
* 添加UI
*/
- (void)addUI{
//遮罩层
UIView * maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
maskView.backgroundColor = [UIColor blackColor];
maskView.alpha = 0.5;
maskView.layer.mask = [self maskLayer];
[self addSubview:maskView];
//提示框
UILabel * hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - 120, 60)];
hintLabel.text = @"将 二维码/条形码 放入框内中央,即可自动扫描";
hintLabel.center = CGPointMake(maskView.center.x, maskView.center.y + (self.frame.size.width - 120) * 0.5 + 40);
hintLabel.textColor = [UIColor lightGrayColor];
hintLabel.numberOfLines = 0;
hintLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:hintLabel];
//边框
UIImage * topLeft = [UIImage imageNamed:@"ScanQR1"];
UIImage * topRight = [UIImage imageNamed:@"ScanQR2"];
UIImage * bottomLeft = [UIImage imageNamed:@"ScanQR3"];
UIImage * bottomRight = [UIImage imageNamed:@"ScanQR4"];
//左上
UIImageView * topLeftImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5, topLeft.size.width, topLeft.size.height)];
topLeftImg.image = topLeft;
[self addSubview:topLeftImg];
//右上
UIImageView * topRightImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5 - topRight.size.width + self.frame.size.width - 120, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5, topRight.size.width, topRight.size.height)];
topRightImg.image = topRight;
[self addSubview:topRightImg];
//左下
UIImageView * bottomLeftImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5 - bottomLeft.size.height + self.frame.size.width - 120, bottomLeft.size.width, bottomLeft.size.height)];
bottomLeftImg.image = bottomLeft;
[self addSubview:bottomLeftImg];
//右下
UIImageView * bottomRightImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5 - bottomRight.size.width + self.frame.size.width - 120, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5 - bottomRight.size.width + self.frame.size.width - 120, bottomRight.size.width, bottomRight.size.height)];
bottomRightImg.image = bottomRight;
[self addSubview:bottomRightImg];
//扫描线
UIImage * scanLine = [UIImage imageNamed:@"QRCodeScanLine"];
self.scanLineImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5, self.frame.size.width - 120, scanLine.size.height)];
self.scanLineImg.image = scanLine;
self.scanLineImg.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:self.scanLineImg];
[self.scanLineImg.layer addAnimation:[self animation] forKey:nil];
}
- (CABasicAnimation *)animation{
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 3;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.repeatCount = MAXFLOAT;
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5)];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.scanLineImg.frame.origin.y + self.frame.size.width - 120 - self.scanLineImg.frame.size.height * 0.5)];
return animation;
}
/**
* 遮罩层bezierPath
*
* @return UIBezierPath
*/
- (UIBezierPath *)maskPath{
UIBezierPath * bezier = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[bezier appendPath:[[UIBezierPath bezierPathWithRect:CGRectMake((self.frame.size.width - (self.frame.size.width - 120)) * 0.5, (self.frame.size.height - (self.frame.size.width - 120)) * 0.5, self.frame.size.width - 120, self.frame.size.width - 120)] bezierPathByReversingPath]];
return bezier;
}
/**
* 遮罩层ShapeLayer
*
* @return CAShapeLayer
*/
- (CAShapeLayer *)maskLayer{
CAShapeLayer * layer = [CAShapeLayer layer];
layer.path = [self maskPath].CGPath;
return layer;
}
/**
* 移除动画
*/
- (void)removeAnimation{
[self.scanLineImg.layer removeAllAnimations];
}
@end
网友评论