这篇文章主要介绍“Android中如何在安卓上开发一个微笑抓拍神器”,在日常操作中,相信很多人在Android中如何在安卓上开发一个微笑抓拍神器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中如何在安卓上开发一个微笑抓拍神器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联自2013年起,先为淄博等服务建站,淄博等地企业,进行企业商务咨询服务。为淄博企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。华为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' }
这个主要是用来模型更新的,后面算法有了优化,可以自动下载到手机里面更新
检测后拍照:
进行分析器参数配置
把分析器参数配置传给分析器
在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() { @Override public void destroy() { } @Override public void transactResult(MLAnalyzer.Result result) { SparseArray 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中如何在安卓上开发一个微笑抓拍神器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联-成都网站建设公司网站,小编会继续努力为大家带来更多实用的文章!