各位应用程序开发者有没有在后台收到过家长们的反馈? 希望能够提供一个开关,采取一些措施保护小孩的眼睛,因为现在小孩子的近视率越来越高,和他们长时间近距离盯着屏幕有很大的关系。最近有一个海外的客户通过集成了ML kit 实现了防范小朋友眼睛离屏幕过近,或者玩游戏时间过长的父母类控制类功能。
父母需要这个功能防止小朋友眼睛距离屏幕过近,或者小朋友看屏幕时间过长。
打开AndroidStudio项目级build.gradle文件
增量添加如下maven地址:
buildscript {
{
maven {url 'http://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}
}
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'
}
<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 mLensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(640, 480)
.applyFps(30.0f)
.enableAutomaticFocus(true)
.create();
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());
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/' }
}
}
原文链接:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203325260088000089&fid=18
原作者:旭小夜