iOS - 二维码、条码扫描

来源:互联网 发布:三国志11 mac 打不开 编辑:程序博客网 时间:2024/04/18 11:46

iOS7.0后AVFoundation框架提供的原生二维码、条码扫描。

#import "ViewController.h"@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>@property (nonatomic, strong) CAShapeLayer * maskLayer;@property (nonatomic, strong) CAShapeLayer * shadowLayer;@property (nonatomic, strong) CAShapeLayer * scanRectLayer;@property (nonatomic, assign) CGRect scanRect;@property (nonatomic, strong) UILabel * remind;@property (strong, nonatomic) AVCaptureSession *session;//输入输出的中间桥梁@property (strong, nonatomic) AVCaptureVideoPreviewLayer *layer;// 扫描所在的层级@property (nonatomic, retain) UIImageView *rectImage;// 扫描的方框@property (strong, nonatomic) UILabel *explainLabel;// 说明文本@property (nonatomic, retain) UIImageView *line;// 扫码区域的线条// 用于扫码的线条动画@property int num;@property BOOL upOrdown;@property NSTimer * timer;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.title = @"关于设备";}- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    [self scanView];}-(void)scanView {    // 获取摄像设备    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                                 ];//    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];    self.layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];    self.layer.videoGravity=AVLayerVideoGravityResizeAspectFill;    self.layer.frame=self.view.layer.bounds;    [self.view.layer insertSublayer:self.layer atIndex:0];    //开始捕获    [self.session startRunning];    // 方框    CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];    self.rectImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, (screenBounds.size.height - (screenBounds.size.width - 60))/2, screenBounds.size.width - 60, screenBounds.size.width - 60)];    self.rectImage.image = [UIImage imageNamed:@"pick_bg"];    [self.view addSubview:self.rectImage];    self.explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, (screenBounds.size.height - (screenBounds.size.width - 60))/2 + screenBounds.size.width - 50, screenBounds.size.width - 60, 30)];    self.explainLabel.text = @"将方框对准设备条形码进行扫描";    self.explainLabel.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:self.explainLabel];    // 线条动画    self.upOrdown = NO;    self.num =0;    self.line = [[UIImageView alloc] initWithFrame:CGRectMake(70, (screenBounds.size.height - (screenBounds.size.width - 60))/2 + 10, screenBounds.size.width - 140, 2)];    self.line.image = [UIImage imageNamed:@"line.png"];    [self.view addSubview:self.line];    self.timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];}// 扫描线条动画-(void)animation{    CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];    if (self.upOrdown == NO) {        self.num ++;        self.line.frame = CGRectMake(70, (screenBounds.size.height - (screenBounds.size.width - 60))/2 + 10 +2*self.num, screenBounds.size.width - 140, 2);        if (2*self.num == 280) {            self.upOrdown = YES;        }    }    else {        self.num --;        self.line.frame = CGRectMake(70, (screenBounds.size.height - (screenBounds.size.width - 60))/2 + 10 +2*self.num, screenBounds.size.width - 140, 2);        if (self.num == 0) {            self.upOrdown = NO;        }    }}// 捕获条码扫描结果-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{    if (metadataObjects != nil && metadataObjects.count>0) {        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];        NSString *scanStr = metadataObject.stringValue;        // 输出扫描字符串        NSLog(@"%@",scanStr);    }}- (void)stopScan {    [self.session stopRunning];    // 震动、声音效果    [self systemSound];    [self systemVibrate];    // 移除扫描层    [self.layer removeFromSuperlayer];    // 去掉扫描显示的内容    [self.timer invalidate];    [self.line removeFromSuperview];    [self.rectImage removeFromSuperview];    [self.explainLabel removeFromSuperview];}#pragma mark- 震动、声音效果#define SOUNDID  1109  //1012 -iphone   1152 ipad  1109 ipad- (void)systemVibrate{    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);}- (void)systemSound{    AudioServicesPlaySystemSound(SOUNDID);}@end
0 0
原创粉丝点击