超简单集成HMS ML Kit 实现parental control

前言


  各位应用程序开发者有没有在后台收到过家长们的反馈? 希望能够提供一个开关,采取一些措施保护小孩的眼睛,因为现在小孩子的近视率越来越高,和他们长时间近距离盯着屏幕有很大的关系。最近有一个海外的客户通过集成了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


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44708240/article/details/107998091