4

我目前正在尝试使用 iOS 7 的最新 api 来扫描代码 39 条码,但这让我发疯。我必须以特定的方式握住手机 10 秒钟,以便它检测到它。我将它与 Red Laser、Zbar 等进行了比较,即使它有点歪斜,他们也可以在 1 秒内对其进行分析。我不确定这是因为我加载捕获会话的方式还是什么。我会很感激帮助。关于如何提高性能的任何建议?

这是我在 viewDidLoad 方法中加载扫描仪的方法:

    //Initialize Laser View
    laserView = [[UIView alloc] init];
    laserView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    laserView.layer.borderColor = [UIColor redColor].CGColor;
    laserView.layer.borderWidth = 8;
    laserView.layer.cornerRadius = 10;
    [self.view addSubview:laserView];

    //Start Session
    scannerSession = [[AVCaptureSession alloc] init];
    scannerDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Define Error Messages
    NSError *error = nil;

    //Define Input
    scannerInput = [AVCaptureDeviceInput deviceInputWithDevice:scannerDevice error:&error];

    //Check if Device has a Camera
    if (scannerInput) {
        [scannerSession addInput:scannerInput];
    } else {
        NSLog(@"Error: %@", error);
    }

    // Locks the configuration
    BOOL success = [scannerDevice lockForConfiguration:nil];
    if (success) {
        if ([scannerDevice isAutoFocusRangeRestrictionSupported]) {

            // Restricts the autofocus to near range (new in iOS 7)
            [scannerDevice setAutoFocusRangeRestriction:AVCaptureAutoFocusRangeRestrictionNear];
        }
    }
    // unlocks the configuration
    [scannerDevice unlockForConfiguration];

    //Define Output & Metadata Object Types
    scannerOutput = [[AVCaptureMetadataOutput alloc] init];
    [scannerOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [scannerSession addOutput:scannerOutput];
    scannerOutput.metadataObjectTypes = [scannerOutput availableMetadataObjectTypes];

    //Create Video Preview Layer
    scannerPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:scannerSession];
    scannerPreviewLayer.frame = self.view.bounds;
    scannerPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:scannerPreviewLayer];

    //Start Session
    [scannerSession startRunning];
    [self.view bringSubviewToFront:cancelButton];
    [self.view bringSubviewToFront:laserView];

和:

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

    //Prepare Laser View
    CGRect laser = CGRectZero;

    //Format Date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"M/d"];

    //Format Time
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setDateFormat:@"h:ma"];

    //Define Barcode Types to Recognize
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *idNumber = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code];

    if ([metadataObjects count] > 1) {

        NSLog(@"%lu Barcodes Found.", (unsigned long)[metadataObjects count]);

    }

    //Get String Value For Every Barcode (That Matches The Type We're Looking For)
    for (AVMetadataObject *metadata in metadataObjects) {


        for (NSString *type in barCodeTypes) {


            //If The Barcode Is The Type We Need Then Get Data
            if ([metadata.type isEqualToString:type]) {

                barCodeObject = (AVMetadataMachineReadableCodeObject *)[scannerPreviewLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                laser = barCodeObject.bounds;
                idNumber = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }

        // If IDNumber Found
        if (idNumber != nil) {

            //Stop Session
            [scannerSession stopRunning];
            [self vibrate];

            NSLog(@"ID: %@", idNumber);

            break;
        }

        //If IDNumber Is Not Found
        else {

            NSLog(@"No ID Found.");
        }
    }

    //Update Laser
    laserView.frame = laser;
}
4

4 回答 4

1

我对 AVCaptureSession 也有类似的问题,捕获非常慢,有时需要很长时间才能完成。

不知道我的解决方案是否对您有好处,但绝对可以帮助其他寻找此问题的人,就像我一样。

AVCaptureSession *captureSession = [AVCaptureSession new];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;

使用此代码,您可以强制相机进行高质量预设。

希望这会对某人有所帮助。

于 2015-09-09T15:23:01.427 回答
1

尝试放大一点... videoDevice.videoZoomFactor = 2.0;

于 2015-10-07T22:34:57.997 回答
0

我建议你 NSLog 在你的委托函数中放一个 captureOutput:

你会看到它被调用了很多次,只是为了扫描一次条形码。初始化 NSDateFormatter 是一项非常昂贵的操作,为什么分配或初始化 NSDateFormatter 被认为是“昂贵的”?.

我建议您创建 NSDateFormatter 委托函数的外部并重新使用它,而不是每次调用函数时都创建它。这应该使您的应用程序更具响应性。

于 2014-10-07T15:33:00.627 回答
0

这在很大程度上与图像质量(焦点、眩光、照明等)有关。在非常好的条件下,您应该获得几乎瞬时的扫描。

于 2014-03-07T18:39:14.627 回答