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:

バーコードを 1 回スキャンするだけで、何度も呼び出されることがわかります。NSDateFormatter の初期化は、NSDateFormatter の割り当てまたは初期化が「高価」と見なされるのはなぜですか? .

NSDateFormatter 関数が呼び出されるたびに作成するのではなく、デリゲート関数の外側を作成して再利用することをお勧めします。これにより、アプリの応答性が向上します。

于 2014-10-07T15:33:00.627 に答える