前言
各位應用程序開發者有沒有在后台收到過家長們的反饋? 希望能夠提供一個開關,采取一些措施保護小孩的眼睛,因為現在小孩子的近視率越來越高,和他們長時間近距離盯着屏幕有很大的關系。最近有一個海外的客戶通過集成了ML kit 實現了防范小朋友眼睛離屏幕過近,或者玩游戲時間過長的父母類控制類功能。
場景
父母需要這個功能防止小朋友眼睛距離屏幕過近,或者小朋友看屏幕時間過長。
開發前准備
在項目級gradle里添加華為maven倉
打開AndroidStudio項目級build.gradle文件
增量添加如下maven地址:
buildscript {
{
maven {url 'http://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}
}
在應用級的build.gradle里面加上SDK依賴
dependencies {
implementation 'com.huawei.hms:ml-computer-vision-face:1.0.4.300'
implementation 'com.huawei.hms:ml-computer-vision-face-shape-point-model:1.0.4.300'
implementation 'com.huawei.hms:ml-computer-vision-face-emotion-model:1.0.4.300'
implementation 'com.huawei.hms:ml-computer-vision-face-feature-model:1.0.4.300'
}
在AndroidManifest.xml文件里面申請相機、訪問網絡和存儲權限
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
動態權限申請
動態權限申請
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestCameraPermission();
}
代碼開發關鍵步驟
創建人體臉部分析器。
MLFaceAnalyzer analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer();
創建LensEngine實例用於視頻流的人臉檢測,該類由ML Kit SDK提供,用於捕捉相機動態視頻流並傳入分析器。
LensEngine mLensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(640, 480)
.applyFps(30.0f)
.enableAutomaticFocus(true)
.create();
開發者創建識別結果處理類“FaceAnalyzerTransactor”,該類實現MLAnalyzer.Result
接口,使用此類中的transactResult方法獲取人臉呈現在屏幕上的檢測結果,並根據手機屏幕的寬高比例與呈現在屏幕上臉部的寬高比例進行對比,如果呈現在屏幕前的人臉所占比率過大,則鎖屏
public class FaceAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLFace> {
@Override
public void transactResult(MLAnalyzer.Result<MLFace> results) {
SparseArray<MLFace> items = results.getAnalyseList();
// 開發者根據需要處理識別結果,需要注意,這里只對檢測結果進行處理。
// 不可調用ML kit提供的其他檢測相關接口。
if (items != null) {
MLFace features = items.get(0);
if (features == null) return;
BigDecimal bigPhoneWidth = new BigDecimal(Float.toString(640));
BigDecimal bigPhoneHeight = new BigDecimal(Float.toString(480));
float phoneRatio = bigPhoneWidth.multiply(bigPhoneHeight).floatValue();
BigDecimal bigFaceWidth = new BigDecimal(Float.toString(features.getWidth()));
BigDecimal bigFaceHeight = new BigDecimal(Float.toString(features.getHeight()));
float faceRatio = bigFaceWidth.multiply(bigFaceHeight).floatValue();
BigDecimal bigPhoneRatio = new BigDecimal(Float.toString(phoneRatio));
BigDecimal bigFaceRatio = new BigDecimal(Float.toString(faceRatio));
final float ratio = bigPhoneRatio.divide(bigFaceRatio, 2, BigDecimal.ROUND_HALF_EVEN).floatValue();
BigDecimal bigRatio = new BigDecimal(Float.toString(ratio));
BigDecimal schedule = new BigDecimal(Float.toString(10));
float scheduleRatio = bigRatio.multiply(schedule).floatValue();
final int realRatio = Math.round(scheduleRatio);
int distance = Integer.parseInt(mDistance);
if (distance <= 6)
distance = 6;
if (distance >= realRatio) {
// 鎖屏提示,距離屏幕過近,屏幕鎖屏
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// 緩慢靠近時提示,當下距離屏幕前的距離
}
});
}
}
}
@Override
public void destroy() {
// 檢測結束回調方法,用於釋放資源等。
release();
}
}
設置識別結果處理器,實現分析器與結果處理器的綁定
analyzer.setTransactor(new FaceAnalyzerTransactor());
調用run方法,啟動相機,讀取視頻流,進行識別。
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
// 異常處理
lensEngine.release();
lensEngine = null;
}
檢測完成,停止分析器,釋放檢測資源
if (mLensEngine != null) {
mLensEngine.release();
}
if (analyzer != null) {
try {
analyzer.stop();
} catch (IOException e) {
// 異常處理
}
}
maven地址
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
Demo
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203325260088000089&fid=18
原作者:旭小夜