前言

双十一即将来临不晓得各位的购物是不是曾经塞满了货色呢?小编也想将本人的购物车塞得满满的,奈何钱包不想,于是就只能通过游戏虚构购物来满足本人的购物欲了。没曾想到居然被我发现了一款集成了华为HMS ML Kit手部关键点检测的小游戏-疯狂购物车,上面就跟小编一起看看这个游戏是怎么实现的吧!

利用场景

疯狂购物车小游戏是通过集成华为HMS ML Kit手部关键点检测服务来实现的,通过手势检测能够管制购物车左右挪动,从而接住掉落下来的各类商品,每隔15秒将提一次速,给玩家带来不一样的购物游戏体验。

怎么样,这么乏味的游戏还不心动吗?那就一起来看看开发步骤吧!

开发实战

  1. 配置Maven仓地址

关上Android Studio我的项目级“build.gradle”文件

buildscript {    repositories {        google()        jcenter()        maven {url 'https://developer.huawei.com/repo/'}    }    dependencies {        ...        classpath 'com.huawei.agconnect:agcp:1.4.1.300'    }} allprojects {    repositories {        google()        jcenter()        maven {url 'https://developer.huawei.com/repo/'}    }}
  1. Full SDK集成
dependencies{    // 引入根底SDK    implementation 'com.huawei.hms:ml-computer-vision-handkeypoint:2.0.4.300'    // 引入手部关键点检测模型包    implementation 'com.huawei.hms:ml-computer-vision-handkeypoint-model:2.0.4.300'}

用上述形式两种办法之一集成SDK后,在文件头增加配置。

在apply plugin: 'com.android.application'后增加apply plugin: 'com.huawei.agconnect'

  1. 创立手部关键点分析器
MLHandKeypointAnalyzer analyzer =MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();
  1. 创立辨认后果解决类“HandKeypointTransactor”
public class HandKeypointTransactor implements MLAnalyzer.MLTransactor<List<MLHandKeypoints>> {    @Override    public void transactResult(MLAnalyzer.Result<List<MLHandKeypoints>> results) {        SparseArray<List<MLHandKeypoints>> analyseList = results.getAnalyseList();        // 开发者依据须要解决辨认后果,须要留神,这里只对检测后果进行解决。        // 不可调用ML Kit提供的其余检测相干接口。    }    @Override    public void destroy() {        // 检测完结回调办法,用于开释资源等。    }}
  1. 设置辨认后果处理器,实现分析器与后果处理器的绑定
analyzer.setTransactor(new HandKeypointTransactor());
  1. 创立LensEngine
LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)    .setLensType(LensEngine.BACK_LENS)    .applyDisplayDimension(1280, 720)    .applyFps(20.0f)    .enableAutomaticFocus(true)    .create();
  1. 调用run办法,启动相机,读取视频流,进行辨认
// 请自行实现SurfaceView控件的其余逻辑。SurfaceView mSurfaceView = findViewById(R.id.surface_view);try {    lensEngine.run(mSurfaceView.getHolder());} catch (IOException e) {    // 异样解决逻辑。}
  1. 检测实现,进行分析器,开释检测资源
if (analyzer != null) {    analyzer.stop();}if (lensEngine != null) {    lensEngine.release();}

结束语

看完次要开发步骤是不是感觉集成简略又疾速,除了上述的疯狂购物车小游戏,手部关键点辨认技术在生活中有很多的利用场景。比方拍摄短视频的软件在集成了这种技术后,能够依据手部关键点生成一些可恶或者搞笑的特效,减少短视频的趣味性。或者是在面向智能家居的场景中,能够自定义一些手势作为智能家电的远距离操控指令,进行一些更加智能的人机交互形式。快来试试吧,一起开发好玩又乏味的利用吧!

Github Demo

更具体的开发指南参考华为开发者联盟官网:https://developer.huawei.com/consumer/cn/hms/huawei-mlkit


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0204394603426760022?fid=18

原作者:timer