我試圖使用CIDetector
製作面部檢測器,只要檢測到面部就啓用按鈕。我搜索和找不到的部分是如何在代碼檢測到人臉時觸發一個函數。當臉部離開相機框時禁用它。使用iOS CIDetector面部檢測功能觸發功能
這裏是我到現在爲止代碼:
.h文件中:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *actionButton;
//Update 2:
@property (weak, nonatomic) IBOutlet UIView *containerView;
- (IBAction)actionButton:(id)sender;
@end
.m文件:
#import "ViewController.h"
@import AVFoundation;
@interface ViewController() <AVCaptureMetadataOutputObjectsDelegate> {
AVCaptureVideoPreviewLayer *_previewLayer;
AVCaptureSession *_session;
CIDetector *_faceDetector;
CIContext *_ciContext;
}
@end
@implementation SCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a new AVCaptureSession
_session = [[AVCaptureSession alloc] init];
[_session setSessionPreset:AVCaptureSessionPreset640x480];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if(input) {
// Add the input to the session
[_session addInput:input];
} else {
NSLog(@"error: %@", error);
return;
}
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
// Have to add the output before setting metadata types
[_session addOutput:output];
// Restrict the output metadata to faces
[output setMetadataObjectTypes:@[AVMetadataObjectTypeFace]];
// This VC is the delegate. Please call us on the main queue
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Display on screen
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.bounds = self.view.bounds;
_previewLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
// Update 2 change
[self.containerView.layer addSublayer:_previewLayer];
// Hide the button
self.actionButton.hidden = YES;
// Start the AVSession running
[_session startRunning];
}
// Update 1:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
for(AVMetadataObject *metadataObject in metadataObjects) {
if([metadataObject.type isEqualToString:AVMetadataObjectTypeFace]) {
self.retakeButton.hidden = NO;
}
}
}
- (IBAction)actionButton:(id)sender {
}
@end
你檢查AVCaptureMetadataOutput委託方法? ' - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)來自連接的元數據對象:(AVCaptureConnection *)連接'這個sohuld會在檢測到face後調用。這裏有更多的信息:http://stackoverflow.com/questions/18995236/ios-7-avcapturemetadataoutput-delegate-qrcode-scanner – 2015-01-15 15:25:57
我已經添加了建議的功能,但它似乎什麼都不做。 – 2015-01-15 15:40:12
@BiancaIoana你可能不得不做'self.retakeButton.hidden = NO;'在UI線程上。 – Kiran 2015-01-15 15:43:04