zoukankan      html  css  js  c++  java
  • 二维码+遮罩

    首先是要用到的几个类

    @property (strong,nonatomic)AVCaptureDevice * device;

    @property (strong,nonatomic)AVCaptureDeviceInput * input;

    @property (strong,nonatomic)AVCaptureMetadataOutput * output;

    @property (strong,nonatomic)AVCaptureSession * session;

    @property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;

    下面分别创建他们

    // Device

    _device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    // Input

    _input = [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];

    // Output

    _output = [[AVCaptureMetadataOutputalloc]init];

    [_outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

    // Session

    _session = [[AVCaptureSessionalloc]init];

    [_sessionsetSessionPreset:AVCaptureSessionPresetHigh];

    if ([_sessioncanAddInput:self.input])

    {

    [_sessionaddInput:self.input];

    }

    if ([_sessioncanAddOutput:self.output])

    {

    [_sessionaddOutput:self.output];

    }

    // 条码类型 AVMetadataObjectTypeQRCode

    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];

    // Preview

    _preview =[AVCaptureVideoPreviewLayerlayerWithSession:_session];

    _preview.videoGravity =AVLayerVideoGravityResizeAspectFill;

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

    [self.view.layerinsertSublayer:_previewatIndex:0];

    // Start

    [_sessionstartRunning];

    然后实现 AVCaptureMetadataOutputObjectsDelegate

    #pragma mark AVCaptureMetadataOutputObjectsDelegate

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

    {

     //判断是否有数据

        if (metadataObjects != nil && [metadataObjects count] > 0) {

            AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

    //        NSLog(@"------ 二维码数据 %@ %@",[metadataObj stringValue],metadataObjects);

    //        NSDictionary *dic = (NSDictionary *)[metadataObj stringValue];

            

            NSData *jsonData = [[metadataObj stringValue] dataUsingEncoding:NSUTF8StringEncoding];

            NSError *err;

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData

                                                                options:NSJSONReadingMutableContainers

                                                                  error:&err];

            

            NSLog(@"------ 二维码数据 dic %@",dic);

            NSLog(@"------ 二维码数据 code %@",[dic objectForKey:@"code"]);

            ScanManager *manager = [ScanManager manager];

            manager.tableDic = dic;

            NSLog(@" ======= tableDic %@",manager.tableDic);

            

            //判断回传的数据类型

            if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {

                [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];

                [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];

                _isReading = NO;

                [self dismissViewControllerAnimated:YES completion:nil];

            }

        }

    -(void)stopReading

    {

        [_session stopRunning];

        [_scanImg removeFromSuperview];

    }

     

     

     

    -------------------------- 遮罩

    #import <UIKit/UIKit.h>

    #define kMaskViewBorderWidth 2.0f

     

    @interface MaskView : UIView

     

    @property (unsafe_unretained, nonatomic)CGRect cropRect;

    @property (strong, nonatomic) UIColor *borderColor;

    @end

    #import "MaskView.h"

     

    @implementation MaskView

     

    - (void)drawRect:(CGRect)rect {

        [super drawRect:rect];

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.4);

        CGContextFillRect(ctx, self.bounds);

        

        CGContextSetStrokeColorWithColor(ctx, self.borderColor.CGColor);

        CGContextStrokeRectWithWidth(ctx, _cropRect, kMaskViewBorderWidth);

        

        CGContextClearRect(ctx, _cropRect);

    }

     

    @end

  • 相关阅读:
    程序员职业规划
    SSH框架优缺点
    Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
    什么是J2EE,包括哪些规范!
    JS中定义类的方法
    audio.js – 随时随地,播放 HTML5 的声音
    jquery面试题里 缓存问题如何解决?
    产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复
    JAVA排序算法
    java面试题中常见的关于String类问题总结
  • 原文地址:https://www.cnblogs.com/shifu/p/5505658.html
Copyright © 2011-2022 走看看