关闭

简单使用系统的设备实现二维码扫描

标签: 二维码扫描
127人阅读 评论(0) 收藏 举报

这里我用了系统的东西简单的实现了一下扫描二维码

.h

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface saoMiaoViewController : UIViewController



@property(nonatomic,retain) UIView * sanRectView;


//硬件设备

@property(nonatomic,retain) AVCaptureDevice * device;


//输入设备

@property(nonatomic,retain) AVCaptureDeviceInput * input;


//输出设备

@property(nonatomic,retain) AVCaptureMetadataOutput * outPut;



//桥梁。链接输入和输出设备


@property(nonatomic,retain)AVCaptureSession * session;


@property(nonatomic,retain)AVCaptureVideoPreviewLayer * preView;



@end


接下来看.m文件


@interface saoMiaoViewController ()<AVCaptureMetadataOutputObjectsDelegate>


@end


@implementation saoMiaoViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    self.title = @"进入连接";

    

    CGSize windowSize = [UIScreen mainScreen].bounds.size;

    

    

    CGSize scanSize = CGSizeMake(windowSize.width*3/5, windowSize.width * 3/5);

    

    

    CGRect scanRect = CGRectMake( ((windowSize.height-scanSize.height)/2)/windowSize.height,((windowSize.width-scanSize.width)/2)/windowSize.width,  (scanSize.height+10)/windowSize.height,(scanSize.width + 10)/windowSize.width);

    

    

    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    

    

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

    

    

    self.outPut = [[AVCaptureMetadataOutput alloc] init];

    

    

    [self.outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    

    self.session = [[AVCaptureSession alloc] init];

    

    

    [self.session setSessionPreset:([UIScreen mainScreen].bounds.size.height<500)?AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];

    

    

    

    [self.session addInput:self.input];

    [self.session addOutput:self.outPut];

    

    self.outPut.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];


    self.preView = [AVCaptureVideoPreviewLayer layerWithSession:self.session];

    self.preView.videoGravity = AVLayerVideoGravityResizeAspectFill;

    self.preView.frame = [UIScreen mainScreen].bounds;

    

    self.preView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7].CGColor;

    [self.view.layer insertSublayer:self.preView atIndex:0];

    

    


    

    self.sanRectView = [UIView new];

    [self.view addSubview:self.sanRectView];

    self.sanRectView.frame = CGRectMake(0, 0, scanSize.width, scanSize.height);

    self.sanRectView.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));

    self.sanRectView.layer.borderColor = [UIColor whiteColor].CGColor;

    self.sanRectView.layer.borderWidth = 2;

    

    self.outPut.rectOfInterest =scanRect;

    //开始捕获

    [self.session startRunning];

    

    

    

    

}



- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

    

    

    if ( (metadataObjects.count==0) )

    {

        return;

    }

    

    if (metadataObjects.count>0) {

        

        [self.session stopRunning];

        

        AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject;

    

        SendUrlViewController * send = [[SendUrlViewController alloc] init];

        

        

        send.nameUrl = metadataObject.stringValue;

        

        [self.navigationController pushViewController:send animated:YES];

        

    }

}


此文纯属个人为了记忆而写。当然要是能帮到别人当然也很开心了A.A








1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5745次
    • 积分:110
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论