如何在captureStillImageAsynchronouslyFromConnection方法中实现聚焦

3

我正在尝试在iOS上拍照。

我使用方法captureStillImageAsynchronouslyFromConnection尝试拍摄照片,但出现了问题。

我已经实现了该方法并且它可以正常工作,但我的问题是它会立即拍摄照片而不等待对焦。

我真的很努力地寻找答案,但始终未果。

我已经像这样实现了预览视频:

session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = cameraImage.frame;
captureVideoPreviewLayer.bounds = cameraImage.bounds;
captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
[cameraImage.layer addSublayer:captureVideoPreviewLayer];
 device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


 NSError *error = nil;


if ([session canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
    session.sessionPreset = AVCaptureSessionPreset1920x1080;
}

[session addInput:input];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey,nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];

[session startRunning];

当按下按钮时,我有:

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
    for (AVCaptureInputPort *port in [connection inputPorts])
    {
        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}


[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     if (exifAttachments)
     {
     }
     else{}


     // TAKE IMAGE


    if ([device lockForConfiguration:&error]) {
        [device setFocusPointOfInterest:CGPointMake(0.5f, 0.f)];
        [device setExposurePointOfInterest:CGPointMake(0.5f, 0.f)];

        [device setFocusMode:AVCaptureFocusModeAutoFocus];
        [device unlockForConfiguration];
    }
    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];
     UIImage *rotatedImage = [[UIImage alloc]initWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationDown];

     [session stopRunning];

很遗憾,拍摄的照片并没有聚焦。
有人可以帮助我解决问题吗?
Marko

有人知道如何修复这个问题,或者至少知道在哪里查找吗? - user1570600
@Marco - 你最终得到解决这个问题的帮助了吗? - Patricia
1个回答

1

您希望观察调整焦距的值并在其完成后拍照。首先,在尝试捕获图像之前,我通常会检查设备是否正在对焦,并添加一个观察器以替代。

if ([device isAdjustingFocus])
{
    [device addObserver: self forKeyPath: @"adjustingFocus" options: NSKeyValueObservingOptionNew context:nil ];
}
else
{
    // You can just take the image if the device isn't adjusting
}

然后创建一个观察值变化的方法:
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString: @"adjustingFocus"])
    {
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );

        //
        // If not adjusting focus then call
        // captureStillImageAsynchronouslyFromConnection here
        if (adjustingFocus == NO)
        {
            // Remove the observer when we're finished
            [device removeObserver: self forKeyPath: @"adjustingFocus" ];

        }
    }
}

希望这有所帮助...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接