美文网首页iOS点点滴滴
ios二维码扫描demo(微信二维码扫描音效)

ios二维码扫描demo(微信二维码扫描音效)

作者: 筱贰笔 | 来源:发表于2018-02-03 10:07 被阅读3227次

    非常感谢大家利用自己宝贵的时间来阅读我的文章 ,  今天给大家带来的是一个原生二维码扫描的demo,很久之前写的代码,最近项目要用,想着以后用到的可能性也挺大的,就翻出来整理了个demo出来,如果需要的可以做个参考。如果需要的话希望能帮到你 , 当然, 有任何不妥的地方 欢迎指正。喜欢的可以关注一下我的简书我的博客

    老规矩,先上效果展示

    卧槽,这个gif为何如此之大。。。不管了,上demo地址ZQScanTool

    用法很简单,把ZQScanTool文件夹拖入到项目中,在需要跳转扫描的时候把ScanViewController给Push出来

    ScanViewController *vc = [[ScanViewController alloc] init];

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

    把扫描结果展示控制器ScanResultViewController换成你的调转页

    主要代码:

    1、相机设置

    - (void)instanceDevice{

        line_tag = 1872637;

        //获取摄像设备

        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()];

        //初始化链接对象

        session = [[AVCaptureSession alloc]init];

        //高质量采集率

        [session setSessionPreset:AVCaptureSessionPresetHigh];

        if (input) {

            [session addInput:input];

        }

        if (output) {

            [session addOutput:output];

            //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

            NSMutableArray *a = [[NSMutableArray alloc] init];

            if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) {

                [a addObject:AVMetadataObjectTypeQRCode];

            }

            if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) {

                [a addObject:AVMetadataObjectTypeEAN13Code];

            }

            if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) {

                [a addObject:AVMetadataObjectTypeEAN8Code];

            }

            if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) {

                [a addObject:AVMetadataObjectTypeCode128Code];

            }

            output.metadataObjectTypes=a;

        }

        AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];

        layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

        layer.frame=self.view.layer.bounds;

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

        [self setOverlayPickerView];

        [session addObserver:self forKeyPath:@"running" options:NSKeyValueObservingOptionNew context:nil];

        //开始捕获

        [session startRunning];

    }

    2、扫描UI

    - (void)setOverlayPickerView

    {

        //左侧的view

        UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, ScreenHeight)];

        leftView.alpha = 0.5;

        leftView.backgroundColor = [UIColor blackColor];

        [self.view addSubview:leftView];

        //右侧的view

        UIImageView *rightView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth-30, 0, 30, ScreenHeight)];

        rightView.alpha = 0.5;

        rightView.backgroundColor = [UIColor blackColor];

        [self.view addSubview:rightView];

        //最上部view

        UIImageView* upView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, ScreenWidth - 60, (self.view.center.y-(ScreenWidth-60)/2))];

        upView.alpha = 0.5;

        upView.backgroundColor = [UIColor blackColor];

        [self.view addSubview:upView];

        UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 44, 44)];

        [cancleBtn setImage:[UIImage imageNamed:@"nav_backButton_image"] forState:UIControlStateNormal];

        [cancleBtn addTarget:self action:@selector(cancleBtnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:cancleBtn];

        //底部view

        UIImageView * downView = [[UIImageView alloc] initWithFrame:CGRectMake(30, (self.view.center.y+(ScreenWidth-60)/2), (ScreenWidth-60), (ScreenHeight-(self.view.center.y-(ScreenWidth-60)/2)))];

        downView.alpha = 0.5;

        downView.backgroundColor = [UIColor blackColor];

        [self.view addSubview:downView];

        UIImageView *centerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-60, ScreenHeight-60)];

        centerView.center = self.view.center;

        centerView.image = [UIImage imageNamed:@"scan_circle"];

        centerView.contentMode = UIViewContentModeScaleAspectFit;

        centerView.backgroundColor = [UIColor clearColor];

        [self.view addSubview:centerView];

        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(upView.frame), ScreenWidth-60, 2)];

        line.tag = line_tag;

        line.image = [UIImage imageNamed:@"scan_line"];

        line.contentMode = UIViewContentModeScaleAspectFill;

        line.backgroundColor = [UIColor clearColor];

        [self.view addSubview:line];

        UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMinY(downView.frame), ScreenWidth-60, 60)];

        msg.backgroundColor = [UIColor clearColor];

        msg.textColor = [UIColor whiteColor];

        msg.textAlignment = NSTextAlignmentCenter;

        msg.font = [UIFont systemFontOfSize:16];

        msg.text = @"将二维码放入框内,即可自动扫描";

        [self.view addSubview:msg];

    }

    3、扫描动画

    /**

    *

    *  监听扫码状态-修改扫描动画

    *

    */

    - (void)observeValueForKeyPath:(NSString *)keyPath

                          ofObject:(id)object

                            change:(NSDictionary *)change

                          context:(void *)context{

        if ([object isKindOfClass:[AVCaptureSession class]]) {

            BOOL isRunning = ((AVCaptureSession *)object).isRunning;

            if (isRunning) {

                [self addAnimation];

            }else{

                [self removeAnimation];

            }

        }

    }

    *  添加扫码动画

    - (void)addAnimation{

        UIView *line = [self.view viewWithTag:line_tag];

        line.hidden = NO;

        CABasicAnimation *animation = [ScanViewController moveYTime:2 fromY:[NSNumber numberWithFloat:0] toY:[NSNumber numberWithFloat:ScreenWidth-60-2] rep:OPEN_MAX];

        [line.layer addAnimation:animation forKey:@"LineAnimation"];

    }

     *去除扫码动画

    - (void)removeAnimation{

        UIView *line = [self.view viewWithTag:line_tag];

        [line.layer removeAnimationForKey:@"LineAnimation"];

        line.hidden = YES;

    }

    + (CABasicAnimation *)moveYTime:(float)time fromY:(NSNumber *)fromY toY:(NSNumber *)toY rep:(int)rep

    {

        CABasicAnimation *animationMove = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

        [animationMove setFromValue:fromY];

        [animationMove setToValue:toY];

        animationMove.duration = time;

        animationMove.delegate = self;

        animationMove.repeatCount  = rep;

        animationMove.fillMode = kCAFillModeForwards;

        animationMove.removedOnCompletion = NO;

        animationMove.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        return animationMove;

    }

    3、扫描结果

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

        if (metadataObjects.count>0) {

            [session stopRunning];

            NSURL *url=[[NSBundle mainBundle]URLForResource:@"scanSuccess.wav" withExtension:nil];

                //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)

                SystemSoundID soundID=8787;

                AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);

                //3.播放音效文件

                //下面的两个函数都可以用来播放音效文件,第一个函数伴随有震动效果

                AudioServicesPlayAlertSound(soundID);

            AudioServicesPlaySystemSound(8787);

            AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex :0];

            //输出扫描字符串

            NSString *data = metadataObject.stringValue;

            ScanResultViewController *resultVC = [[ScanResultViewController alloc] init];

            resultVC.title = @"扫描结果";

            resultVC.result = data;

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

        }

    }

    @#%¥¥#&……¥#@!#!@¥@#%¥¥!@#!@#!恩,就这么多了。好用别忘了点赞!

    相关文章

      网友评论

        本文标题:ios二维码扫描demo(微信二维码扫描音效)

        本文链接:https://www.haomeiwen.com/subject/ssoezxtx.html