前言-基于Vision和CIDetector
iOS开发中有需要让儿童保持观看距离的功能,护眼模式。
举例
大概思路
- 通过手机的摄像头小孔成像原理参数
- 将脸部的截取图片进行截取和转向
- iOS10以上用Vision,否则用CIDetector 进行识别距离
API
+ (BOOL)isSupported;
- (void)setCompleteBlock:(FDFaceDistanceBlock)block;
- (void)start;
- (void)stop;
- (void)pause;
- (void)resume;
使用
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.label];
__weak typeof(self) weakSelf = self;
[FDFaceToCameraDistance startWithConfiguration:[self createFDConfig] detectBlock:^(BOOL success, NSUInteger distance) {
NSLog(@"face distance %ld", distance);
if(distance > 100) {
NSLog(@"has not get you face");
} else {
weakSelf.label.text = [NSString stringWithFormat:@"face distance is %ld", distance];
}
}];
}
- (FDConfiguration *)createFDConfig {
FDConfiguration *config = [[FDConfiguration alloc] init];
config.repeatCount = NSIntegerMax;
config.firstRepeatDelay = 5.0;
config.timeInterval = 2.0;
return config;
}