Skip to content

Commit 550a818

Browse files
committed
Convert sample to compose
Various improvements. 1. Compose to be up to date with android standards. 2. Kotlin since Java is no longer the programming language for Android. 3. Coroutines instead of threads for better multithreaded management. 4. Flows instead of LiveData 5. Document the code somewhat.
1 parent 8c61e0f commit 550a818

19 files changed

Lines changed: 511 additions & 384 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ buildscript {
88
}
99
dependencies {
1010
classpath(libs.gradle)
11+
classpath(libs.kotlin.gradle.plugin)
1112

1213
// NOTE: Do not place your application dependencies here; they belong
1314
// in the individual module build.gradle files

gradle/libs.versions.toml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ test = "1.6.1"
1616
tesseract4android = "4.7.0"
1717
coreKtx = "1.13.1"
1818
kotlin = "2.0.0"
19+
kotlinGradlePlugin = "1.9.0"
20+
lifecycleRuntimeKtx = "2.8.3"
21+
activityCompose = "1.9.0"
22+
composeBom = "2024.06.00"
23+
window = "1.3.0"
24+
adaptiveAndroid = "1.0.0-beta04"
1925

2026
[libraries]
2127
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" }
@@ -25,8 +31,10 @@ androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", vers
2531
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidJUnit" }
2632
androidx-lifecycle-livedata = { module = "androidx.lifecycle:lifecycle-livedata", version.ref = "lifecycleLivedata" }
2733
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycleLivedata" }
34+
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose" }
2835
androidx-rules = { module = "androidx.test:rules", version.ref = "test" }
2936
androidx-runner = { module = "androidx.test:runner", version.ref = "test" }
37+
androidx-window = { module = "androidx.window:window", version.ref = "window" }
3038
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
3139
junit = { module = "junit:junit", version.ref = "junit" }
3240
material = { module = "com.google.android.material:material", version.ref = "material" }
@@ -38,6 +46,18 @@ tesseract4android-jitpack-openmp = { group = "cz.adaptech.tesseract4android", na
3846
tesseract4android-local = { group = "cz.adaptech", name = "tesseract4android", version.ref = "tesseract4android" }
3947
tesseract4android-local-openmp = { group = "cz.adaptech", name = "tesseract4android-openmp", version.ref = "tesseract4android" }
4048
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
49+
kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" }
50+
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
51+
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
52+
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
53+
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
54+
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
55+
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
56+
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
57+
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
58+
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
59+
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
60+
androidx-adaptive-android = { group = "androidx.compose.material3.adaptive", name = "adaptive-android", version.ref = "adaptiveAndroid" }
4161

4262
[plugins]
43-
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
63+
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android" }

sample/build.gradle.kts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ android {
1515
versionName = "1.0"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary = true
20+
}
1821
}
1922

2023
buildTypes {
2124
release {
22-
isMinifyEnabled = false
2325
proguardFiles(
2426
getDefaultProguardFile("proguard-android-optimize.txt"),
2527
"proguard-rules.pro"
@@ -32,10 +34,19 @@ android {
3234
}
3335
buildFeatures {
3436
viewBinding = true
37+
compose = true
3538
}
3639
kotlinOptions {
3740
jvmTarget = "17"
3841
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion = "1.5.1"
44+
}
45+
packaging {
46+
resources {
47+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
48+
}
49+
}
3950
}
4051

4152
// In case you are using dependency on local library (the project(":tesseract4android") below),
@@ -64,13 +75,26 @@ dependencies {
6475
// Which flavor to use is determined by missingDimensionStrategy parameter above.
6576
//implementation(project(":tesseract4android"))
6677

67-
implementation(libs.androidx.appcompat)
6878
implementation(libs.material)
69-
implementation(libs.androidx.constraintlayout)
70-
implementation(libs.androidx.lifecycle.livedata)
71-
implementation(libs.androidx.lifecycle.viewmodel)
7279
implementation(libs.androidx.core.ktx)
80+
implementation(libs.androidx.activity.compose)
81+
implementation(platform(libs.androidx.compose.bom))
82+
implementation(libs.androidx.ui)
83+
implementation(libs.androidx.ui.graphics)
84+
implementation(libs.androidx.ui.tooling.preview)
85+
implementation(libs.androidx.material3)
86+
implementation(libs.androidx.window)
87+
88+
implementation(libs.androidx.lifecycle.runtime.ktx)
89+
implementation(libs.androidx.lifecycle.viewmodel)
90+
implementation(libs.androidx.lifecycle.viewmodel.compose)
91+
implementation(libs.androidx.adaptive.android)
92+
7393
testImplementation(libs.junit)
7494
androidTestImplementation(libs.androidx.junit)
7595
androidTestImplementation(libs.androidx.espresso.core)
96+
androidTestImplementation(platform(libs.androidx.compose.bom))
97+
androidTestImplementation(libs.androidx.ui.test.junit4)
98+
debugImplementation(libs.androidx.ui.tooling)
99+
debugImplementation(libs.androidx.ui.test.manifest)
76100
}

sample/src/androidTest/java/cz/adaptech/tesseract4android/sample/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package cz.adaptech.tesseract4android.sample
22

33
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
47
import androidx.appcompat.app.AppCompatActivity
5-
import cz.adaptech.tesseract4android.sample.ui.main.MainFragment
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.material3.Scaffold
11+
import androidx.compose.ui.Modifier
12+
import cz.adaptech.tesseract4android.sample.ui.main.MainView
13+
import cz.adaptech.tesseract4android.sample.ui.theme.Tesseract4AndroidTheme
614

7-
class MainActivity : AppCompatActivity() {
15+
class MainActivity : ComponentActivity() {
816
override fun onCreate(savedInstanceState: Bundle?) {
917
super.onCreate(savedInstanceState)
10-
setContentView(R.layout.activity_main)
11-
if (savedInstanceState == null) {
12-
supportFragmentManager.beginTransaction()
13-
.replace(R.id.container, MainFragment.newInstance())
14-
.commitNow()
18+
enableEdgeToEdge()
19+
setContent {
20+
MainView()
1521
}
1622
}
1723
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cz.adaptech.tesseract4android.sample
2+
3+
4+
/**
5+
* Represents the various states that the OCR can be in.
6+
*
7+
* @since 2024/07/22
8+
* @author Clocks
9+
*/
10+
sealed interface OCRState {
11+
/**
12+
* OCR is loading up.
13+
*/
14+
data object Loading : OCRState
15+
16+
/**
17+
* OCR is prepared.
18+
*
19+
* @param version Version of tesseract
20+
* @param flavour Build flavour of tesseract
21+
*/
22+
data class StartUp(val version: String, val flavour: String) : OCRState
23+
24+
/**
25+
* OCR has been stopped.
26+
*/
27+
data object Stopped : OCRState
28+
29+
/**
30+
* OCR is being stopped.
31+
*/
32+
data object Stopping : OCRState
33+
34+
/**
35+
* OCR is starting up.
36+
*/
37+
data object Processing : OCRState
38+
39+
/**
40+
* OCR is currently in process.
41+
*
42+
* @param progress 0-100 progress indication.
43+
*/
44+
data class Progress(val progress: Int) : OCRState
45+
46+
/**
47+
* OCR has completed its task.
48+
*
49+
* @param time How many seconds it took to process the image.
50+
*/
51+
data class Finished(val time: Float) : OCRState
52+
}

sample/src/main/java/cz/adaptech/tesseract4android/sample/ui/main/MainFragment.kt

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)