• 二维码+遮罩


    首先是要用到的几个类

    @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

  • 相关阅读:
    一个泛型栈类(GenericStack)
    Google Maps API v2初探
    浅谈工作中celery与Redis遇到的一些问题 PTSD
    python PTSD
    77%的Linux运维都不懂的内核问题
    教程 | Linux常用命令大全
    分布式架构系列: 负载均衡技术详解
    10大Python开源项目推荐(Github平均star2135)
    你必须学写 Python 装饰器的五个理由
    五分钟搞定 Linux 文档全部知识,就看这篇文章
  • 原文地址:https://www.cnblogs.com/shifu/p/5505658.html
Copyright © 2020-2023  润新知