前言
We wish you a Merry Christmas, We wish you a Merry Christmas, We wish you a Merry Christmas and Happy New Year。聖誕節來臨是不是大街小巷已經想起了這首熟悉的聖誕歌曲了呢?鋪天蓋地的聖誕樹,聖誕玩偶,和雪花都在提醒着你聖誕節的到來,一款集成了華為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/'}
}
}
2.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'
3.創建手部關鍵點分析器
MLHandKeypointAnalyzer analyzer =MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();
4.創建識別結果處理類“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() {
// 檢測結束回調方法,用於釋放資源等。
}
}
5.設置識別結果處理器,實現分析器與結果處理器的綁定
analyzer.setTransactor(new HandKeypointTransactor());
6.創建LensEngine
LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(1280, 720)
.applyFps(20.0f)
.enableAutomaticFocus(true)
.create();
7.調用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();
}
結束語
手部關鍵點識別支持識別包括手指指尖,手腕等21個手部關鍵點,並返回關鍵點的位置數據。這款聖誕小游戲就是通過識別手部動態軌跡而控制雪橇車的移動,來接住各類從天而降的禮物,增加游戲的可玩性和趣味性。手部關鍵點識別還可以廣泛應用在其它類別的APP上,大家一起來打開腦洞開發試試吧!
了解更多
欲了解更多詳情,請參閱:
華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1
獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1
參與開發者討論請到Reddit社區:https://www.reddit.com/r/HMSCore/
下載demo和示例代碼請到Github:https://github.com/HMS-Core
解決集成問題請到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0202442756764380482?fid=18
原作者:timer
