ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## **1****.腾讯移动直播** 注:以下文档针对腾讯6.5版本,如果需要适配6.5以上版本详细看本节结尾说明。 1.初始化MHSDK **找到LpApplication.java文件,初始化SDK** MHSDK.getInstance().init(this," "); 2.初始化美颜界面 注意添加自定义渲染的监听 mLivePusher.setVideoProcessListener(this); **找到TxMainActivity.java文件****,****实现****DefaultBeautyEffectListener****接口****,****添加以下变量****:** ``` private Handler mHandler; private AlertDialog dialog; private RelativeLayout beautyContainer; private MHBeautyManager mhBeautyManager; private BaseBeautyViewHolder beautyViewHolder; private boolean isResume; private int retryCount; private Thread beautyThread; ``` **添加以下方法** ``` private void initBeautyView() {         beautyContainer = findViewById(R.id.beauty_view_container);         mHandler = new Handler(getMainLooper());         if (MHSDK.getInstance().isVerEnd()){             setBeautyView();         }else {             if (beautyThread != null) {                 beautyThread.interrupt();                 beautyThread = null;             }             beautyThread = new BeautyThread(this);             beautyThread.start();         }     } private void setBeautyView(){ LogManager.getInstance().writeData("MainActivity_setBeautyView_SDKVersion=" + MHSDK.getInstance().getVer());         if (!MHSDK.getInstance().isVerifyAccess()){             if (retryCount < 10) {                 showRetryDialog();             }else {                 showExitDialog();                 return;             }         }         if (beautyViewHolder != null) {             beautyViewHolder.release();             beautyViewHolder = null;         }         beautyViewHolder = BeautyViewHolderFactory.getBeautyViewHolder(getApplicationContext(), beautyContainer);         beautyViewHolder.show();         beautyViewHolder.setEffectListener(this);         beautyViewHolder.setCameraClickListener(this);         beautyViewHolder.setVisibleListener(visible -> {             if (!visible) {                 beautyViewHolder.show();             }         });             } private void initMHBeautyManager(){     if (mhBeautyManager == null) {         mhBeautyManager = new MHBeautyManager(this,true); mhBeautyManager.setBeautyDataModel(BeautyDataModel.getInstance()); if (beautyViewHolder != null) {          beautyViewHolder.setMhBeautyManager(mhBeautyManager);         }     } } @Override protected void onResume() {     super.onResume();     initMHBeautyManager();     Log.d("meihu_beauty", "TxMainActivity--onResume"); }     static class DiaLogListener implements DialogInterface.OnClickListener {         WeakReference activityWeakReference;         DiaLogListener(TxMainActivity activity) {             this.activityWeakReference = new WeakReference<>(activity);         }         @Override         public void onClick(DialogInterface dialog, int which) {             TxMainActivity activity = activityWeakReference.get();             if (activity == null)return;             switch (which){                 case -1:                     dialog.cancel();                     activity.finish();                     break;                 case -2:                     dialog.cancel();                     break;             }         }     }     static class SDKDialogListener implements DialogInterface.OnClickListener {         WeakReference activityWeakReference;         SDKDialogListener(TxMainActivity activity) {             this.activityWeakReference = new WeakReference<>(activity);         }         @Override         public void onClick(DialogInterface dialog, int which) {             TxMainActivity activity = activityWeakReference.get();             if (activity == null)return;             switch (which){                 case -1:                     activity.retryCount ++;                     MHSDK.getInstance().clearVerNote();                     MHSDK.getInstance().verify("");                     activity.initBeautyView();                     dialog.cancel(); //                    System.exit(0);                     break;                 case -2:                     dialog.cancel();                     break;             }         }     }     //由于demo进入应用直接进入美颜界面,为了防止授权校验未完成而一直显示标准版界面,需要等授权校验完成后初始化对应的美颜界面(延迟1秒或者按下面处理)     static class BeautyThread extends  Thread {         WeakReference mainActivityWeakReference;         BeautyThread(TxMainActivity txMainActivity) {             this.mainActivityWeakReference = new WeakReference<>(txMainActivity);         }         @Override         public void run() {             while (mainActivityWeakReference.get()!= null && !MHSDK.getInstance().isVerEnd()) {                 if (isInterrupted()) return;                 try {                     Thread.sleep(200);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }             }             TxMainActivity activity = mainActivityWeakReference.get();             if (activity != null){                 activity.mHandler.post(activity::setBeautyView);             }         }     }     private void showRetryDialog() {         if (dialog != null) {             dialog.cancel();         }         SDKDialogListener diaLogListener = new SDKDialogListener(this);         dialog = new AlertDialog.Builder(this)                 .setTitle("提示")                 .setMessage("资源获取失败,请检查网络")                 .setCancelable(false)                 .setPositiveButton("确定", diaLogListener)                 .create();         dialog.show();     }     private void showExitDialog() {         if (dialog != null) {             dialog.cancel();         }         DiaLogListener diaLogListener = new DiaLogListener(this);         dialog = new AlertDialog.Builder(this)                 .setTitle("提示")                 .setMessage("资源获取失败,请检查网络并退出重试")                 .setCancelable(false)                 .setPositiveButton("确定", diaLogListener)                 .create();         dialog.show();     } @Override public void onFilterChanged(FilterBean filterBean) {     if (mhBeautyManager == null) {         return;     }     FilterEnum filterEnum = filterBean.getFilterEnum();     if (filterEnum == FilterEnum.PRO_FILTER) {         mhBeautyManager.changeDynamicFilter(filterBean.getmFilterName());     } else {         Bitmap lookupBitmap = BitmapUtil.getFilterBitmap(this,filterEnum);         mhBeautyManager.setFilter(lookupBitmap);     } } @Override public void onMeiBaiChanged(int progress) {     if (mhBeautyManager != null) {         mhBeautyManager.setSkinWhiting(progress);     } } @Override public void onMoPiChanged(int progress) {     if (mhBeautyManager != null) {         mhBeautyManager.setSkinSmooth(progress);     } } @Override public void onFengNenChanged(int progress) {     if (mhBeautyManager != null) {         mhBeautyManager.setSkinTenderness(progress);     } } @Override public void onBeautyOrigin() {     if (mhBeautyManager != null) {         mhBeautyManager.setSkinWhiting(0);         mhBeautyManager.setSkinSmooth(0);         mhBeautyManager.setSkinTenderness(0);     } } ``` **在****onCreate****里调用****initBeautyView()****方法** **在布局文件中添加美颜UI** ``` <RelativeLayout     android:id="@+id/beauty_view_container"     android:layout_width="match_parent"     android:layout_height="wrap_content"     /> ``` 3.自定义渲染 实现TXLivePusher.VideoCustomProcessListener接口,利用livePusher.setVideoProcessListener(this);//设置回调。 ```     @Override     public int onTextureCustomProcess(int texture, int width, int height) {         int textureId = texture;         try {             if (mhBeautyManager == null) {                 return textureId;             }             long st = System.currentTimeMillis();             int  faceScale = 4; //人脸识别对纹理的缩小倍数             int  textureScale = 2;//图像渲染对纹理的缩小倍数             if (mDeviceLevel == DEVICE_LEVEL_HIGH) {                 textureScale = 1;             }             int align = 128;             int newWidth =  ((width * 4 + (align - 1)) & ~(align - 1))/4;             int newHeight =  ((height * 4 + (align - 1)) & ~(align - 1))/4;             textureId = mhBeautyManager.render12(texture, newWidth, newHeight,faceScale,textureScale);             long et = System.currentTimeMillis(); //            Log.e(TAG, "onTextureCustomProcess: ---->" + (et - st));         } catch (Exception e) {             e.printStackTrace();         }         return textureId;     } @Override public void onDetectFacePoints(float[] floats) { } @Override public void onTextureDestoryed() {     if (mhBeautyManager != null) {         mhBeautyManager.destroy();         mhBeautyManager = null;     } } ``` 说明: 1.如果出现贴纸资源不能下载的情况, 请在AndroidManifest.xml文件的application节点添加        android:usesCleartextTraffic="true"属性。 2.如果使用腾讯SDK6.5(不包含6.5)以上的版本,需要更改纹理的缩小参数,修改方式如下 ``` textureId = mhBeautyManager.render12(texture, width, height,faceScale,textureScale); ``` 其中第4个参数为人脸识别输入对纹理的缩小倍数,第5个参数为自定义渲染图像的缩小倍数。 如:当腾讯SDK返回的纹理大小为1080x1905,第4个参数为4时,人脸识别输入的纹理大小为原来的1/4, 大小为270x476。第5个参数为2时,自定义渲染的图像为原来的1/2,即540x952。具体还可以根据卡顿和清晰度情况来设置第4个和第5个参数。 3. 如果使用腾讯SDK6.5(不包含6.5)以上的版本,还可以根据手机性能来实现对纹理的缩小和适配。相关代码如下: 重写onTextureCustomProcess方法: ```  @Override     public int onTextureCustomProcess(int texture, int width, int height) {         int textureId = texture;         try {             if (mhBeautyManager == null) {                 return textureId;             }             long st = System.currentTimeMillis();             int  faceScale = 4; //人脸识别对纹理的缩小倍数             int  textureScale = 2;//图像渲染对纹理的缩小倍数             if (mDeviceLevel == DEVICE_LEVEL_HIGH) {                 textureScale = 1;             }             int align = 128;             int newWidth =  ((width * 4 + (align - 1)) & ~(align - 1))/4;             int newHeight =  ((height * 4 + (align - 1)) & ~(align - 1))/4;             textureId = mhBeautyManager.render12(texture, newWidth, newHeight,faceScale,textureScale);             long et = System.currentTimeMillis(); //            Log.e(TAG, "onTextureCustomProcess: ---->" + (et - st));         } catch (Exception e) {             e.printStackTrace();         }         return textureId;     } ```