为什么使用新的iOS 7 API扫描条形码的速度真的很慢?

为什么使用新的iOS 7 API扫描条形码的速度真的很慢?,ios,objective-c,avcapturesession,avcapturedevice,Ios,Objective C,Avcapturesession,Avcapturedevice,我目前正试图使用iOS7最新的api来扫描代码39的条形码,但这让我抓狂。我必须以一种特定的方式保持手机静止10秒钟,以便它能够检测到它。我将它与红色激光、Zbar等进行了比较,他们可以在1秒内分析它,即使它有点歪斜。我不确定这是因为我加载捕获会话的方式还是什么原因。我很感激你的帮助。对如何提高绩效有何建议 以下是我如何在viewDidLoad方法中加载扫描仪: //Initialize Laser View laserView = [[UIView alloc] init];

我目前正试图使用iOS7最新的api来扫描代码39的条形码,但这让我抓狂。我必须以一种特定的方式保持手机静止10秒钟,以便它能够检测到它。我将它与红色激光、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];
以及:


这与图像质量(焦距、眩光、照明等)有很大关系。在非常好的条件下,你应该得到几乎是瞬时的扫描。

我建议你在代理函数
捕获输出:

您将看到,仅扫描一次条形码,就会多次调用它。初始化NSDateFormatter是一项非常昂贵的操作


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

我在AVCaptureSession中遇到了类似的问题,捕获速度非常慢,有时需要很长时间才能完成

我不知道我的解决方案是否对你有好处,但肯定会像我一样对其他寻找这个问题的人有帮助

AVCaptureSession *captureSession = [AVCaptureSession new];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;
使用此代码可以强制相机设置为高质量预设


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

试着放大一点。。。videoDevice.videoZoomFactor=2.0

您是否对时间进行了分析,以了解哪些代码行使您的应用程序变慢了?事实上,我经历了完全相反的结果。如果您有幸提高条形码检测的性能,我会得到什么样的结果。我也遇到了类似的结果,我很困惑。苹果的iOS原生条码扫描器是垃圾:为什么
AVCaptureSessionPresetPhoto
具有更好的分辨率
AVCaptureSession *captureSession = [AVCaptureSession new];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;