Skip to content

Commit

Permalink
Dirty Kotlin Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Doomsdayrs committed Jul 22, 2024
1 parent c2c8de6 commit 8c61e0f
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 370 deletions.
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ material = "1.11.0"
constraintlayout = "2.1.4"
test = "1.6.1"
tesseract4android = "4.7.0"
coreKtx = "1.13.1"
kotlin = "2.0.0"

[libraries]
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" }
Expand All @@ -35,5 +37,7 @@ tesseract4android-jitpack = { group = "cz.adaptech.tesseract4android", name = "t
tesseract4android-jitpack-openmp = { group = "cz.adaptech.tesseract4android", name = "tesseract4android-openmp", version.ref = "tesseract4android" }
tesseract4android-local = { group = "cz.adaptech", name = "tesseract4android", version.ref = "tesseract4android" }
tesseract4android-local-openmp = { group = "cz.adaptech", name = "tesseract4android-openmp", version.ref = "tesseract4android" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }

[plugins]
[plugins]
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
5 changes: 5 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("com.android.application")
alias(libs.plugins.jetbrains.kotlin.android)
}

android {
Expand Down Expand Up @@ -32,6 +33,9 @@ android {
buildFeatures {
viewBinding = true
}
kotlinOptions {
jvmTarget = "17"
}
}

// In case you are using dependency on local library (the project(":tesseract4android") below),
Expand Down Expand Up @@ -65,6 +69,7 @@ dependencies {
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.lifecycle.livedata)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.core.ktx)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cz.adaptech.tesseract4android.sample

import android.content.Context
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

object Assets {
/**
* Returns locally accessible directory where our assets are extracted.
*/
fun getLocalDir(context: Context): File {
return context.filesDir
}

/**
* Returns locally accessible directory path which contains the "tessdata" subdirectory
* with *.traineddata files.
*/
@JvmStatic
fun getTessDataPath(context: Context): String {
return getLocalDir(context).absolutePath
}

@JvmStatic
fun getImageFile(context: Context): File {
return File(getLocalDir(context), Config.IMAGE_NAME)
}

@JvmStatic
fun getImageBitmap(context: Context): Bitmap? {
return BitmapFactory.decodeFile(getImageFile(context).absolutePath)
}

@JvmStatic
fun extractAssets(context: Context) {
val am = context.assets

val localDir = getLocalDir(context)
if (!localDir.exists() && !localDir.mkdir()) {
throw RuntimeException("Can't create directory $localDir")
}

val tessDir = File(getTessDataPath(context), "tessdata")
if (!tessDir.exists() && !tessDir.mkdir()) {
throw RuntimeException("Can't create directory $tessDir")
}

// Extract all assets to our local directory.
// All *.traineddata into "tessdata" subdirectory, other files into root.
try {
for (assetName in am.list("")!!) {
val targetFile = if (assetName.endsWith(".traineddata")) {
File(tessDir, assetName)
} else {
File(localDir, assetName)
}
if (!targetFile.exists()) {
copyFile(am, assetName, targetFile)
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}

private fun copyFile(
am: AssetManager, assetName: String,
outFile: File
) {
try {
am.open(assetName).use { `in` ->
FileOutputStream(outFile).use { out ->
val buffer = ByteArray(1024)
var read: Int
while ((`in`.read(buffer).also { read = it }) != -1) {
out.write(buffer, 0, read)
}
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cz.adaptech.tesseract4android.sample

import com.googlecode.tesseract.android.TessBaseAPI

object Config {
const val TESS_ENGINE: Int = TessBaseAPI.OEM_LSTM_ONLY

const val TESS_LANG: String = "eng"

const val IMAGE_NAME: String = "sample.jpg"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cz.adaptech.tesseract4android.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cz.adaptech.tesseract4android.sample.ui.main.MainFragment

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
}
}
}

This file was deleted.

Loading

0 comments on commit 8c61e0f

Please sign in to comment.