Java | Android 中如何实现人物运动跟踪功能

1 概述

视频就是回忆ーー那么为什么不花更多的时间让它们看起来更好呢?市场上的许多移动应用只提供基本的编辑功能,比如应用过滤器和添加贴纸。也就是说,对于那些想要制作动态视频的人来说,仅仅让一个移动的人保持焦点是不够的。传统上,这需要添加一个关键帧和视频图像手动调整,这可以吓跑许多业余视频编辑器。我就是其中之一,我一直在寻找一种更简单的方法来实现这种功能。幸运的是,我偶然发现了 HMS 核心视频编辑工具包中的跟踪人员功能,它可以自动生成一个以移动人物为中心的视频。

2 代码实现

2.1 准备工作

配置AppGallery Connect

2.2 项目配置

通过访问令牌或 API 密钥设置应用程序的认证信息。使用 setAccessToken 方法在应用程序初始化期间设置访问令牌。这只需要设置一次。

MediaApplication.getInstance().setAccessToken("your access token");

或者,在应用程序初始化过程中使用setApiKey来设置一个 API 密钥。

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

设置一个唯一的许可证 ID。

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

初始化华为视频编辑器的执行期函式库。当创建一个视频编辑项目时,首先创建一个华为视频编辑器对象并初始化它的执行期函式库。在退出视频编辑项目时释放此对象。

创建一个华为视频编辑器对象。

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

指定预览区域的位置。该区域呈现视频图像。这个过程是通过在 SDK 中创建 SurfaceView 实现的,必须在创建区域之前指定预览区域的位置。

<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" />
// Specify the preview area position.
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// Configure the preview area layout.
editor.setDisplay(mSdkPreviewContainer);

初始化执行期函式库。如果许可证验证失败,将引发 LicenseException。创建 HuaweiVideoEditor对象不会占用任何系统资源。执行期函式库的初始化时间必须手动设置。然后,在 SDK 中创建必要的线程和计时器。

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

添加视频或图片。创建一个视频通道。使用文件路径向通道添加视频或图像。

// Obtain the HVETimeLine object.
HVETimeLine timeline = editor.getTimeLine();

// Create a video lane.
HVEVideoLane videoLane = timeline.appendVideoLane();

// Add a video to the end of the lane.
HVEVideoAsset videoAsset = videoLane.appendVideoAsset("test.mp4");

// Add an image to the end of the video lane.
HVEImageAsset imageAsset = videoLane.appendImageAsset("test.jpg");

3 功能实现

// Initialize the capability engine.
visibleAsset.initHumanTrackingEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // Initialization progress.
        }

        @Override
        public void onSuccess() {
        // The initialization is successful.
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        // The initialization failed.
    }
   });

// Track a person using the coordinates. Coordinates of two vertices that define the rectangle containing the person are returned.
List<Float> rects = visibleAsset.selectHumanTrackingPerson(bitmap, position2D);

// Enable the effect of person tracking.
visibleAsset.addHumanTrackingEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
            // Handling progress.
        }

        @Override
        public void onSuccess() {
            // Handling successful.
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // Handling failed.
        }
});

// Interrupt the effect.
visibleAsset.interruptHumanTracking();

// Remove the effect.
visibleAsset.removeHumanTrackingEffect();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟华328

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值