2024年已经过半了,我作为聋人独立开发者,我经常会时不时反思:自己这半年到底进步了多少?在这篇文章里,我分享实现机器学习部署Android端开发Nim翻译App的案例。如果你有一定开发经验,相信这篇文章对你会非常有所帮助。
一、项目背景
使用机器学习套件构建一个 Android App实现文本识别 API 识别实时相机画面中的文本。使用机器学习套件 Language Identification API 识别识别出的文本的语言。机器学习套件 Translation API,文本翻译成选项里任何选定的语言。
二、项目开发
这项目使用 Jetpack Compose 进行 UI 构建,结合 Material 3 设计元素实现了简洁美观UI。Kotlin 协程处理并发任务,两个者多个选手的进度同步更新而不阻塞主线程。 PS:适合已有编程基础的开发者,如果你是初学者,建议先看看我另一篇基础文章:安卓软件开发:如何实现机器学习部署到安卓端-腾讯云开发者社区-腾讯云 (tencent.com)
2.1 在build.gradle
依赖MLKit套件和 CameraX
代码语言:groovy复制// CameraX
def camerax_version = "1.0.0-beta05"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha12"
// ML Kit
implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.0.0'
implementation 'com.google.mlkit:language-id:16.0.0'
implementation 'com.google.mlkit:translate:16.0.0'
2.2 构建 UI
代码语言:java复制<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.google.mlkit.showcase.translate.main.MainFragment">
<androidx.camera.view.PreviewView
android:id="@ id/viewfinder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/middle"
app:layout_constraintTop_toTopOf="parent" />
<SurfaceView
android:id="@ id/overlay"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="@id/viewfinder"
app:layout_constraintEnd_toEndOf="@id/viewfinder"
app:layout_constraintBottom_toBottomOf="@id/viewfinder"
app:layout_constraintTop_toTopOf="@id/viewfinder" />
<androidx.constraintlayout.widget.Guideline
android:id="@ id/middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".5" />
<TextView
android:id="@ id/srcLang"
style="@style/SrcLangText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toTopOf="@ id/srcTextScrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/middle"
tools:text=">源语言" />
<ScrollView
android:id="@ id/srcTextScrollView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="4dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@ id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/srcLang">
<TextView
android:id="@ id/srcText"
style="@style/SrcText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
tools:text="源文本" />
</ScrollView>
<View
android:id="@ id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@ id/targetLangSelector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/srcTextScrollView" />
<Spinner
android:id="@ id/targetLangSelector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider" />
<ScrollView
android:id="@ id/translatedTextScrollView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="4dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@ id/divider2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/targetLangSelector">
<TextView
android:id="@ id/translatedText"
style="@style/TranslatedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="4dp"
tools:text="翻译文本" />
</ScrollView>
<View
android:id="@ id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="20dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/translatedTextScrollView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:scaleType="center"
android:textSize="15sp"
android:text="@Nim独立开发者"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider2" />
<ProgressBar
android:id="@ id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="30dp"
android:layout_height="30dp"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="@ id/targetLangSelector"
app:layout_constraintTop_toTopOf="@id/targetLangSelector"
app:layout_constraintBottom_toBottomOf="@id/targetLangSelector" />
<TextView
android:id="@ id/progressText"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/downloading_model_files"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@ id/progressBar"
app:layout_constraintBottom_toBottomOf="@id/progressBar"
android:gravity="center_vertical"
app:layout_constraintTop_toTopOf="@id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.3 效果图
2.4 开发添加文本识别的功能
2.4.1 实例化文本检测器
代码语言:java复制private val detector = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
init {
lifecycle.addObserver(detector)
}
还没写业务code,待更新,未续..