这篇文章主要介绍“Android中如何在安卓上开发一个微笑抓拍神器”,在日常操作中,相信很多人在Android中如何在安卓上开发一个微笑抓拍神器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中如何在安卓上开发一个微笑抓拍神器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
华为HMS的kit开发前准备工作都差不多,无非就是添加maven依赖,引入SDK。
增量添加如下maven地址:
buildscript { repositories { maven {url 'http://developer.huawei.com/repo/'} } }allprojects { repositories { maven { url 'http://developer.huawei.com/repo/'} }}
把人脸识别的SDK和基础SDK引入:
dependencies{ // 引入基础SDK implementation 'com.huawei.hms:ml-computer-vision:1.0.2.300' // 引入人脸检测能力包 implementation 'com.huawei.hms:ml-computer-vision-face-recognition-model:1.0.2.300' }
这个主要是用来模型更新的,后面算法有了优化,可以自动下载到手机里面更新
<manifest <application <meta-data android:name="com.huawei.hms.ml.DEPENDENCY" android:value= "face"/> </application></manifest>
<!--相机权限--><uses-permission android:name="android.permission.CAMERA" /><!--使用存储权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
检测后拍照:
进行分析器参数配置
把分析器参数配置传给分析器
在analyzer.setTransacto内通过重写transactResult处理人脸识别后的内容,人脸识别后会返回一个微笑的置信度(简单可以理解为是微笑的概率),只要设置大于一定置信度进行拍照就可以了。
private MLFaceAnalyzer analyzer;private void createFaceAnalyzer() { MLFaceAnalyzerSetting setting = new MLFaceAnalyzerSetting.Factory() .setFeatureType(MLFaceAnalyzerSetting.TYPE_FEATURES) .setKeyPointType(MLFaceAnalyzerSetting.TYPE_UNSUPPORT_KEYPOINTS) .setMinFaceProportion(0.1f) .setTracingAllowed(true) .create(); this.analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer(setting); this.analyzer.setTransactor(new MLAnalyzer.MLTransactor<MLFace>() { @Override public void destroy() { } @Override public void transactResult(MLAnalyzer.Result<MLFace> result) { SparseArray<MLFace> faceSparseArray = result.getAnalyseList(); int flag = 0; for (int i = 0; i < faceSparseArray.size(); i++) { MLFaceEmotion emotion = faceSparseArray.valueAt(i).getEmotions(); if (emotion.getSmilingProbability() > smilingPossibility) { flag++; } } if (flag > faceSparseArray.size() * smilingRate && safeToTakePicture) { safeToTakePicture = false; mHandler.sendEmptyMessage(TAKE_PHOTO); } } });}
拍照存储部分:
private void takePhoto() { this.mLensEngine.photograph(null, new LensEngine.PhotographListener() { @Override public void takenPhotograph(byte[] bytes) { mHandler.sendEmptyMessage(STOP_PREVIEW); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); saveBitmapToDisk(bitmap); } });}
private void createLensEngine() { Context context = this.getApplicationContext(); // Create LensEngine this.mLensEngine = new LensEngine.Creator(context, this.analyzer).setLensType(this.lensType) .applyDisplayDimension(640, 480) .applyFps(25.0f) .enableAutomaticFocus(true) .create();}
@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_live_face_analyse); if (savedInstanceState != null) { this.lensType = savedInstanceState.getInt("lensType"); } this.mPreview = this.findViewById(R.id.preview); this.createFaceAnalyzer(); this.findViewById(R.id.facingSwitch).setOnClickListener(this); // Checking Camera Permissions if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { this.createLensEngine(); } else { this.requestCameraPermission(); }} private void requestCameraPermission() { final String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}; if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { ActivityCompat.requestPermissions(this, permissions, LiveFaceAnalyseActivity.CAMERA_PERMISSION_CODE); return; }}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode != LiveFaceAnalyseActivity.CAMERA_PERMISSION_CODE) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); return; } if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { this.createLensEngine(); return; }}
到此,关于“Android中如何在安卓上开发一个微笑抓拍神器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。