4

Zxingを使用しているアプリケーションがあります。ユーザーがスキャナーページに入ると、「スキャン」ボタンを押すことなく、zxingが即座に初期化されます。わかりましたので、次のような問題に直面し ていました。残念ながら、ユーザーが設定ページに移動して (キャプチャを停止している)、スキャナ ページにすばやく戻る (キャプチャを開始している) などのクイック アクションを実行すると、アプリケーションがクラッシュします。

カスタムキューの非同期ディスパッチャーでカメラをかがめ、同じキューの同期ディスパッチャーで初期化することでこれを解決しました。カメラが停止する前に初期化が呼び出されるまで、正常に動作します。この場合、初期化コードはカメラ停止ブロックの最後で待機していますが、この 2 番目のコードには約 10 秒かかりました! (通常1~2日程度かかります)。何が起こっているのかわかりません。

何か提案はありますか?

コードは次のとおりです。

    - (void)stopCapture
{
#if HAS_AVFF
  decoding = NO;
 [self.prevLayer removeFromSuperlayer];

    dispatch_async(myQueue, ^{
        NSLog(@"stop capture");
      [captureSession stopRunning];
        NSLog(@"session Stopped");
        if(captureSession.inputs.count>0)
        {
              AVCaptureInput* input = [captureSession.inputs objectAtIndex:0];
              [captureSession removeInput:input];
        }
        if(captureSession.outputs.count>0)
        {
              AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[captureSession.outputs objectAtIndex:0];
              [captureSession removeOutput:output];
        }

  self.prevLayer = nil;
  self.captureSession = nil;

        NSLog(@"end stop capture");
    });

#endif
}

- (void)initCapture {
dispatch_sync(myQueue, ^{
    NSLog(@"init capture");
    #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];

      self.prevLayer.frame = self.view.bounds;
      self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      [self.view.layer addSublayer: self.prevLayer];

      [self.captureSession startRunning];
    #endif
    NSLog(@"end init");
    });

}

4

0 に答える 0