前言
各位应用程序开发者有没有在后盾收到过家长们的反馈? 心愿可能提供一个开关,采取一些措施爱护小孩的眼睛,因为当初小孩子的远视率越来越高,和他们长时间近距离盯着屏幕有很大的关系。最近有一个海内的客户通过集成了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<T>接口,应用此类中的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/...
原作者:旭小夜