合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
**1.找到****TestSendCustomCameraData****.java文件,添加如下变量:** ``` private BaseBeautyViewHolder mBeautyViewHolder; private MHBeautyManager mhBeautyManager; ``` **找到****onTextureProcess****方法,添加美颜处理代码:** ``` @Override public int onTextureProcess(int textureId, EGLContext eglContext) {     if (!mIsSending) return textureId;     if(mBeautyViewHolder==null){         return textureId;     }     if (mhBeautyManager == null) {         mhBeautyManager = new MHBeautyManager(mContext,true);         mhBeautyManager.setBeautyDataModel(BeautyDataModel.getInstance());         if (mBeautyViewHolder != null) {             mBeautyViewHolder.setMhBeautyManager(mhBeautyManager);         }         return textureId;     }     int newTex = mhBeautyManager.render(textureId, mCameraHeight, mCameraWidth);     //将视频帧通过纹理方式塞给SDK     TRTCCloudDef.TRTCVideoFrame videoFrame = new TRTCCloudDef.TRTCVideoFrame();     videoFrame.texture = new TRTCCloudDef.TRTCTexture();     videoFrame.texture.textureId = newTex;     videoFrame.texture.eglContext14 = eglContext;     videoFrame.width = mCameraHeight;     videoFrame.height = mCameraWidth;     videoFrame.pixelFormat = TRTCCloudDef.TRTC_VIDEO_PIXEL_FORMAT_Texture_2D;     videoFrame.bufferType = TRTCCloudDef.TRTC_VIDEO_BUFFER_TYPE_TEXTURE;     mTRTCCloud.sendCustomVideoData(videoFrame);     return textureId; } ``` **找到****onSurfaceTextureDestroy****方法,销毁美颜管理类对象:** ``` @Override public void onSurfaceTextureDestroy(SurfaceTexture surfaceTexture) {     Log.d(TAG, "onSurfaceTextureDestroy: " + Thread.currentThread().getName());     if(mhBeautyManager!=null){         mhBeautyManager.destroy();     } } ``` **添加****getMHBeautyManager****方法:** ``` public MHBeautyManager getMHBeautyManager(){     return mhBeautyManager; } ``` **2.找到****TRTCMainActivity****.java文件** **实现****DefaultBeautyEffectListener****接口。代码如下:** ``` @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 onMoPiChanged(int progress) {     if (mCustomCameraCapture!=null&&mCustomCameraCapture.getMHBeautyManager() != null) {         mCustomCameraCapture.getMHBeautyManager().setSkinSmooth(progress);     } } @Override public void onFengNenChanged(int progress) {     if (mCustomCameraCapture!=null&&mCustomCameraCapture.getMHBeautyManager() != null) {         mCustomCameraCapture.getMHBeautyManager().setSkinTenderness(progress);     } } @Override public void onBeautyOrigin() {     if (mCustomCameraCapture!=null&&mCustomCameraCapture.getMHBeautyManager() != null) {         mCustomCameraCapture.getMHBeautyManager().setSkinWhiting(0);         mCustomCameraCapture.getMHBeautyManager().setSkinSmooth(0);         mCustomCameraCapture.getMHBeautyManager().setSkinTenderness(0);     } } ``` ** 添加如下变量** ``` private Handler mHandler; private AlertDialog dialog; private RelativeLayout beautyContainer; private BaseBeautyViewHolder beautyViewHolder; private boolean isResume; private int retryCount; private Thread beautyThread; ``` **添加如下代码** ``` static class DiaLogListener implements DialogInterface.OnClickListener {         WeakReference activityWeakReference;         DiaLogListener(TRTCMainActivity activity) {             this.activityWeakReference = new WeakReference<>(activity);         }         @Override         public void onClick(DialogInterface dialog, int which) {             TRTCMainActivity 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(TRTCMainActivity activity) {             this.activityWeakReference = new WeakReference<>(activity);         }         @Override         public void onClick(DialogInterface dialog, int which) {             TRTCMainActivity 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(TRTCMainActivity 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();                 }             }             TRTCMainActivity activity = mainActivityWeakReference.get();             if (activity != null){                 activity.mHandler.post(activity::setBeautyView);             }         }     }     private void initBeautyView() {         beautyContainer = findViewById(R.id.beauty_view_container);         if (MHSDK.getInstance().isVerEnd()){             setBeautyView();         }else {             if (beautyThread != null) {                 beautyThread.interrupt();                 beautyThread = null;             }             beautyThread = new BeautyThread(this);             beautyThread.start();         }     }     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();     }     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();             }         }); //        beautyViewHolder.setMhBeautyManager(mhBeautyManager);         mCustomCameraCapture.setBaseBeautyViewHolder(beautyViewHolder);     } ``` **在****onCreate****里最后一行添加:** ``` initBeautyView(); ```