zXing(ios版)黑/白屏错误

时间:2012-10-12 12:40:18

标签: objective-c ios avfoundation zxing

我正在申请,使用zxing库来读取QR码。我有ZxingWidgetController的问题 - 当显示视图时,在应用程序处于后台/未激活期间(例如屏幕锁定)来自摄像机的图像未显示在屏幕上 - 只有背景可见,而扫描仪似乎不是工作。

当我再次调用initCapture方法时,显示来自相机的视频有点延迟,但在这种情况下,每次应用程序丢失活动时我都需要重新初始化扫描程序 - 这种行为根本不舒服。< / p>

这个bug几乎可以在所有使用zXing的aplication上重复,所以我想这是一些zXing bug。

zXing initCapture方法代码是:

- (void)initCapture {
#if HAS_AVFF
  AVCaptureDeviceInput *captureInput =
    [AVCaptureDeviceInput deviceInputWithDevice:
            [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] 
                                          error:nil];
    if(!captureInput)
    {
        NSLog(@"ERROR - CaptureInputNotInitialized");
    }


  AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
  captureOutput.alwaysDiscardsLateVideoFrames = YES;
    if(!captureOutput)
    {
        NSLog(@"ERROR - CaptureOutputNotInitialized");
    }


  [captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

  NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
  NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];

  NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];

  [captureOutput setVideoSettings:videoSettings]; 
  self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
  self.captureSession.sessionPreset = AVCaptureSessionPresetMedium; // 480x360 on a 4

    if([self.captureSession canAddInput:captureInput])
    {
        [self.captureSession addInput:captureInput];
    }
    else
    {
        NSLog(@"ERROR - cannot add input");
    }
    if([self.captureSession canAddOutput:captureOutput])
    {
        [self.captureSession addOutput:captureOutput];
    }
    else
    {
        NSLog(@"ERROR - cannot add output");
    }

  [captureOutput release];

  if (!self.prevLayer)
  {
      [self.prevLayer release];
  }
  self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];

  // NSLog(@"prev %p %@", self.prevLayer, self.prevLayer);
  self.prevLayer.frame = self.view.bounds;
  self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  [self.view.layer addSublayer: self.prevLayer];

  [self.captureSession startRunning];
#endif
}

也许你们知道出了什么问题?

1 个答案:

答案 0 :(得分:0)

我不明白你的问题。如果应用程序处于后台/不活动状态,当然它可以正常工作。你应该说清楚。