您好,登录后才能下订单哦!
这篇文章主要介绍“Android中如何开发一个证件照DIY小程序”,在日常操作中,相信很多人在Android中如何开发一个证件照DIY小程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中如何开发一个证件照DIY小程序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
  打开AndroidStudio项目级build.gradle文件。
增量添加如下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-image-segmentation-body-model:1.0.2.301' }
要使应用程序能够在用户从华为应用市场安装您的应用程序后,自动将最新的机器学习模型更新到用户设备,请将以下语句添加到该应用程序的AndroidManifest.xml文件中:
<manifest <application <meta-data android:name="com.huawei.hms.ml.DEPENDENCY" android:value= "imgseg "/> </application> </manifest>
<!--使用存储权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (!allPermissionsGranted()) { getRuntimePermissions(); }}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode != PERMISSION_REQUESTS) { return; } boolean isNeedShowDiag = false; for (int i = 0; i < permissions.length; i++) { if (permissions[i].equals(Manifest.permission.READ_EXTERNAL_STORAGE) && grantResults[i] != PackageManager.PERMISSION_GRANTED) { // 如果相机或者存储权限没有授权,则需要弹出授权提示框 isNeedShowDiag = true; } } if (isNeedShowDiag && !ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { AlertDialog dialog = new AlertDialog.Builder(this) .setMessage(getString(R.string.camera_permission_rationale)) .setPositiveButton(getString(R.string.settings), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); // 根据包名打开对应的设置界面 startActivityForResult(intent, 200); startActivity(intent); } }) .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).create(); dialog.show(); }}
可以通过图像分割检测配置器“MLImageSegmentationSetting”创建图像分割检测器。
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory() .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG) .setExact(true) .create(); this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
可以通过图像分割检测配置器“MLImageSegmentationSetting”创建图像分割检测器。
MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create();
// 创建一个task,处理图像分割检测器返回的结果。 Task<MLImageSegmentation> task = analyzer.asyncAnalyseFrame(frame); // 异步处理图像分割检测器返回结果 Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame); task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() { @Override public void onSuccess(MLImageSegmentation mlImageSegmentationResults) { // Transacting logic for segment success. if (mlImageSegmentationResults != null) { StillCutPhotoActivity.this.foreground = mlImageSegmentationResults.getForeground(); StillCutPhotoActivity.this.preview.setImageBitmap(StillCutPhotoActivity.this.foreground); StillCutPhotoActivity.this.processedImage = ((BitmapDrawable) ((ImageView) StillCutPhotoActivity.this.preview).getDrawable()).getBitmap(); StillCutPhotoActivity.this.changeBackground(); } else { StillCutPhotoActivity.this.displayFailure(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // Transacting logic for segment failure. StillCutPhotoActivity.this.displayFailure(); return; } });
this.backgroundBitmap = BitmapUtils.loadFromPath(StillCutPhotoActivity.this, id, targetedSize.first, targetedSize.second);BitmapDrawable drawable = new BitmapDrawable(backgroundBitmap);this.preview.setDrawingCacheEnabled(true);this.preview.setBackground(drawable);this.preview.setImageBitmap(this.foreground);this.processedImage = Bitmap.createBitmap(this.preview.getDrawingCache());this.preview.setDrawingCacheEnabled(false);
到此,关于“Android中如何开发一个证件照DIY小程序”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。