2016-11-16 63 views
0

真正的产品条码的条码扫描仪工作在Mac屏幕上,而不是真实的产品,如美极等AVCapturesession不工作的iOS中

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _highlightView = [[UIView alloc] init]; 
    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 
    _highlightView.layer.borderColor = [UIColor greenColor].CGColor; 
    _highlightView.layer.borderWidth = 3; 
    [self.view 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]; 

    _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 = self.view.bounds; 
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [self.view.layer addSublayer:_prevLayer]; 

    [_session startRunning]; 

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

是否有问题?我使用的大部分代码来自avcapturesession,AVmetadataoutput等,但它只读取液晶屏幕。

回答

0

检查你的_output.metadataObjectTypes,你必须添加所有类型的条码。例如(EAN8,EAN13 ..... e.t.c)

+0

感谢萨格尔,但其也被尝试。但它从LCD屏幕读取所有格式,但不是在真实世界中。 –

+0

_output = [[AVCaptureMetadataOutput alloc] init]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [_session addOutput:_output]; _output.metadataObjectTypes = [_output availableMetadataObjectTypes]; //这里你在数组中添加所有类型的条形码 –

+0

做了同样的sagar。 –

1

创建AVCaptureMetadataOutputObjectsDelegate像

- (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; 
     } 
    } 
} 
} 
+0

是的。谢谢大卫。但之前已经完成。只在显示屏上工作。 –