iOS原生二维码扫描
2017-03-22 本文已影响73人
哈哈大p孩
公司要做二维码扫描功能,研究了下,发现苹果有源生的类库,将代码贴一下,详细注释在代码中。
首先需要导入
<AVFoundation/AVFoundation.h>
定义了一些宏
/**
* 屏幕 高 宽 边界
*/
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds
#define TOP (SCREEN_HEIGHT-220)/2
#define LEFT (SCREEN_WIDTH-220)/2
#define kScanRect CGRectMake(LEFT, TOP, 220, 220)
定义变量
{
int num;
BOOL upOrdown;
NSTimer * timer;
CAShapeLayer *cropLayer;
}
@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
@property (nonatomic, strong) UIImageView * line;
遵守代理AVCaptureMetadataOutputObjectsDelegate
详细代码如下,在这里就不累赘了,具体解释已经标注在代码里了。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blackColor];
[self setCropRect:kScanRect];
[self performSelector:@selector(setupCamera) withObject:nil afterDelay:0.3];
[self configView];
}
-(void)configView{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:kScanRect];
imageView.image = [UIImage imageNamed:@"pick_bg"];
[self.view addSubview:imageView];
upOrdown = NO;
num =0;
_line = [[UIImageView alloc] initWithFrame:CGRectMake(LEFT, TOP+10, 220, 2)];
_line.image = [UIImage imageNamed:@"line.png"];
[self.view addSubview:_line];
timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
}
-(void)viewWillAppear:(BOOL)animated{
//重新开启扫描
if (_session != nil && timer != nil) {
[_session startRunning];
[timer setFireDate:[NSDate date]];
}
}
-(void)animation1
{
if (upOrdown == NO) {
num ++;
_line.frame = CGRectMake(LEFT, TOP+10+2*num, 220, 2);
if (2*num == 200) {
upOrdown = YES;
}
}
else {
num --;
_line.frame = CGRectMake(LEFT, TOP+10+2*num, 220, 2);
if (num == 0) {
upOrdown = NO;
}
}
}
- (void)setCropRect:(CGRect)cropRect{
cropLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, cropRect);
CGPathAddRect(path, nil, self.view.bounds);
[cropLayer setFillRule:kCAFillRuleEvenOdd];
[cropLayer setPath:path];
[cropLayer setFillColor:[UIColor blackColor].CGColor];
[cropLayer setOpacity:0.6];
[cropLayer setNeedsDisplay];
[self.view.layer addSublayer:cropLayer];
}
- (void)setupCamera
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device==nil) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"设备没有摄像头" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// Device,获取摄像设备
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input,创建输入流
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output,创建输出流
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置扫描区域
CGFloat top = TOP/SCREEN_HEIGHT;
CGFloat left = LEFT/SCREEN_WIDTH;
CGFloat width = 220/SCREEN_WIDTH;
CGFloat height = 220/SCREEN_HEIGHT;
///top 与 left 互换 width 与 height 互换
[_output setRectOfInterest:CGRectMake(top,left, height, width)];
// Session 初始化连接对象
_session = [[AVCaptureSession alloc]init];
// 高质量采集率
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
// 条码类型 设置条形码和二维码兼容
[_output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, nil]];
// Preview 开始捕获
_preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame =self.view.layer.bounds;
[self.view.layer insertSublayer:_preview atIndex:0];
// Start
[_session startRunning];
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSString *stringValue;
if ([metadataObjects count] >0)
{
//停止扫描
[_session stopRunning];
[timer setFireDate:[NSDate distantFuture]];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
NSLog(@"扫描结果:%@",stringValue);
[self.navigationController pushViewController:[JumpViewController new] animated:YES];
} else {
NSLog(@"无扫描信息");
return;
}
}
看下真机效果:
ds.gif
我在这里根据网上的朋友们写的东西,做了点总结并写了个demo,详细代码就是上面的了,如果需要demo的点击下载我上传github源码
当然了,如果你用swift语言在,你可以看下色长的简书关于swift下原生二维码扫描。@TomatosX