美摄SDK For Android
3.14.2
|
在拍摄视频时,通过对视频进行抠像,然后运用美摄SDk的虚拟场景包(.capturescene)到视频上,即实现了虚拟直播功能。
具体实现流程如下:
初始化StreamingContext。创建NvsStreamingContext单例对象时,有两种方式,这里选用不带flags的创建方式。
m_streamingContext = NvsStreamingContext.init(this, null);
设置回调并将采集预览输出连接到NvsLiveWindow控件。
//给streaming context设置回调接口 m_streamingContext.setCaptureDeviceCallback(this); m_streamingContext.getAssetPackageManager().setCallbackInterface(this);
//采集预览输出连接到NvsLiveWindow控件 m_streamingContext.connectCapturePreviewWithLiveWindow(m_liveWindow);
添加抠像采集特效。
NvsCaptureVideoFx keyerFx = m_streamingContext.appendBuiltinCaptureVideoFx("Master Keyer"); if(keyerFx != null) { //开启溢色去除 keyerFx.setBooleanVal("Spill Removal",true); //将溢色去除强度设置为最低 keyerFx.setFloatVal("Spill Removal Intensity",0); //设置收缩边界强度 keyerFx.setFloatVal("Shrink Intensity",0.4); }
启动采集预览。
//需要对采集的视频进行颜色吸取操作,因此需要使用STREAMING_ENGINE_CAPTURE_FLAG_GRAB_CAPTURED_VIDEO_FRAME标志 m_streamingContext.startCapturePreview(0, NvsStreamingContext.VIDEO_CAPTURE_RESOLUTION_GRADE_HIGH, NvsStreamingContext.STREAMING_ENGINE_CAPTURE_FLAG_GRAB_CAPTURED_VIDEO_FRAME, null);
点击吸色,并将吸取的背景画面颜色值设置给抠像特技。
NvsColor sampledColor = m_streamingContext.sampleColorFromCapturedVideoFrame(sampleRect); //将吸取下来的背景画面颜色值设置给抠像特技 NvsCaptureVideoFx keyerFx = m_streamingContext.getCaptureVideoFxByIndex(0); if(keyerFx) { keyerFx.setColorVal("Key Color",sampledColor); }
运用虚拟场景包,然后就会出现虚拟直播的场景。
m_streamingContext.applyCaptureScene(sceneId);