您好,登錄后才能下訂單哦!
本篇文章為大家展示了Android中怎么實現一個拍照翻譯小程序,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
??由于使用了云側的服務,需要到華為的開發者聯盟注冊開發者賬號,并且在云端開通這些服務,這里就不細講了,直接按照官方的的AppGallery Connect配置、開通服務部分操作步驟進行即可:
   注冊開發者,開通服務參考請戳:
  打開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{ implementation 'com.huawei.hms:ml-computer-vision:1.0.2.300' implementation 'com.huawei.hms:ml-computer-translate:1.0.2.300' }
??要使應用程序能夠在用戶從華為應用市場安裝您的應用程序后,自動將最新的機器學習模型更新到用戶設備,請將以下語句添加到該應用程序的AndroidManifest.xml文件中:
<manifest <application <meta-data android:name="com.huawei.hms.ml.DEPENDENCY" android:value= "imgseg "/> </application> </manifest>
<uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-feature android:name="android.hardware.camera" /><uses-feature android:name="android.hardware.camera.autofocus" />
private static final int CAMERA_PERMISSION_CODE = 1; @Override public void onCreate(Bundle savedInstanceState) { // Checking camera permission if (!allPermissionsGranted()) { getRuntimePermissions(); }}
MLRemoteTextSetting setting = (new MLRemoteTextSetting.Factory()). setTextDensityScene(MLRemoteTextSetting.OCR_LOOSE_SCENE).create();this.textAnalyzer = MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer(setting);
MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create();
Task<MLText> task = this.textAnalyzer.asyncAnalyseFrame(mlFrame); task.addOnSuccessListener(new OnSuccessListener<MLText>() { @Override public void onSuccess(MLText mlText) { // Transacting logic for segment success. if (mlText != null) { RemoteTranslateActivity.this.remoteDetectSuccess(mlText); } else { RemoteTranslateActivity.this.displayFailure(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // Transacting logic for segment failure. RemoteTranslateActivity.this.displayFailure(); return; } });
MLRemoteTranslateSetting.Factory factory = new MLRemoteTranslateSetting .Factory() // Set the target language code. The ISO 639-1 standard is used. .setTargetLangCode(this.dstLanguage); if (!this.srcLanguage.equals("AUTO")) { // Set the source language code. The ISO 639-1 standard is used. factory.setSourceLangCode(this.srcLanguage); } this.translator = MLTranslatorFactory.getInstance().getRemoteTranslator(factory.create());
final Task<String> task = translator.asyncTranslate(this.sourceText); task.addOnSuccessListener(new OnSuccessListener<String>() { @Override public void onSuccess(String text) { if (text != null) { RemoteTranslateActivity.this.remoteDisplaySuccess(text); } else { RemoteTranslateActivity.this.displayFailure(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { RemoteTranslateActivity.this.displayFailure(); } });
if (this.textAnalyzer != null) { try { this.textAnalyzer.close(); } catch (IOException e) { SmartLog.e(RemoteTranslateActivity.TAG, "Stop analyzer failed: " + e.getMessage()); } } if (this.translator != null) { this.translator.stop(); }
上述內容就是Android中怎么實現一個拍照翻譯小程序,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。