您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。