• 系统二维码扫描 扫描框设置 rectOfInterest


    #import "ViewController.h"

    #import <AVFoundation/AVFoundation.h>

    @interface ViewController ()<WJScanningViewdelegate,AVCaptureMetadataOutputObjectsDelegate>

    @property (nonatomic, strong) CIDetector *detecter;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];

        

        AVCaptureSession *session = [[AVCaptureSession alloc] init];

        [session addInput:input];

        [session addOutput:output];

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

        [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

        [output setRectOfInterest:CGRectMake(100/self.view.frame.size.height, 50/self.view.frame.size.width, 300/self.view.frame.size.height, 400/self.view.frame.size.width)];

        

        AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

        [layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

        [layer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        [self.view.layer addSublayer:layer];

        [session startRunning];

        

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];

        view.backgroundColor = [UIColor clearColor];

        view.layer.borderWidth = 1;

        view.layer.borderColor = [UIColor redColor].CGColor;

        [self.view addSubview:view];

        //调整扫描区域   ,有些资料说设置 rectOfInterest  

       //CGRectMake(y/deviceHeight, x/deviceWidth, height/deviceHeight, width/deviceWidth);

    解释下CGRectMake写法:都把x y width height 互换 了的.你扫一扫的那个框框的  起点坐标为 x  y 宽为width 高为height  ,deviceHeight ,deviceWidth指的是AVCaptureVideoPreviewLayer对象的高度,个人按这个方法弄但是有很多问题,效果不是很好。  

    //个人觉得还是下面比较容易接受,与以往设置frame一样。

      __weak typeof(self) weakSelf = self;

        [[NSNotificationCenter defaultCenter]addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification object:nil queue:[NSOperationQueue mainQueue]

        usingBlock:^(NSNotification * _Nonnull note) {

        if (weakSelf){

        AVCaptureMetadataOutput *output = session.outputs.firstObject;

        output.rectOfInterest = [layer metadataOutputRectOfInterestForRect:CGRectMake(50, 100, 200, 200)];

        }

            

        }];

        

    }

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

        

        //判断是否有数据

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

            AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

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

            //判断回传的数据类型

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

        

            }

        }

    }

  • 相关阅读:
    [Swift通天遁地]一、超级工具-(7)创建一个图文并茂的笔记本程序
    [Swift通天遁地]一、超级工具-(6)通过JavaScript(脚本)代码调用设备的源生程序
    [Swift通天遁地]一、超级工具-(5)使用UIWebView(网页视图)加载本地页面并调用JavaScript(脚本)代码
    [Swift通天遁地]一、超级工具-(4)使用UIWebView(网页视图)加载HTML和Gif动画
    [Swift通天遁地]一、超级工具-(3)带切换图标的密码文本框
    [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条
    将css 中的16进制颜色, 转化为 rgb格式
    Axure实现Tab选项卡切换功能
    UVA 10951
    No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).错误解决方法
  • 原文地址:https://www.cnblogs.com/wujie123/p/6782129.html
Copyright © 2020-2023  润新知