如何在iPhone SDK中自动对焦? [英] How to take a photo automatically at focus in iPhone SDK?

查看:110
本文介绍了如何在iPhone SDK中自动对焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating an app in which user takes the picture of a image with text and upload to server. I have used AVCaptureSession to open camera and placed a bar button which captures the latest frame and uploads it to the server. In this app, user can send multiple images to the server one by one by clicking the bar button.

我想知道是否有可能用户不必按捕获帧的按钮。是否可以通过自动对焦自动捕获当前帧?就像用户只是将相机放在图像上一秒钟,然后在图像聚焦良好时,图像会自动捕获,以便用户可以移动到下一张图像而无需按下任何按钮?

I was wondering if it was possible that user does not have to press the button to capture a frame. Is this possible that current frame is captured automatically with auto focus? Like user just places the camera for a second on the image and when the image is well-focused, it is automatically captured so that user can move to next image without having to press any button?

我捕获框架的代码如下:

My code to capture frame is as follows:

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

}

感谢您的帮助。
谢谢。

I will be grateful for your help. Thanks.

推荐答案

您必须添加 adjustingFocus 观察者到您的视频输入:

You have to add adjustingFocus observer to your video input:

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

然后添加 observeValueForKeyPath 方法调整焦点完成后的照片:

and then add the observeValueForKeyPath method that takes a photo when adjusting focus is finished:

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

这篇关于如何在iPhone SDK中自动对焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆