超简单集成ML kit 实现听写单词播报 (2)

设置读取照片、拍照和朗读按钮

this.relativeLayoutLoadPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ReadPhotoActivity.this.selectLocalImage(ReadPhotoActivity.this.REQUEST_CHOOSE_ORIGINPIC); } }); this.relativeLayoutTakePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ReadPhotoActivity.this.takePhoto(ReadPhotoActivity.this.REQUEST_TAKE_PHOTO); } });

在拍照和读取照片的回调当中启动文本识别startTextAnalyzer()

private void startTextAnalyzer() { if (this.isChosen(this.originBitmap)) { 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) { ReadPhotoActivity.this.remoteDetectSuccess(mlText); } else { ReadPhotoActivity.this.displayFailure(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // Transacting logic for segment failure. ReadPhotoActivity.this.displayFailure(); return; } }); } else { Toast.makeText(this.getApplicationContext(), R.string.please_select_picture, Toast.LENGTH_SHORT).show(); return; } }

识别成功后,点击播放按钮即可开始播放

this.relativeLayoutRead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (ReadPhotoActivity.this.sourceText == null) { Toast.makeText(ReadPhotoActivity.this.getApplicationContext(), R.string.please_select_picture, Toast.LENGTH_SHORT).show(); } else { ReadPhotoActivity.this.mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND); Toast.makeText(ReadPhotoActivity.this.getApplicationContext(), R.string.read_start, Toast.LENGTH_SHORT).show(); } } }); Demo效果

在这里插入图片描述

往期链接:超简单集成HMS Scan Kit扫码SDK,轻松实现扫码购
原文链接:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0201283755975150303&fid=18
原作者:littlewhite

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfxgx.html