2

When making short videos, users will have various beautification needs, such as wanting to change the hair color of video characters, how to easily achieve it? Huawei video editing service (Video Editor Kit) one-click hair coloring function, integration allows the application to have the ability to change hair color. With a variety of preset hair color styles, users can click to have a variety of new hair colors, experience supernatural hair color effects, meet users' personalized beautification needs, and make videos more colorful and interesting.

Features

  1. Support picture and video resources, one-click hair coloring ability can process pictures and video materials in real time.
  2. Support multiple people to dye their hair at the same time. One-click hair coloring ability can produce hair color effect on multiple people in the picture/video.
  3. The intensity of action can be adjusted, and users can freely adjust the depth of hair color according to the scene.

integration code

1 Development preparation

For detailed preparation steps, please refer to the official website of Huawei Developer Alliance:

https://developer.huawei.com/consumer/cn/doc/development/Media-Guides/config-agc-0000001101108580

2 Edit engineering integration

2.1 Set the authentication information of the application

Application authentication information can be set through api_key or Access Token.

  • The Access Token is set through the setAccessToken method, which can be initialized and set 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 by the setApiKey method, which can be initialized and set 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.

The license ID is a valid certificate for management and control. You must ensure that the license ID is unique.

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

2.2.1 Initialize the Editor runtime environment

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

(1) Create 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 video images, which is realized by creating SurfaceView inside the video editing atomic capability SDK. Before creating the 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 running 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 the timing of its environment initialization needs to be manually selected. At this time, the necessary threads and timers will be created inside the Video Editing Atomic Capability SDK.

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

2.2.2 Adding videos and pictures

Create a video swimlane, then add pictures or video footage to the swimlane. Images and video clips need to be added to the swimlane via the file path.

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

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

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

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

3 One-click hair dyeing integration

// 一键染发AI算法初始化
asset.initHairDyeingEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // 初始化进度
        }

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

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

// 添加一键染发特效,指定一键染发的色卡和默认强度。
asset.addHairDyeingEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
            // 一键染发处理进度。
        }

        @Override
        public void onSuccess() {
            // 一键染发处理成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // 一键染发处理失败
        }
    }, colorPath, defaultStrength);

// 移除一键染发特效
asset.removeHairDyeingEffect();

Demo

  • During the integrated development process, you can online bill of lading if you have any questions, and someone will answer it.

more details>>

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

us and know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

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