简体   繁体   English

如何在iPhone SDK中自动对焦?

[英]How to take a photo automatically at focus in iPhone SDK?

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. 我已经使用AVCaptureSession打开相机并放置了一个条形按钮,该按钮捕获了最新的帧并将其上载到服务器。 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. 谢谢。

You have to add adjustingFocus observer to your video input: 您必须在视频输入中添加adjustingFocus观察器:

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

and then add the observeValueForKeyPath method that takes a photo when adjusting focus is finished: 然后添加在完成焦点调整后拍摄照片的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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM