// // LPBeautySetVC.m // zhimi // // Created by 张灿 on 2019/1/17. // Copyright © 2019年 caiPro. All rights reserved. // #import "LPBeautySetVC.h" #import "FUCamera.h" #import #import "PFAPIDemoBar.h" #import "PFDateHandle.h" #import "FUOpenGLView.h" @interface LPBeautySetVC() { dispatch_semaphore_t signal; dispatch_semaphore_t semaphore; UIView *localCameraVideoView; } @property (nonatomic, strong) FUCamera *mCamera ; @property (nonatomic,assign) BOOL needSave; //是否需要保存 @property (nonatomic,strong) UIView* botView; @property (nonatomic,strong) UIButton* btnSwitch; @property (nonatomic,assign) BOOL beautyOpen;//美颜是否开启 @property (strong, nonatomic) FUOpenGLView *renderView; @property(nonatomic, strong) PFAPIDemoBar *beautyEditView; @property (nonatomic, strong) NSUserDefaults *def; @property (nonatomic,strong) SMPixelFree *mPixelFree; @property (nonatomic,assign)BOOL clickCompare; @property (nonatomic,strong) UILabel *titleLabel; @end @implementation LPBeautySetVC - (BOOL)prefersStatusBarHidden { return YES; } -(FUCamera *)mCamera { if (!_mCamera) { _mCamera = [[FUCamera alloc] init]; _mCamera.delegate = self ; // _mCamera.dataSource = self; } return _mCamera ; } -(FUOpenGLView *)renderView { if (!_renderView) { _renderView = [[FUOpenGLView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)]; } return _renderView ; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [UIApplication sharedApplication].idleTimerDisabled = YES; [self.mCamera startCapture]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [UIApplication sharedApplication].idleTimerDisabled = YES; [self.mCamera resetFocusAndExposureModes]; [self.mCamera stopCapture]; } -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // 适配 iPhone X } -(void)dealloc{ NSLog(@"dealloc---"); } -(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; [self.view addSubview:self.renderView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchScreenAction:)]; [self.renderView addGestureRecognizer:tap]; self.def = [NSUserDefaults standardUserDefaults]; [self initPixelFree]; [self setDefaultParam]; [self.mCamera setExposureValue:0]; [self.mCamera startCapture]; signal = dispatch_semaphore_create(1); [self addObserver]; [self setupUI]; //v1.1.2 增加相机权限判断 [UCAuthorityManager cameraAuthority:^{ } denied:^{ UIAlertController *systemAlert = [UIAlertController alertControllerWithTitle:@"请在iphone的“设置-隐私-相机”选项中,允许APP访问您的相机。" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"现在去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } else { // 去系统设置页面 if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; } else { // Fallback on earlier versions } } }]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [systemAlert addAction:cancelAction]; [systemAlert addAction:action]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController: systemAlert animated: YES completion: nil]; }); }]; } - (void)touchScreenAction:(UITapGestureRecognizer *)tap { if (tap.view == self.renderView) { self.botView.hidden = NO; self.beautyEditView.hidden = YES; } } - (void)setupUI{ self.beautyOpen = YES; self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, StatusBarHeight + 12.0f, KScreenWidth, 20)]; self.titleLabel.textAlignment = NSTextAlignmentCenter ; self.titleLabel.textColor = [UIColor whiteColor]; self.titleLabel.text = @"澜极美颜设置"; self.titleLabel.font = [UIFont boldSystemFontOfSize:20]; [self.view addSubview:self.titleLabel]; UIButton* backAction = [[UIButton alloc]initWithFrame:CGRectMake(14, StatusBarHeight + 10.0f, 24, 24)]; [backAction setImage:[UIImage imageNamed:@"vqu_images_return_white"] forState:(UIControlStateNormal)]; [backAction setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)]; backAction.adjustsImageWhenHighlighted = NO; [backAction addTarget:self action:@selector(backClick) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:backAction]; UIButton* beautSwiBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-62-14, StatusBarHeight + 12.0f, 62, 20)]; if (Is_iPhoneX) { // backAction.frame = CGRectMake(KScreenWidth-68-15, 45, 68, 32); } [beautSwiBtn setImage:[UIImage imageNamed:@"vqu_images_beauty_open"] forState:(UIControlStateSelected)]; [beautSwiBtn setImage:[UIImage imageNamed:@"vqu_images_beauty_off"] forState:(UIControlStateNormal)]; beautSwiBtn.adjustsImageWhenHighlighted = NO; [beautSwiBtn addTarget:self action:@selector(beautSwiClick:) forControlEvents:(UIControlEventTouchUpInside)]; beautSwiBtn.selected = self.beautyOpen; self.btnSwitch = beautSwiBtn; [self.view addSubview:beautSwiBtn]; UIView* botView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-120, KScreenWidth, 70)]; self.botView = botView; [self.view addSubview:botView]; UIButton* beautSaveBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/2-35, 0, 70, 70)]; [beautSaveBtn setImage:[UIImage imageNamed:@"vqu_images_beauty_save"] forState:(UIControlStateNormal)]; beautSaveBtn.adjustsImageWhenHighlighted = NO; [beautSaveBtn addTarget:self action:@selector(beautSave) forControlEvents:(UIControlEventTouchUpInside)]; [self.botView addSubview:beautSaveBtn]; UIButton* beautSetBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/4-45, 8, 54, 54)]; [beautSetBtn setImage:[UIImage imageNamed:@"vqu_images_beauty_be"] forState:(UIControlStateNormal)]; beautSetBtn.adjustsImageWhenHighlighted = NO; [beautSetBtn addTarget:self action:@selector(beautSetClick) forControlEvents:(UIControlEventTouchUpInside)]; [self.botView addSubview:beautSetBtn]; UIButton* beautChangeBtn= [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth*3/4-10, 8, 54, 54)]; [beautChangeBtn setImage:[UIImage imageNamed:@"vqu_images_beauty_switch"] forState:(UIControlStateNormal)]; beautChangeBtn.adjustsImageWhenHighlighted = NO; [beautChangeBtn addTarget:self action:@selector(changeCamera:) forControlEvents:(UIControlEventTouchUpInside)]; [self.botView addSubview:beautChangeBtn]; } - (void)beautSetClick{ self.botView.hidden = YES; self.beautyEditView.hidden = NO; } - (void)beautSwiClick:(UIButton *)sender { sender.selected = !sender.selected; self.beautyOpen = sender.selected; if (sender.selected) { self.needSave = YES; }else{ self.needSave = YES; } } - (void)changeCamera:(UIButton *)sender { sender.userInteractionEnabled = NO ; dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC); dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){ sender.userInteractionEnabled = YES ; }); [self.mCamera changeCameraInputDeviceisFront:sender.selected]; sender.selected = !sender.selected ; } - (void)beautSave{ // 归档 NSData *shapeParamsData = [NSKeyedArchiver archivedDataWithRootObject:_beautyEditView.shapeParams]; NSData *skinParamsData = [NSKeyedArchiver archivedDataWithRootObject:_beautyEditView.skinParams]; NSUserDefaults*userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setInteger:_beautyEditView.oneKeyType forKey:@"oneKeyType"]; [userDefaults setInteger:_beautyEditView.filterIndex forKey:@"filtersUseIndex"];; [userDefaults synchronize]; // 写本地 [self writeData:shapeParamsData fileName:@"shapeParamsData"]; [self writeData:skinParamsData fileName:@"skinParamsData"]; [ZCHUDHelper showTitle:@"保存成功"]; [self back]; } - (void)backClick{ if (self.needSave) { //弹窗保存美颜设置 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存设置" message:@"保存当前美颜设置,视频中直接使用" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [self back]; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [self.navigationController popToRootViewControllerAnimated:NO]; [self beautSave]; }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }else{ [self back]; } } - (void)back{ dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER); [self.mCamera stopCapture]; UIViewController *presenting = self.presentingViewController; if(presenting){ [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } dispatch_semaphore_signal(signal); } #pragma mark --- FUCameraDelegate -(void)didOutputVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer { CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) ; if (self.beautyOpen) { [_mPixelFree processWithBuffer:pixelBuffer rotationMode:PFRotationMode0]; } [self.renderView displayPixelBuffer:pixelBuffer withLandmarks:NULL count:0 MAX:YES]; } -(void)demoBarDidShowTopView:(BOOL)shown { //是否显示上半部分 } #pragma mark --- Observer - (void)addObserver{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; } - (void)willResignActive { if (self.navigationController.visibleViewController == self) { [_mCamera stopCapture]; } } - (void)willEnterForeground { if (self.navigationController.visibleViewController == self) { [_mCamera startCapture]; } } - (void)didBecomeActive { if (self.navigationController.visibleViewController == self) { [_mCamera startCapture]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark 澜极美颜--start -(PFAPIDemoBar *)beautyEditView { if (!_beautyEditView) { _beautyEditView = [[PFAPIDemoBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 280, self.view.frame.size.width, 280)]; _beautyEditView.mDelegate = self; } return _beautyEditView ; } -(void)comparisonButtonDidClick:(BOOL)state{ self.clickCompare = state; } -(void)writeData:(NSData *)data fileName:(NSString *)fileName{ NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [documentDirectories objectAtIndex:0]; // 获取目标文件的完整路径 NSString *filePath = [documentDirectory stringByAppendingPathComponent:fileName]; // [[NSFileManager defaultManager] fileExistsAtPath:filePath]; // 创建文件并覆盖写入数据 BOOL success = [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; if (success) { NSLog(@"数据写入成功"); } else { NSLog(@"数据写入失败"); } } -(NSData *)readDatafileName:(NSString *)fileName{ NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [documentDirectories objectAtIndex:0]; // 获取目标文件的完整路径 NSString *filePath = [documentDirectory stringByAppendingPathComponent:fileName]; NSData *data = [NSData dataWithContentsOfFile:filePath]; return data; } -(void)initPixelFree{ NSString *face_FiltePath = [[NSBundle mainBundle] pathForResource:@"filter_model.bundle" ofType:nil]; NSString *authFile = [[NSBundle mainBundle] pathForResource:@"pixelfreeAuth.lic" ofType:nil]; self.mPixelFree = [[SMPixelFree alloc] initWithProcessContext:nil srcFilterPath:face_FiltePath authFile:authFile]; [self.view addSubview:self.beautyEditView]; self.beautyEditView.hidden = YES; } -(void)setDefaultParam{ NSArray* defaultData = [PFDateHandle setupShapData]; NSArray* defaultSkinData = [PFDateHandle setupSkinData]; NSArray* defaultfiltersData = [PFDateHandle setupFilterData]; NSArray* defaultfaceData = [PFDateHandle setupFaceType]; // 读本地缓存 NSData *data = [self readDatafileName:@"shapeParamsData"]; if (data) { defaultData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; } data = [self readDatafileName:@"skinParamsData"]; if (data) { defaultSkinData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; } data = [self readDatafileName:@"filtersParamsData"]; if (data) { defaultfiltersData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; } data = [self readDatafileName:@"oneKeyParamsData"]; if (data) { defaultfaceData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; } // 更新 UI self.beautyEditView.shapeParams = defaultData; self.beautyEditView.skinParams = defaultSkinData; self.beautyEditView.filtersParams = defaultfiltersData; self.beautyEditView.faceTypeParams = defaultfaceData; NSUserDefaults*userDefaults = [NSUserDefaults standardUserDefaults]; int oneKeyType = (int)[userDefaults integerForKey:@"oneKeyType"]; int filtersIndex = (int)[userDefaults integerForKey:@"filtersUseIndex"]; self.beautyEditView.oneKeyType = oneKeyType; self.beautyEditView.filterIndex = filtersIndex; [self.beautyEditView updateDemoBar]; // 更新 SDK 设置 for (PFBeautyParam *param in defaultData) { [self filterValueChange:param]; } for (PFBeautyParam *param in defaultSkinData) { [self filterValueChange:param]; } PFBeautyParam *param = defaultfaceData[oneKeyType]; [self filterValueChange:param]; param = defaultfiltersData[filtersIndex]; [self filterValueChange:param]; } -(void)filterValueChange:(PFBeautyParam *)param{ CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); float value = param.mValue; if(param.type == FUDataTypeBeautify){ if ([param.mParam isEqualToString:@"face_EyeStrength"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_EyeStrength value:&value]; } if ([param.mParam isEqualToString:@"face_thinning"]) { float aa = param.mValue; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_thinning value:&value]; } if ([param.mParam isEqualToString:@"face_narrow"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_narrow value:&value]; } if ([param.mParam isEqualToString:@"face_chin"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_chin value:&value]; } if ([param.mParam isEqualToString:@"face_V"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_V value:&value]; } if ([param.mParam isEqualToString:@"face_small"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_small value:&value]; } if ([param.mParam isEqualToString:@"face_nose"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_nose value:&value]; } if ([param.mParam isEqualToString:@"face_forehead"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_forehead value:&value]; } if ([param.mParam isEqualToString:@"face_mouth"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_mouth value:&value]; } if ([param.mParam isEqualToString:@"face_philtrum"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_philtrum value:&value]; } if ([param.mParam isEqualToString:@"face_long_nose"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_long_nose value:&value]; } if ([param.mParam isEqualToString:@"face_eye_space"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_eye_space value:&value]; } if ([param.mParam isEqualToString:@"face_smile"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_smile value:&value]; } if ([param.mParam isEqualToString:@"face_eye_rotate"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_eye_rotate value:&value]; } if ([param.mParam isEqualToString:@"face_canthus"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFace_canthus value:&value]; } if ([param.mParam isEqualToString:@"runddy"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceRuddyStrength value:&value]; } if ([param.mParam isEqualToString:@"writen"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceWhitenStrength value:&value]; } if ([param.mParam isEqualToString:@"blur"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceBlurStrength value:&value]; } if ([param.mParam isEqualToString:@"sharpen"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceSharpenStrength value:&value]; } if ([param.mParam isEqualToString:@"newWhitenStrength"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceM_newWhitenStrength value:&value]; } if ([param.mParam isEqualToString:@"qualityStrength"]) { [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeFaceH_qualityStrength value:&value]; } } if (param.type == FUDataTypeFilter) { const char *aaa = [param.mParam UTF8String]; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterName value:(void *)aaa]; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterStrength value:&value]; } if (param.type == FUDataTypeStickers) { if([param.mParam isEqualToString:@"origin"]){ [self.mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterSticker2DFilter value:NULL]; } else{ // NSString *path = [[NSBundle mainBundle] pathForResource:@"Stickers" ofType:nil]; // NSString *currentFolder = [path stringByAppendingPathComponent:param.mParam]; // const char *aaa = [currentFolder UTF8String]; // [self.mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterSticker2DFilter value:(void *)aaa]; NSString *name = [NSString stringWithFormat:@"%@.bundle",param.mParam]; NSString *paths = [[NSBundle mainBundle] pathForResource:name ofType:nil]; [self.mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterSticker2DFilter value:(void *)[paths UTF8String]]; } } if (param.type == FUDataTypeOneKey) { if ([param.mTitle isEqualToString:@"origin"]) { int value = PFBeautyTypeOneKeyNormal; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; } if ([param.mTitle isEqualToString:@"自然"]) { int value = PFBeautyTypeOneKeyNatural; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; } if ([param.mTitle isEqualToString:@"可爱"]) { int value = PFBeautyTypeOneKeyCute; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; } if ([param.mTitle isEqualToString:@"女神"]) { int value = PFBeautyTypeOneKeyGoddess; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; } if ([param.mTitle isEqualToString:@"白净"]) { int value = PFBeautyTypeOneKeyFair; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; } } } -(void)bottomDidChange:(int)index{ if (index != 0 && _beautyEditView.oneKeyType != PFBeautyTypeOneKeyNormal) { int value = PFBeautyTypeOneKeyNormal; [_mPixelFree pixelFreeSetBeautyFiterParam:PFBeautyFiterTypeOneKey value:&value]; [self showDelayedAlert]; _beautyEditView.oneKeyType = PFBeautyTypeOneKeyNormal; } } - (void)showDelayedAlert { // 创建 UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"一键美颜已关闭" preferredStyle:UIAlertControllerStyleAlert]; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)); dispatch_after(delay, dispatch_get_main_queue(), ^{ [alertController dismissViewControllerAnimated:YES completion:nil]; }); [self presentViewController:alertController animated:YES completion:nil]; } #pragma mark 澜极美颜--end @end