objective-c - iPhone SDK 中如何实现对焦自动拍照?

标签 objective-c ios4 avcapturesession avcapturedevice

我正在创建一个应用程序,用户可以在其中拍摄带有文本的图像并上传到服务器。我使用 AVCaptureSession 打开相机并放置了一个条形按钮,用于捕获最新帧并将其上传到服务器。在此应用中,用户可以通过单击条形按钮将多张图片一张一张地发送到服务器。

我想知道是否有可能用户不必按下按钮来捕获帧。是否有可能使用自动对焦自动捕获当前帧?就像用户只需将相机放在图像上一秒钟,当图像对焦良好时,它就会自动拍摄,这样用户就可以移动到下一张图像,而无需按任何按钮?

我捕获帧的代码如下:

- (void)initCapture {

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput 
                                          deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] 
                                          error:nil];

    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];

    captureOutput.alwaysDiscardsLateVideoFrames = YES; 







    /*We create a serial queue to handle the processing of our frames*/
    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [captureOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);
    // Set the video output to store frame in BGRA (It is supposed to be faster)
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
    [captureOutput setVideoSettings:videoSettings]; 
    /*And we create a capture session*/
    captureSession = [[AVCaptureSession alloc] init];
    /*We add input and output*/
    [captureSession addInput:captureInput];
    [captureSession addOutput:captureOutput];





    AVCaptureConnection *conn = [captureOutput connectionWithMediaType:AVMediaTypeVideo];

    if (conn.supportsVideoMinFrameDuration)
        conn.videoMinFrameDuration = CMTimeMake(5,1);
    if (conn.supportsVideoMaxFrameDuration)
        conn.videoMaxFrameDuration = CMTimeMake(5,1);





    [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];

    customLayer = [CALayer layer];
    customLayer.frame = self.view.bounds;
    customLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    customLayer.contentsGravity = kCAGravityResizeAspectFill;
    //[self.view.layer addSublayer:customLayer];
    /*We add the imageView*/



    imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    //imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];



    [captureSession startRunning];

}

我会很感激你的帮助。 谢谢。

最佳答案

您必须将 adjustingFocus 观察器添加到您的视频输入中:

[[[captureManager videoInput] device] addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];

然后添加observeValueForKeyPath方法,调焦完成后拍照:

- (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] ];

    if (!adjustingFocus) {
             [[self captureManager] captureStillImage:self];
        }
    }
}

关于objective-c - iPhone SDK 中如何实现对焦自动拍照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11423538/

相关文章:

iphone - 如何在不使用 AudioQueueRef 的情况下设置 AudioQueue 中的音量?

iphone - 让 iOS 签名的应用程序在越狱设备上执行未签名的应用程序

iphone - 多次按下时声音重叠

ios - 检查 bool 是否为真 x 秒 - Swift

ios - 来自 AVCaptureMetadataOutput 委托(delegate)的 UIImage (didOutputMetadataObjects)

ios - 修改曝光持续时间并返回 AVCaptureExposureModeContinuousAutoExposure 后的奇怪行为

ios - iOS:主线程检查器:在后台线程上调用的UI API:-[UIView keepCount]

ios - 单击自定义 uitableviewcell 中的一个 uibutton 会选择 uitableview 中的多个 uibutton

ios - 精确到百分之一秒的时间 float - Objective C

iphone - 如何使用 iPhone SDK 获取网络上的计算机或设备列表及其 IP 地址和名称?