本篇文章为大家展示了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。