快速搭建直播平台教程,美颜SDK接入实现多种美颜特效
想要快速搭建直播平台,最快的办法就是使用成品直播源码直接搭建直播平台,而在搭建过程中,美颜功能是必不可少的。
如上图所示,直播平台中使用的美颜主要有四大功能,分别是贴纸、美颜、特效和哈哈镜:
1.贴纸是给人脸上添加一些面具等装饰效果,分为基础和高级大类
2.美颜是调整人脸的美白,磨皮,红润,大眼,瘦脸,瘦鼻,额头等形状位置,从而美化面部特征,以及添加多种滤镜
3.特效里面包括一些动态特效,例如各种抖动效果,以及添加水印的功能。
4.哈哈镜则是让人脸像哈哈镜一样进行各种夸张的变化,增加趣味性。这个功能是依托于人脸识别来实现的。
快速搭建直播平台,接入美颜功能的部分代码如下:
public class MhDataManager { private static MhDataManager sInstance; private MHBeautyManager mMhManager; private MeiYanValueBean mInputValue; private MeiYanValueBean mUseValue; private MeiYanValueBean mMeiYanValue; private MeiYanValueBean mOneKeyValue; private IBeautyEffectListener mMeiYanChangedListener; private String mStickerName;//贴纸 private int mSpeciallyName = MHSDK.SPECIAL_NONE;//特效 private int mFilterId = MHSDK.FILTER_NONE;//滤镜 private int mWaterBitmapRes;//水印图片 private int mWaterAlignEnum = MHSDK.WATER_NONE;//水印位置 private int mDistortionName = MHSDK.HAHA_NONE;//哈哈镜 private MhDataManager() { } public static MhDataManager getInstance() { if (sInstance == null) { synchronized (MhDataManager.class) { if (sInstance == null) { sInstance = new MhDataManager(); } } } return sInstance; } public MhDataManager init() { mInputValue = new MeiYanValueBean(); mUseValue = null; mMeiYanValue = null; mOneKeyValue = null; mStickerName = null; mSpeciallyName = MHSDK.SPECIAL_NONE; mFilterId = MHSDK.FILTER_NONE; mWaterBitmapRes = 0; mWaterAlignEnum = MHSDK.WATER_NONE; mDistortionName = MHSDK.HAHA_NONE; releaseBeautyManager(); return this; } public void createBeautyManager() { if (mInputValue != null) { mInputValue.reset(); } else { mInputValue = new MeiYanValueBean(); } try { mMhManager = new MHBeautyManager(CommonAppContext.getInstance()); } catch (Exception e) { mMhManager = null; e.printStackTrace(); } } public void setMeiYanChangedListener(IBeautyEffectListener meiYanChangedListener) { mMeiYanChangedListener = meiYanChangedListener; } public void release() { mMeiYanChangedListener = null; mInputValue = null; mUseValue = null; mMeiYanValue = null; mOneKeyValue = null; mStickerName = null; mSpeciallyName = MHSDK.SPECIAL_NONE; mFilterId = MHSDK.FILTER_NONE; mWaterBitmapRes = 0; mWaterAlignEnum = MHSDK.WATER_NONE; mDistortionName = MHSDK.HAHA_NONE; releaseBeautyManager(); } public void releaseBeautyManager() { if (mMhManager != null) { try { mMhManager.destroy(); } catch (Exception e) { e.printStackTrace(); } } mMhManager = null; } public int render(int texture, int width, int height) { if (mMhManager != null) { try { // mMhSDKManager.setAsync(true); int faceScale = 4; int textureScale = 2; int; int newWidth = ((width * 4 + (align - 1)) & ~(align - 1)) / 4; int newHeight = ((height * 4 + (align - 1)) & ~(align - 1)) / 4; texture = mMhManager.render12(texture, newWidth, newHeight, faceScale, textureScale); } catch (Exception e) { e.printStackTrace(); } } return texture; } public void restoreBeautyValue() { notifyMeiYanChanged(); notifyLiangDuChanged(); if (!TextUtils.isEmpty(mStickerName)) { setTieZhi(mStickerName); } if (mFilterId != MHSDK.FILTER_NONE) { if (isUseMhFilter() && mMhManager != null) { mMhManager.setFilter(mFilterId); } } if (mSpeciallyName != MHSDK.SPECIAL_NONE) { setTeXiao(mSpeciallyName); } if (mWaterBitmapRes != 0 && mWaterAlignEnum != MHSDK.WATER_NONE) { setWater(mWaterBitmapRes, mWaterAlignEnum); } if (mDistortionName != MHSDK.HAHA_NONE) { setHaHa(mDistortionName); } } public void saveBeautyValue() { if (mMeiYanValue != null) { CommonHttpUtil.setBeautyValue(JSON.toJSONString(mMeiYanValue)); } } public MeiYanValueBean getMeiYanValue() { return mMeiYanValue; } public MeiYanValueBean getOneKeyValue() { return mOneKeyValue; } public MhDataManager setMeiYanValue(MeiYanValueBean meiYanValue) { mMeiYanValue = meiYanValue; return this; } public MhDataManager setOneKeyValue(MeiYanValueBean oneKeyValue) { mOneKeyValue = oneKeyValue; return this; } public MhDataManager useMeiYan() { mUseValue = mMeiYanValue; return this; } public MhDataManager useOneKey() { mUseValue = mOneKeyValue; return this; } public void setMeiBai(int meiBai) { if (mMeiYanValue != null) { mMeiYanValue.setMeiBai(meiBai); } useMeiYan().notifyMeiYanChanged(); } public void setMoPi(int moPi) { if (mMeiYanValue != null) { mMeiYanValue.setMoPi(moPi); } useMeiYan().notifyMeiYanChanged(); } public void setHongRun(int hongRun) { if (mMeiYanValue != null) { mMeiYanValue.setHongRun(hongRun); } useMeiYan().notifyMeiYanChanged(); } public void setLiangDu(int liangDu) { if (mMeiYanValue != null) { mMeiYanValue.setLiangDu(liangDu); } useMeiYan().notifyLiangDuChanged(); } public void setDaYan(int daYan) { if (mMeiYanValue != null) { mMeiYanValue.setDaYan(daYan); } useMeiYan().notifyMeiYanChanged(); } public void setMeiMao(int meiMao) { if (mMeiYanValue != null) { mMeiYanValue.setMeiMao(meiMao); } useMeiYan().notifyMeiYanChanged(); } public void setYanJu(int yanJu) { if (mMeiYanValue != null) { mMeiYanValue.setYanJu(yanJu); } useMeiYan().notifyMeiYanChanged(); } public void setYanJiao(int yanJiao) { if (mMeiYanValue != null) { mMeiYanValue.setYanJiao(yanJiao); } useMeiYan().notifyMeiYanChanged(); } public void setShouLian(int shouLian) { if (mMeiYanValue != null) { mMeiYanValue.setShouLian(shouLian); } useMeiYan().notifyMeiYanChanged(); } public void setZuiXing(int zuiXing) { if (mMeiYanValue != null) { mMeiYanValue.setZuiXing(zuiXing); } useMeiYan().notifyMeiYanChanged(); } public void setShouBi(int shouBi) { if (mMeiYanValue != null) { mMeiYanValue.setShouBi(shouBi); } useMeiYan().notifyMeiYanChanged(); } public void setXiaBa(int xiaBa) { if (mMeiYanValue != null) { mMeiYanValue.setXiaBa(xiaBa); } useMeiYan().notifyMeiYanChanged(); } public void setETou(int ETou) { if (mMeiYanValue != null) { mMeiYanValue.setETou(ETou); } useMeiYan().notifyMeiYanChanged(); } public void setChangBi(int changBi) { if (mMeiYanValue != null) { mMeiYanValue.setChangBi(changBi); } useMeiYan().notifyMeiYanChanged(); } public void setXueLian(int xueLian) { if (mMeiYanValue != null) { mMeiYanValue.setXueLian(xueLian); } useMeiYan().notifyMeiYanChanged(); } public void setKaiYanJiao(int kaiYanJiao) { if (mMeiYanValue != null) { mMeiYanValue.setKaiYanJiao(kaiYanJiao); } useMeiYan().notifyMeiYanChanged(); } /** * 哈哈镜 */ public void setHaHa(int hahaName) { mDistortionName = hahaName; if (mMhManager != null) { mMhManager.setDistortionEffect(hahaName); } } /** * 贴纸是否可用 */ public boolean isTieZhiEnable() { return mMeiYanChangedListener == null || mMeiYanChangedListener.isTieZhiEnable(); } /** * 贴纸 */ public void setTieZhi(String tieZhiName) { mStickerName = tieZhiName; if (mMhManager != null) { mMhManager.setSticker(tieZhiName); } } /** * 水印 */ public void setWater(int bitmapRes, int position) { mWaterBitmapRes = bitmapRes; mWaterAlignEnum = position; if (mMhManager != null) { Bitmap bitmap = null; if (bitmapRes == 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8); } else { bitmap = BitmapFactory.decodeResource(CommonAppContext.getInstance().getResources(), bitmapRes); } mMhManager.setWaterMark(bitmap, position); } } /** * 特效 */ public void setTeXiao(int teXiaoId) { mSpeciallyName = teXiaoId; if (mMhManager != null) { mMhManager.setSpeciallyEffect(teXiaoId); } } /** * 是否使用用美狐的滤镜 true使用美狐滤镜,false不使用 */ public boolean isUseMhFilter() { return mMeiYanChangedListener == null || mMeiYanChangedListener.isUseMhFilter(); } /** * 滤镜 */ public void setFilter(MeiYanFilterBean bean) { if (isUseMhFilter()) { mFilterId = bean.getFilterRes(); if (mMhManager != null) { mMhManager.setFilter(bean.getFilterRes()); } } else { if (mMeiYanChangedListener != null) { mMeiYanChangedListener.onFilterChanged(bean.getName()); } } } /** * 亮度数值发生变化 */ private void notifyLiangDuChanged() { if (mMhManager == null || mInputValue == null || mUseValue == null) { return; } if (mInputValue.getLiangDu() != mUseValue.getLiangDu()) { mInputValue.setLiangDu(mUseValue.getLiangDu()); mMhManager.setBrightness(mUseValue.getLiangDu()); } } /** * 美颜美型数值发生变化 */ public void notifyMeiYanChanged() { if (mMhManager == null || mInputValue == null || mUseValue == null) { return; } MeiYanValueBean input = mInputValue; MeiYanValueBean use = mUseValue; if (mMeiYanChangedListener != null) { boolean meiBaiChanged = false; boolean moPiChanged = false; boolean hongRunChanged = false; if (input.getMeiBai() != use.getMeiBai()) { input.setMeiBai(use.getMeiBai()); meiBaiChanged = true; } if (input.getMoPi() != use.getMoPi()) { input.setMoPi(use.getMoPi()); moPiChanged = true; } if (input.getHongRun() != use.getHongRun()) { input.setHongRun(use.getHongRun()); hongRunChanged = true; } mMeiYanChangedListener.onMeiYanChanged(input.getMeiBai(), meiBaiChanged, input.getMoPi(), moPiChanged, input.getHongRun(), hongRunChanged); } else { //美白 if (input.getMeiBai() != use.getMeiBai()) { input.setMeiBai(use.getMeiBai()); mMhManager.setSkinWhiting(input.getMeiBai()); } //磨皮 if (input.getMoPi() != use.getMoPi()) { input.setMoPi(use.getMoPi()); mMhManager.setSkinSmooth(input.getMoPi()); } //红润 if (input.getHongRun() != use.getHongRun()) { input.setHongRun(use.getHongRun()); mMhManager.setSkinTenderness(input.getHongRun()); } } //大眼 if (input.getDaYan() != use.getDaYan()) { input.setDaYan(use.getDaYan()); mMhManager.setBigEye(input.getDaYan()); } //眉毛 if (input.getMeiMao() != use.getMeiMao()) { input.setMeiMao(use.getMeiMao()); mMhManager.setEyeBrow(input.getMeiMao()); } //眼距 if (input.getYanJu() != use.getYanJu()) { input.setYanJu(use.getYanJu()); mMhManager.setEyeLength(input.getYanJu()); } //眼角 if (input.getYanJiao() != use.getYanJiao()) { input.setYanJiao(use.getYanJiao()); mMhManager.setEyeCorner(input.getYanJiao()); } //瘦脸 if (input.getShouLian() != use.getShouLian()) { input.setShouLian(use.getShouLian()); mMhManager.setFaceLift(input.getShouLian()); } //嘴型 if (input.getZuiXing() != use.getZuiXing()) { input.setZuiXing(use.getZuiXing()); mMhManager.setMouseLift(input.getZuiXing()); } //瘦鼻 if (input.getShouBi() != use.getShouBi()) { input.setShouBi(use.getShouBi()); mMhManager.setNoseLift(input.getShouBi()); } //下巴 if (input.getXiaBa() != use.getXiaBa()) { input.setXiaBa(use.getXiaBa()); mMhManager.setChinLift(input.getXiaBa()); } //额头 if (input.getETou() != use.getETou()) { input.setETou(use.getETou()); mMhManager.setForeheadLift(input.getETou()); } //长鼻 if (input.getChangBi() != use.getChangBi()) { input.setChangBi(use.getChangBi()); mMhManager.setLengthenNoseLift(input.getChangBi()); } //削脸 if (input.getXueLian() != use.getXueLian()) { input.setXueLian(use.getXueLian()); mMhManager.setFaceShave(input.getXueLian()); } //开眼角 if (input.getKaiYanJiao() != use.getKaiYanJiao()) { input.setKaiYanJiao(use.getKaiYanJiao()); mMhManager.setEyeAlat(input.getKaiYanJiao()); } } /** * 获取贴纸列表 */ public static void getTieZhiList(int id, final CommonCallback<String> commonCallback) { MHSDK.getTieZhiList(id, new MHSDK.TieZhiListCallback() { @Override public void getTieZhiList(String data) { if (commonCallback != null) { commonCallback.callback(data); } } }); } /** * 下载贴纸 */ public static void downloadTieZhi(String tieZhiName, final CommonCallback<Boolean> commonCallback) { MHSDK.downloadSticker(tieZhiName, new MHSDK.TieZhiDownloadCallback() { @Override public void tieZhiDownload(String tieZhiName, boolean success) { if (success) { if (commonCallback != null) { commonCallback.callback(true); } } else { if (commonCallback != null) { commonCallback.callback(false); } } } }); } /** * 贴纸是否下载了 */ public static boolean isTieZhiDownloaded(String name) { return MHSDK.isTieZhiDownloaded(name); }
这样就实现了美颜的功能,一般来说,在快速搭建直播平台的过程中,美颜功能是直接使用美颜SDK实现的,云豹科技可为您提供云豹美颜SDK,如您需要快速搭建直播平台,云豹科技也可为您提供原生直播源码,欢迎联系。
声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com