腾讯特效SDK快速接入 Avatar_音视频解决方案_同尘科技

腾讯特效SDK 2年前 (2023-07-14) 浏览 216

由于 Avatar 是腾讯特效的部分功能,所以需要先集成腾讯美颜特效 SDK,再加载 Avatar 素材即可。若未接入腾讯美颜特效 SDK,可参考 独立集成腾讯特效 进行了解与集成。

步骤1:准备 Avatar 素材

1. 集成腾讯特效 SDK。2. 在官网下载对应的 Demo 工程,并解压。3. 将 Demo 中的 BeautyDemo/bundle/avatarMotionRes.bundle 素材文件复制到您的工程中。

步骤2:接入 Demo 界面

接入方法

1. 在项目中使用与 BeautyDemo 一样的 Avatar 操作界面。2. 复制 Demo 中 BeautyDemo/Avatar 文件夹下的所有类到您的工程中,添加如下代码即可:

  AvatarViewController *avatarVC = [[AvatarViewController alloc] init];  avatarVC.modalPresentationStyle = UIModalPresentationFullScreen;  avatarVC.currentDebugProcessType = AvatarPixelData; // 图像或者纹理Id方式  [self presentViewController:avatarVC animated:YES completion:nil];

代码参考:自定义捏脸功能

可参考 BeautyDemo/Avatar/Controller 中的 AvatarViewController 相关代码。说明:接口说明请参见 Avatar SDK 说明。1. 创建 xmagic 对象,设置 Avatar 默认模板。

- (void)buildBeautySDK {    CGSize previewSize = CGSizeMake(kPreviewWidth, kPreviewHeight);    NSDictionary *assetsDict = @{@"core_name":@"LightCore.bundle",                                                             @"root_path":[[NSBundle mainBundle] bundlePath]    };    // Init beauty kit    self.beautyKit = [[XMagic alloc] initWithRenderSize:previewSize assetsDict:assetsDict];    // Register log    [self.beautyKit registerSDKEventListener:self];    [self.beautyKit registerLoggerListener:self withDefaultLevel:YT_SDK_ERROR_LEVEL];
// 传入素材文件对应的路径即可加载avatar默认形象 AvatarGender gender = self.genderBtn.isSelected ? AvatarGenderFemale : AvatarGenderMale; NSString *bundlePath = [self.resManager avatarResPath:gender]; [self.beautyKit loadAvatar:bundlePath exportedAvatar:nil];
}

2. 获取素材的 Avatar 源数据。

@implementation AvatarViewController_resManager = [[AvatarResManager alloc] init];NSDictionary *avatarDict = self.resManager.getMaleAvatarData;@end

@implementation AvatarResManager
- (NSDictionary *)getMaleAvatarData{ if (!_maleAvatarDict) { NSString *resDir = [self avatarResPath:AvatarGenderFemale]; NSString *savedConfig = [self getSavedAvatarConfigs:AvatarGenderMale]; // 通过sdk接口解析出素材源数据 _maleAvatarDict = [XMagic getAvatarConfig:resDir exportedAvatar:savedConfig]; } return _maleAvatarDict;}@end

3. 捏脸操作。

// 从sdk接口解析出来的素材源数据中拿到想要形象的avatar对象,传入sdkNSMutableArray *avatars = [NSMutableArray array];// avatarConfig是从sdk接口getAvatarConfig:exportedAvatar:获取的其中一个avatar对象[avatars addObject:avatarConfig];// 捏脸/换装接口,调用后实时更新当前素材呈现出的形象[self.beautyKit updateAvatar:avatars];

4. 导出捏脸字符串:
将当前 Avatar 配置的对象导出为字符串,可自定义存储。

- (BOOL)saveSelectedAvatarConfigs:(AvatarGender)gender{    NSMutableArray *avatarArr = [NSMutableArray array];    NSDictionary *avatarDict = gender == AvatarGenderMale ? _maleAvatarDict : _femaleAvatarDict;    // 1、遍历找出选中的avatar对象    for (NSArray *arr in avatarDict.allValues) {            for (AvatarData *config in arr) {                    if (config.type == AvatarDataTypeSelector) {                            if (config.isSelected) {                                    [avatarArr addObject:config];                            }                    } else {                            [avatarArr addObject:config];                    }            }    }    // 2、调用sdk接口将选中的avatar对象导出为字符串    NSString *savedConfig = [XMagic exportAvatar:avatarArr.copy];    if (savedConfig.length <= 0) {            return NO;    }    NSError *error;    NSString *fileName = [self getSaveNameWithGender:gender];    NSString *savePath = [_saveDir stringByAppendingPathComponent:fileName];    // 判断目录是否存在,不存在则创建目录    BOOL isDir;    if (![[NSFileManager defaultManager] fileExistsAtPath:_saveDir isDirectory:&isDir]) {            [[NSFileManager defaultManager] createDirectoryAtPath:_saveDir withIntermediateDirectories:YES attributes:nil error:nil];    }    // 3、将导出的字符串写入沙盒,下次取出来可用    [savedConfig writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:&error];    if (error) {            return NO;    }    return YES;}

5. 设置半身或全身形象。

// 设置半身- (void)setupHalfOrWholeBody{    AvatarData *config = [self createAvatarData];    [self.beautyKit updateAvatar:@[config] completion:^(BOOL success, NSArray * _Nullable invalidAvatarList) {        NSLog(@"---");    }];}- (AvatarData *)createAvatarData{    AvatarData *config = [[AvatarData alloc] init];    config.entityName = @"Camera3D";    config.action = @"basicTransform";    config.value = @{@"position" : _avatarViewValue[_viewIndex]};    return config;}



对音视频的解决方案有疑惑?想了解解决方案收费? 联系解决方案专家

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心,购买腾讯云享受折上折,更有现金返利:同意关联,立享优惠

阿里云解决方案也看看?: 点击对比阿里云的解决方案

- 0人点赞 -

发表点评 (0条)

not found

暂无评论,你要说点什么吗?