[英]How to take a photo automatically at focus in iPhone SDK?
我正在創建一個應用程序,其中用戶用文本拍攝圖像圖片並上傳到服務器。 我已經使用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];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.