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


    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上都有下载

    */

  • 相关阅读:
    go语言正则
    20个简洁的js代码片段
    笔记本连接WiFi提示“无法连接此网络”解决方法总结
    gitlab-cicd常见问题
    Angular服务器渲染常遇的坑
    一次页面性能问题排查的过程
    短链接生成工具(好用)
    MySQL分页查询
    2021精选 Java面试题附答案(一)
    CSRF攻击原理及防护
  • 原文地址:https://www.cnblogs.com/WX95/p/4600922.html
Copyright © 2020-2023  润新知