码迷,mamicode.com
首页 > 移动开发 > 详细

Ios二维码扫描(系统自带的二维码扫描)

时间:2015-06-25 22:41:49      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:

Ios二维码扫描

 

  这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画!

操作步骤:

  1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label

          技术分享

   2)、你需要在你当前的控制器中导入

        #import <AVFoundation/AVFoundation.h>

        <AVCaptureMetadataOutputObjectsDelegate>代理

   3)、在@interface中定义    

      @property (nonatomic,strong)AVCaptureDevice * device;

      @property (nonatomic,strong)AVCaptureDeviceInput * input;

      @property (nonatomic,strong)AVCaptureMetadataOutput * output;

      @property (nonatomic,strong)AVCaptureSession * session;

      @property (nonatomic,strong)AVCaptureVideoPreviewLayer * preview;

    4)、将UI界面中的俩个图片属性拖进来

      @property (strong, nonatomic) IBOutlet UIImageView *preReferImage;//这个是UI界面中的框子图片

      @property (nonatomic,strong)IBOutlet UIImageView * imageLine;//这个是UI界面中的绿色线条图片

    5)、就是将如下代码放进你的控制器中   

  - (void)viewDidLoad

    {

          [super viewDidLoad];

          [self setupCamera];//设置相机

}

    //设置相机

  -(void)setupCamera

    {

        // Device 属性 

        self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        // Input 属性

        NSError *error = nil;

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

       if (error) {

            NSLog(@"错误");

            return;

        }

        // Output 属性

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

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

        // Session 属性

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

        [self.session setSessionPreset:AVCaptureSessionPresetHigh];

       if ([self.session canAddInput:self.input])

      {

           [self.session addInput:self.input];

      }

        if ([self.session canAddOutput:self.output])

      {

           [self.session addOutput:self.output];

      }

      self.output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];//这里是设置扫描的类型

      // Preview 属性

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

      self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

      self.preview.frame = self.view.bounds;

      [self.view.layer insertSublayer:self.preview below:self.preReferImage.layer];  

      // Start 属性

      [self.session startRunning];  

    [self setupAnimation];//这个是横线的上下扫描动画,可添加可不添加

   }

/**注意了:你如果需要添加这个横线扫描的动画需要在@interface中添加如下几个属性*/

{

    int lineValue; //保存横线的frame值

    NSTimer * timer;//定时器让横线走动

    BOOL Down;//向下

    BOOL up;//向上

}

  //横线动画

 - (void)setupAnimation

  {

      CGFloat beginLineValue = CGRectGetMinY(self.preReferImage.frame);

      Down = YES;

      up = NO;

      lineValue =beginLineValue;

      [timer invalidate];

      timer = [NSTimer scheduledTimerWithTimeInterval:.01

            target:self selector:@selector(animation1) userInfo:nil repeats:YES];

}

 -(void)animation1

  {

    if (Down) {

         CGFloat maxValue = CGRectGetMaxY(self.preReferImage.frame);

         lineValue++;

        self.imageLine.frame = CGRectMake(self.imageLine.frame.origin.x,lineValue, self.imageLine.frame.size.width, self.imageLine.frame.size.height);

        if (lineValue ==maxValue) {

            up = YES;

        }

    }

      if (up){

          CGFloat minValue = CGRectGetMinY(self.preReferImage.frame);

          lineValue-=2;

          self.imageLine.frame = CGRectMake(self.imageLine.frame.origin.x, lineValue, self.imageLine.frame.size.width, self.imageLine.frame.size.height);

        if (lineValue ==minValue) {

            Down = YES;

            up = NO;

        }

    }

}

 

/**这个是必须要实现的代理方法,从这个方法中可以得到所扫描出来的URL*/

#pragma mark - AVCaptureMetadataOutputObjectsDelegate

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

{

    NSString * stringValue ;

    if ([metadataObjects count] > 0) {

        [self.session stopRunning];

        AVMetadataMachineReadableCodeObject * metadataObj = [metadataObjects objectAtIndex:0];

        stringValue = metadataObj.stringValue;

        [timer invalidate];//这个是扫描成功后停止动画

        timer = nil;

        [self requestParsing:stringValue]; //这个是扫描成功后所做的网络请求方法,可以注释

        NSLog(@"stringValue =%@",stringValue);

    }

}

 /**注意:

    1、以上这个例子只是说明如何使用系统自带的二维码扫描,没有做扫描成功后进行网络访问;

    2、这个二维码扫描除开中间图片框式可以看见外其他屏幕也是可以看见的,我的解决方法是添加一个View跟当前的这个View是在同一层次上不透明就可以了

    这个扫描中用到的框架有:

        AFNetworking    这个是用来网络请求的

        MBProgressHUD  这个是用来加载网络是的提示语的,例如网络加载中的时候,就会弹出一个框:正在加载中。。。等等!

  当然还有第三方的二维码扫描:ZXing和ZBar在github上都有下载

*/

Ios二维码扫描(系统自带的二维码扫描)

标签:

原文地址:http://www.cnblogs.com/WX95/p/4600922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!