2

When editing a video, if the user wants to achieve the same filter style as a particular picture, how to extract it?

Huawei's video editing service new "exclusive filter" function. After integration, the application will have the ability to copy filters and transfer the color of the filter to the video or picture with one click. What filter do you want? The effect is immediately pasted, which can support diversified image beautification needs, and the integration is simple, and the user operation is easier.

Features

  • Support clone filter and single image imitation, two interfaces, optional integration. The clone filter uploads the original image and the filter image at the same time, the copy effect is better; the single image imitating only needs to upload the filter image, the operation is simpler.
  • The filter image is automatically saved, which is convenient for users to directly transfer the filter style without adding it again.
  • Support custom filter image name.
  • The intensity of the filter action can be adjusted, and the filter style can be customized.

Integration code

1 Development preparation

For detailed preparation steps, please refer to the official website of Huawei Developers Alliance:
https://developer.huawei.com/consumer/cn/doc/development/Media-Guides/config-agc-0000001101108580?ha_source=hms1

2 Edit project integration

2.1 Set the authentication information of the application

The application authentication information can be set through api_key or Access Token.

  • Set the Access Token through the setAccessToken method, which can be initialized once when the application starts, and there is no need to set it multiple times.

    MediaApplication.getInstance().setAccessToken("your access token");
  • The api_key is set through the setApiKey method, which can be initialized once when the application starts, and there is no need to set it multiple times.

    MediaApplication.getInstance().setApiKey("your ApiKey");

    2.2 Set the unique identification ID, that is, the License ID.

License ID is a valid certificate for management and control, and you must ensure that the license ID is unique.

MediaApplication.getInstance().setLicenseId("License ID");

2.2.1 Initialize the Editor operating environment

To create an editing project, you need to first create an Editor object and initialize its running environment. When leaving the editing project, the Editor instance should be released.

(1) Create an Editor object

HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());

(2) Specify the layout position of the preview window

The preview window is responsible for the rendering of the video image, which is implemented by creating a SurfaceView internally in the video editing atomic capability SDK. Before creating a window, you need to specify the layout position of the preview window in your App.

<LinearLayout    
    android:id="@+id/video_content_layout"    
    android:layout_width="0dp"    
    android:layout_height="0dp"    
    android:background="@color/video_edit_main_bg_color"    
    android:gravity="center"    
    android:orientation="vertical" />
// 指定预览窗口 
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// 设置预览窗口承载的布局 
editor.setDisplay(mSdkPreviewContainer);

(3) Initialize the operating environment. If the license authentication fails, a LicenseException will be thrown.

After the Editor object is created, the actual system resources are not occupied at this time, and it is necessary to manually select the timing of its environment initialization. At this time, the necessary threads and timers are created inside the Video Editing Atomic Ability SDK.

try {
        editor.initEnvironment();
   } catch (LicenseException error) { 
        SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());    
        finish();
        return;
   }

2.2.2 Add videos and pictures

Create a video swim lane, and then add pictures or video material to the swim lane. Images and video materials need to be added to the swim lane through the file path.

// 获取时间线对象 
HVETimeLine timeline = editor.getTimeLine();

// 创建视频泳道 
HVEVideoLane videoLane = timeline.appendVideoLane();

// 在视频泳道的末尾,添加视频资源 
HVEVideoAsset videoAsset = vidoeLane.appendVideoAsset("test.mp4");

// 在视频泳道的末尾,添加图片资源 
HVEImageAsset imageAsset = vidoeLane.appendImageAsset("test.jpg");

2.2.3 Create outreach special effects swim lanes

Exclusive filters need to be added to the special effects swim lanes, can span multiple resources, and can adjust the length of time arbitrarily.

// 创建特效泳道 
HVEEffectLane effectLane = timeline.appendEffectLane();

3 Exclusive filter integration

// 创建专属滤镜算法引擎
HVEExclusiveFilter filterEngine = new HVEExclusiveFilter();

// 初始化专属滤镜算法引擎
mFilterEngine.initExclusiveFilterEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // 初始化进度回调
        }

        @Override
        public void onSuccess() {
            // 初始化成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // 初始化失败
        }
    });

// 创建单图滤镜,指定一个Bitmap和滤镜的名字
// 返回滤镜ID,通过此ID,可以在数据库中查询滤镜相关的所有信息
String effectId = mFilterEngine.createExclusiveEffect(bitmap, "自定义滤镜01");

// 将滤镜添加到特效泳道中,起始位置0, 时长3000ms
effectLane.appendEffect(new HVEEffect.Options(
    HVEEffect.CUSTOM_FILTER + mSelectName, effectId, ""), 0, 3000);

Demo demo

for more details>>

Visit Huawei Developer Alliance official website
Obtain development guide document
Huawei Mobile Services open source warehouse address: GitHub , Gitee

and learn about the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。