使用AVFoundation框架进行条形码扫描的错误结果

时间:2014-05-30 07:26:50

标签: ios ios7 avfoundation barcode barcode-scanner

我正在使用iOS 7 AVFoundation框架进行条形码扫描。它适用于我,但在某些条形码上它给我错误的结果。

我正在添加代码文件和条形码以生成问题。请看看并帮助我找出问题所在。

代码示例:

ViewController.h类 -

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVCaptureMetadataOutputObjectsDelegate>
{
    AVCaptureSession                *_session;
    AVCaptureDevice                 *_device;
    AVCaptureDeviceInput            *_input;
    AVCaptureMetadataOutput         *_output;
    AVCaptureVideoPreviewLayer      *_prevLayer;

    UIView                          *_highlightView;

    IBOutlet    UIView              *_viewBg;
    IBOutlet    UIButton            *_btnScanning;
}

@end

ViewController.m类方法 -

- (void)setScanningVideoOrientation
{
    if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait)
    {
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
    }
    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
    }
    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
    {
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
    }
    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight)
    {
        _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    }
}


- (void)startScanning
{
    // Create Session.------------------------------------------------------
    _session = nil;
    _session = [[AVCaptureSession alloc] init];
    _device = nil;
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    _input = nil;
    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input)
    {
        [_session addInput:_input];

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

        _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

        _prevLayer = nil;
        _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
        _prevLayer.frame = _viewBg.frame;
        _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [self setScanningVideoOrientation];
        [self.view.layer addSublayer:_prevLayer];

        [_session startRunning];
    } else
    {
        NSLog(@"Error: %@", error);
    }
    //----------------------------------------------------------------------
}

在此委托方法中。我的条形码字符串错了。请查看&#39; detectionString&#39;。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray     *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,     AVMetadataObjectTypeCode39Mod43Code,
                          AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
                          AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

    for (AVMetadataObject *metadata in metadataObjects) {
        for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type])
            {
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer     transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }

        if (detectionString != nil)
        {
            NSLog(@"ViewController-->captureOutput-->detectionString = %@",detectionString);
            [self stopScanning];
            break;
        }
    }

    barCodeObject   = nil;
    detectionString = nil;
    barCodeTypes    = nil;
}

条形码图片 -

enter image description here

我收到了结果 - 0649954006029

但它应该是 - 649954006029。

至于其他一些条形码,我看到了&#39; l&#39;在实际条形码扫描字符串之前。

希望它能帮助您找出问题。

感谢。

1 个答案:

答案 0 :(得分:3)

您获得13位数代码的原因是因为您正在尝试扫描UPC-A代码(649954 ..)而iOS没有针对UPC-A代码的单独类别。

UPC-A代码以EAN13代码的形式扫描,长度为13位,因此它们的前缀为0。

来自AVFoundation/AVMetadata.h

  

/ *! @constant AVMetadataObjectTypeEAN13Code

     

@abstract一个实例的标识符   具有类型的AVMetadataMachineReadableCodeObject   AVMetadataObjectTypeEAN13Code。

     

@discussion       从EAN-13(包括UPC-A)代码生成的AVMetadataMachineReadableCodeObject对象将返回此常量作为其类型。 * /

AVF_EXPORT NSString *const AVMetadataObjectTypeEAN13Code NS_AVAILABLE(NA, 7_0);