前言
雙十一即將來臨不知道各位的購物是不是已經塞滿了東西呢?小編也想將自己的購物車塞得滿滿的,奈何錢包不想,於是就只能通過游戲虛擬購物來滿足自己的購物欲了。沒曾想到竟然被我發現了一款集成了華為HMS ML Kit手部關鍵點檢測的小游戲-瘋狂購物車,下面就跟小編一起看看這個游戲是怎么實現的吧!
應用場景
瘋狂購物車小游戲是通過集成華為HMS ML Kit手部關鍵點檢測服務來實現的,通過手勢檢測可以控制購物車左右移動,從而接住掉落下來的各類商品,每隔15秒將提一次速,給玩家帶來不一樣的購物游戲體驗。
怎么樣,這么有趣的游戲還不心動嗎?那就一起來看看開發步驟吧!
開發實戰
- 配置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/'}
}
}
- 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'
- 創建手部關鍵點分析器
MLHandKeypointAnalyzer analyzer =MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();
- 創建識別結果處理類“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() {
// 檢測結束回調方法,用於釋放資源等。
}
}
- 設置識別結果處理器,實現分析器與結果處理器的綁定
analyzer.setTransactor(new HandKeypointTransactor());
- 創建LensEngine
LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(1280, 720)
.applyFps(20.0f)
.enableAutomaticFocus(true)
.create();
- 調用run方法,啟動相機,讀取視頻流,進行識別
// 請自行實現SurfaceView控件的其他邏輯。
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
// 異常處理邏輯。
}
- 檢測完成,停止分析器,釋放檢測資源
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