针对二维码优化摄像头

Optimise camera for QR codes

我有一个AVCaptureDevice专门用来扫描二维码(使用AVMetadataObjectTypeQRCode)。我的目标是尽可能快地扫描二维码。

可以在 iOS 中以编程方式调整 AVCaptureDevice 相机的多项设置(例如 focus and exposure)。

我可以对相机进行哪些优化,以最大限度地减少在 iPhone 上捕获二维码所需的时间?

我用的是AVCaptureDevice。 这段代码工作发现我在我的条形码中使用了这个 app.try 这个代码。

-(void)BarcodeStart
{

    _highlightView = [[UIView alloc] init];

    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;

    _highlightView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _highlightView.layer.borderWidth = 3;

    [barcameraView addSubview:_highlightView];

    _label = [[UILabel alloc] init];
    _label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
    _label.textColor = [UIColor whiteColor];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.text = @"(none)";
    [self.view addSubview:_label];
    //BackBtn UP side Show
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //[button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
    UIImageView *img = [[UIImageView alloc] init];
    button.frame = CGRectMake(3,19,30,30);
    img.image = [UIImage imageNamed:@"backBtnImg.png"];
    [button setImage:img.image forState:UIControlStateNormal];
    [_highlightView addSubview:button];
    //
    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input) {
        [_session addInput:_input];
    } else {
        NSLog(@"Error: %@", error);
    }

    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [_session addOutput:_output];

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    // _prevLayer.frame = CGRectMake(20, 70, 280, 280);
    _prevLayer.frame = barcameraView.bounds;
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [barcameraView.layer addSublayer:_prevLayer];

    [_session startRunning];

    [barcameraView bringSubviewToFront:_highlightView];
    [self.view bringSubviewToFront:_label];
}

大多数设置的最佳值因环境而异(例如 dark/bright 房间、near/far QR 码等),因此除非您了解用户的环境(例如如果使用该应用程序仅在工厂流水线上),默认值可能是最好的。

但是(根据this source), If you know that the QR code will be near the camera you can speed up autofocus by setting autoFocusRangeRestriction to a near range value. You could also make sure that smoothAutoFocusEnabled设置为false。