相机未聚焦在运行iOS 7.1的iPhone 4上



iOS从7.0.6升级到7.1.0后,我们遇到了麻烦。我在运行iOS 7.1的iPhone 4s、5、5c和5s上都没有看到这个问题。我正在张贴相机初始化代码:

- (void)initCapture
{
    //Setting up the AVCaptureDevice (camera)
    AVCaptureDevice* inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError* cameraError;
    if ([inputDevice lockForConfiguration:&cameraError])
    {
        if ([inputDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
        {
            NSLog(@"AVCaptureDevice is set to video with continuous auto focus");
            CGPoint autofocusPoint = CGPointMake(0.5f, 0.5f);
            [inputDevice setFocusPointOfInterest:autofocusPoint];
            [inputDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
        }
        [inputDevice unlockForConfiguration];
    }
    //setting up the input streams
    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil];
    //setting up up the AVCaptureVideoDataOutput
    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
    captureOutput.alwaysDiscardsLateVideoFrames = YES;
    [captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    //setting up video settings
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    //passing the settings to the AVCaptureVideoDataOutput
[captureOutput setVideoSettings:videoSettings];
    //setting up the AVCaptureSession
    captureSession = [[AVCaptureSession alloc] init];
    captureSession.sessionPreset = AVCaptureSessionPresetMedium;
    [captureSession addInput:captureInput];
    [captureSession addOutput:captureOutput];
    if (!prevLayer)
{
        prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    }
    NSLog(@"initCapture preview Layer %p %@", self.prevLayer, self.prevLayer);
    self.prevLayer.frame = self.view.bounds;
    self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer: self.prevLayer];
    [self.captureSession startRunning];
}

任何帮助都将不胜感激。。。

您使用的苹果提供的代码已经过时了,他们现在已经完全重写了。我会试试运气,选择新的工作流程。

在这里查看。

为了结束这个线程,除了libzxing之外,我们还使用相机扫描二维码。我们决定实现本机iOS 7.0 AVCaptureMetadataOutputObjectsDelegate,而不是旧的AVCaptureVideoDataOutputSampleBufferDelegate。元数据委托更简单、更干净,我们在http://nshipster.com/ios7/非常有帮助。

以下是诊断问题的一些想法:

  • 您没有if ([inputDevice lockForConfiguration:&cameraError])的其他情况。添加一个
  • 在其他情况下,记录cameraError中包含的错误
  • 您没有if ([inputDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])的其他情况。添加一个;记录,或者在那里添加一个断点以在调试中进行测试
  • 在尝试setFocusPointOfInterest之前,您不会检查属性focusPointOfInterestSupported的返回值。考虑在setFocusPointOfInterest之前调用setFocusMode(不确定这是否重要,但这就是我所拥有的)
  • 通常,您可能需要在尝试锁定配置之前进行所有检查

neuman8的评论指出libzxing中的某些东西阻碍了重新聚焦,之后我自己进行了一些调查

我发现Decoder.mm文件中的以下行是罪魁祸首。

ArrayRef<char> subsetData (subsetBytesPerRow * subsetHeight);

ArrayRef似乎是zxing/common/Array.h文件中的一个类,它试图分配指定大小的数组。它似乎没有做错什么,但我猜测大约170k个char元素数组的分配可能需要一些时间,这是导致阻塞调用速度减慢到足以阻止其他线程运行的罪魁祸首

所以,我试着用一个蛮力的解决方案来检验这个假设。我刚分配完就加了一个睡眠。

[NSThread sleepForTimeInterval:0.02];

相机再次开始对焦,并能够破译二维码

我仍然无法找到更好的方法来解决这个问题。有没有人能够更有效地分配大阵列,或者有更优雅的方式来产生相机焦点的线程
否则,这应该暂时解决问题,即使它很丑陋。

最新更新