IOS 二维码扫码
来源:互联网 发布:矩阵革命电影天堂 编辑:程序博客网 时间:2024/05/17 00:17
引言
最近公司项目需要自定义一个二维码扫描界面,提前写了个小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; } }}
0 0
- IOS 二维码扫码
- [iOS]扫码+生成二维码
- iOS二维码生成及扫码
- iOS二维码生成及扫码
- ios 二维码扫瞄
- IOS 扫二维码
- IOS生成二维码、扫二维码
- ios 二维码生成 扫瞄
- 扫码二维码
- Android 二维码扫码
- 二维码扫码优化
- 二维码扫码
- 二维码扫码
- iOS AV Foundation 二维码扫描 02 扫码
- IOS实现原生二维码扫码(swift版)
- iOS 原生二维码[扫码]与[生成]详解
- iOS原生二维码扫码实现(含蒙版和扫码动画)
- Android二维码扫码模块
- Qt 常用类 (8)—— QIcon
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 常用排序算法----Java实现
- for-in switch
- android 抛出Error:Execution failed for task .....
- IOS 二维码扫码
- 斐波那契数列
- 窗体的形成
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxx'
- iOS开发从 UIWebView 到 WKWebView
- WebView开发总结
- 虚拟主机配置
- Java中垃圾回收(gc)问题
- Git的相关用法