引言
最近公司项目需要自定义一个二维码扫描界面,提前写了个小demo,可以自定义二维码样式和添加按钮,文字等,有详细的注释,代码可直接复制。
代码
#import "CustomScanView.h"
#import <AVFoundation/AVFoundation.h>
#define screenBounds [UIScreen mainScreen].bounds
@interface CustomScanView ()<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIImageView *line;
@property (nonatomic, strong) UIImageView *backRect;
@property (nonatomic, strong) UILabel *explainLabel;
// 用于扫码的线条动画
@property int num;
@property BOOL upOrdown;
@end
@implementation CustomScanView
- (void)viewDidLoad {
[self startScan];
}
- (void)startScan {
//获取摄像设备
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 setSessionPreset:AVCaptureSessionPresetHigh];
[self.session addInput:input];
[self.session addOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
self.layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
self.layer.frame=self.view.layer.bounds;// 设置照相显示的大小
[self.view.layer insertSublayer:self.layer atIndex:2];// 设置层级,可以在扫码时显示一些文字
//开始捕获
[self.session startRunning];
//边框
self.backRect = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 232, 232)];
self.backRect.center = self.view.center;
self.backRect.image = [UIImage imageNamed:@"Rectangle"];
[self.view addSubview:self.backRect];
// 线条
self.upOrdown = NO;
self.num =0;
self.line = [[UIImageView alloc] initWithFrame:CGRectMake((screenBounds.size.width-232)/2+10, self.backRect.frame.origin.y, self.backRect.frame.size.width - 20, 3)];
self.line.image = [UIImage imageNamed:@"Line.png"];
[self.view addSubview:self.line];
//文字提示
self.explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, self.backRect.frame.size.height+self.backRect.frame.origin.y + 30, screenBounds.size.width - 60, 30)];
self.explainLabel.text = @"将方框对准二维码、条形码进行扫描";
self.explainLabel.textColor = [UIColor whiteColor];
self.explainLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.explainLabel];
self.timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}
//扫描时会调用以下方法
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count>0) {
//[session stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
//输出扫描字符串
NSLog(@"%@",metadataObject.stringValue);
// 关闭扫描,退出扫描界面
[self.session stopRunning];
[self.layer removeFromSuperlayer];
}
}
// 扫描线条动画
-(void)animation
{
if (self.upOrdown == NO) {
self.num ++;
self.line.frame = CGRectMake((screenBounds.size.width-232)/2+10, self.backRect.frame.origin.y+2*self.num, self.backRect.frame.size.width - 20, 3);
if (2*self.num == 232) {
self.upOrdown = YES;
}
}
else {
self.num --;
self.line.frame = CGRectMake((screenBounds.size.width-232)/2+10, self.backRect.frame.origin.y+2*self.num, self.backRect.frame.size.width - 20, 3);
if (self.num == 0) {
self.upOrdown = NO;
}
}
}