本篇文章為大家展示了Android中怎么實(shí)現(xiàn)一個(gè)拍照翻譯小程序,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
10多年的麗水網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整麗水建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“麗水網(wǎng)站設(shè)計(jì)”,“麗水網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
??由于使用了云側(cè)的服務(wù),需要到華為的開(kāi)發(fā)者聯(lián)盟注冊(cè)開(kāi)發(fā)者賬號(hào),并且在云端開(kāi)通這些服務(wù),這里就不細(xì)講了,直接按照官方的的AppGallery Connect配置、開(kāi)通服務(wù)部分操作步驟進(jìn)行即可:
注冊(cè)開(kāi)發(fā)者,開(kāi)通服務(wù)參考請(qǐng)戳:
打開(kāi)AndroidStudio項(xiàng)目級(jí)build.gradle文件。
?增量添加如下maven地址:
buildscript { repositories { maven {url 'http://developer.huawei.com/repo/'} } }allprojects { repositories { maven { url 'http://developer.huawei.com/repo/'} } }
?集成SDK。(由于使用云側(cè)能力,只引入SDK基礎(chǔ)包即可)
dependencies{ implementation 'com.huawei.hms:ml-computer-vision:1.0.2.300' implementation 'com.huawei.hms:ml-computer-translate:1.0.2.300' }
??要使應(yīng)用程序能夠在用戶從華為應(yīng)用市場(chǎng)安裝您的應(yīng)用程序后,自動(dòng)將最新的機(jī)器學(xué)習(xí)模型更新到用戶設(shè)備,請(qǐng)將以下語(yǔ)句添加到該應(yīng)用程序的AndroidManifest.xml文件中:
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();
Tasktask = this.textAnalyzer.asyncAnalyseFrame(mlFrame); task.addOnSuccessListener(new OnSuccessListener () { @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 Tasktask = translator.asyncTranslate(this.sourceText); task.addOnSuccessListener(new OnSuccessListener () { @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(); }
上述內(nèi)容就是Android中怎么實(shí)現(xiàn)一個(gè)拍照翻譯小程序,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。