Skip to content

Commit 418ba34

Browse files
authored
Merge pull request #9 from kuamanet/minor_updates
- Fixed hard-coded path for cropped documents
2 parents ce59419 + 9c64dbd commit 418ba34

24 files changed

+282
-220
lines changed

.idea/gradle.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Not Released
22

3+
# 0.1.8
4+
- Fixed hard-coded path for cropped documents
5+
- Move to `ViewBindings`
6+
- Bump kotlin to `1.4.32`
7+
- Fix deprecated `startActivityForResult`
8+
- Fix various warnings
9+
310
# 0.1.7
411
- fixed close buttons
512

app/build.gradle

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
apply plugin: "com.android.library"
22
apply plugin: "kotlin-android"
3-
apply plugin: "kotlin-android-extensions"
43
apply plugin: "com.diffplug.spotless"
54
apply plugin: "maven-publish"
65

76
android {
8-
compileSdkVersion 29
9-
buildToolsVersion "30.0.0"
7+
compileSdkVersion 30
8+
buildToolsVersion "30.0.3"
109

1110
defaultConfig {
1211

1312
minSdkVersion 23
14-
targetSdkVersion 29
13+
targetSdkVersion 30
1514
versionCode project.property("version_code").toInteger()
1615
versionName project.property("version_name") as String
1716

@@ -33,38 +32,45 @@ android {
3332
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
3433
}
3534
}
35+
36+
buildFeatures {
37+
viewBinding true
38+
}
3639
}
3740

3841
dependencies {
3942
implementation fileTree(dir: "libs", include: ["*.jar"])
4043
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
41-
implementation "androidx.core:core-ktx:1.3.0"
42-
implementation "androidx.appcompat:appcompat:1.1.0"
44+
implementation "androidx.core:core-ktx:1.3.2"
45+
implementation "androidx.appcompat:appcompat:1.2.0"
4346
api "com.github.kuamanet:android-native-opencv:0.1"
4447
// ViewModel
45-
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
48+
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
4649
// LiveData
47-
api "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
50+
api "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
4851
// Lifecycles only (without ViewModel or LiveData)
49-
api "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
52+
api "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
5053

51-
api "androidx.activity:activity-ktx:1.1.0"
54+
implementation "androidx.activity:activity-ktx:1.2.2"
55+
implementation "androidx.fragment:fragment-ktx:1.3.3"
5256

5357
// CameraX core library using camera2 implementation
54-
implementation "androidx.camera:camera-camera2:1.1.0-alpha02"
58+
implementation "androidx.camera:camera-camera2:1.1.0-alpha04"
5559
// CameraX Lifecycle Library
56-
implementation "androidx.camera:camera-lifecycle:1.1.0-alpha02"
60+
implementation "androidx.camera:camera-lifecycle:1.1.0-alpha04"
5761
// CameraX View class
58-
implementation "androidx.camera:camera-view:1.0.0-alpha22"
62+
implementation "androidx.camera:camera-view:1.0.0-alpha24"
5963

6064
// zoomable image view
6165
implementation "com.github.chrisbanes:PhotoView:2.3.0"
62-
implementation 'com.google.android.material:material:1.1.0'
63-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
66+
implementation 'com.google.android.material:material:1.3.0'
67+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
68+
69+
implementation "androidx.exifinterface:exifinterface:1.3.2"
6470

65-
testImplementation "junit:junit:4.13"
66-
androidTestImplementation "androidx.test.ext:junit:1.1.1"
67-
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
71+
testImplementation "junit:junit:4.13.2"
72+
androidTestImplementation "androidx.test.ext:junit:1.1.2"
73+
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
6874

6975
}
7076

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
package net.kuama.scanner.data
1+
package net.kuama.documentscanner.data
22

3+
import android.util.Log
34
import org.opencv.core.Point
45
import org.opencv.core.Size
56

6-
data class Corners(val corners: List<Point?>, val size: Size)
7+
data class Corners(val points: List<Point>, val size: Size) {
8+
fun log() {
9+
10+
Log.d(
11+
javaClass.simpleName,
12+
"size: ${size.width}x${size.height} - tl: ${tl.x}, ${tl.y} - tr: ${tr.x}, ${tr.y} - br: ${br.x}, ${br.y} - bl: ${bl.x}, ${bl.y}"
13+
)
14+
}
15+
16+
val tl: Point = points[0]
17+
val tr: Point = points[1]
18+
val br: Point = points[2]
19+
val bl: Point = points[3]
20+
}

app/src/main/java/net/kuama/documentscanner/data/Loader.kt app/src/main/java/net/kuama/documentscanner/data/OpenCVLoader.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ enum class OpenCvStatus {
1010
LOADED, ERROR
1111
}
1212

13-
class Loader(context: Context) {
14-
private val reference = WeakReference<Context>(context)
13+
class OpenCVLoader(context: Context) {
14+
private val reference = WeakReference(context)
1515

1616
private var onLoad: ((OpenCvStatus) -> Unit)? = null
1717
private val mLoaderCallback = object : BaseLoaderCallback(context.applicationContext) {

app/src/main/java/net/kuama/documentscanner/domain/FindPaperSheetContours.kt

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package net.kuama.documentscanner.domain
22

33
import android.graphics.Bitmap
4+
import net.kuama.documentscanner.data.Corners
45
import net.kuama.documentscanner.support.Either
56
import net.kuama.documentscanner.support.Left
67
import net.kuama.documentscanner.support.Right
7-
import net.kuama.documentscanner.support.shape
8-
import net.kuama.scanner.data.Corners
8+
import net.kuama.documentscanner.extensions.shape
99
import org.opencv.android.Utils
10-
import org.opencv.core.*
10+
import org.opencv.core.Mat
11+
import org.opencv.core.MatOfPoint
12+
import org.opencv.core.Size
1113
import org.opencv.imgproc.Imgproc
12-
import java.util.*
13-
import kotlin.Comparator
14-
import kotlin.collections.ArrayList
1514

1615
class FindPaperSheetContours : UseCase<Pair<Bitmap, Corners?>, FindPaperSheetContours.Params>() {
1716
class Params(
@@ -67,9 +66,9 @@ class FindPaperSheetContours : UseCase<Pair<Bitmap, Corners?>, FindPaperSheetCon
6766
.toTypedArray()
6867
.toMutableList()
6968

70-
contours.sortWith(Comparator { lhs, rhs ->
69+
contours.sortWith { lhs, rhs ->
7170
Imgproc.contourArea(rhs).compareTo(Imgproc.contourArea(lhs))
72-
})
71+
}
7372

7473
if (params.returnOriginalMat) {
7574
Utils.matToBitmap(original, params.bitmap)

app/src/main/java/net/kuama/documentscanner/domain/PerspectiveTransform.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package net.kuama.documentscanner.domain
22

33
import android.graphics.Bitmap
4+
import net.kuama.documentscanner.data.Corners
45
import net.kuama.documentscanner.support.Either
56
import net.kuama.documentscanner.support.Left
67
import net.kuama.documentscanner.support.Right
7-
import net.kuama.scanner.data.Corners
88
import org.opencv.android.Utils
99
import org.opencv.core.CvType
1010
import org.opencv.core.Mat
@@ -17,11 +17,12 @@ import kotlin.math.pow
1717
import kotlin.math.sqrt
1818

1919
/**
20-
* Given a set of corners (see [FindPaperSheet]), and a source image,
21-
* crops the corners from the image and transform the shape represented by the corners
20+
* Given a set of corners, and a source image,
21+
* crops the corners from the image and transforms the shape represented by the corners
2222
* into a rectangle
2323
*/
2424
class PerspectiveTransform : UseCase<Bitmap, PerspectiveTransform.Params>() {
25+
2526
class Params(val bitmap: Bitmap, val corners: Corners)
2627

2728
override suspend fun run(params: Params): Either<Failure, Bitmap> = try {
@@ -33,10 +34,10 @@ class PerspectiveTransform : UseCase<Bitmap, PerspectiveTransform.Params>() {
3334

3435
val orderedCorners = sortPoints(
3536
arrayOf(
36-
params.corners.corners[0] ?: error("Invalid corners"),
37-
params.corners.corners[1] ?: error("Invalid corners"),
38-
params.corners.corners[2] ?: error("Invalid corners"),
39-
params.corners.corners[3] ?: error("Invalid corners")
37+
params.corners.tl,
38+
params.corners.tr,
39+
params.corners.br,
40+
params.corners.bl
4041
)
4142
)
4243

app/src/main/java/net/kuama/documentscanner/domain/UriToBitmap.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.kuama.documentscanner.domain
22

33
import android.content.ContentResolver
4-
import android.graphics.*
5-
import android.media.ExifInterface
4+
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
6+
import android.graphics.Matrix
7+
import androidx.exifinterface.media.ExifInterface
68
import android.net.Uri
79
import android.os.ParcelFileDescriptor
810
import net.kuama.documentscanner.support.Either

app/src/main/java/net/kuama/documentscanner/domain/UseCase.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ abstract class UseCase<Type, in Params> where Type : Any? {
1111
abstract suspend fun run(params: Params): Either<Failure, Type>
1212

1313
operator fun invoke(params: Params, onResult: (Either<Failure, Type>) -> Unit = {}) {
14-
val job = GlobalScope.async { run(params) }
14+
val job = GlobalScope.async(Dispatchers.IO) { run(params) }
1515
GlobalScope.launch(Dispatchers.Main) { onResult(job.await()) }
1616
}
17-
18-
class None
1917
}
2018

2119
class Failure(val origin: Throwable)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.kuama.documentscanner.extensions
2+
3+
import android.view.View
4+
import androidx.appcompat.app.AppCompatActivity
5+
6+
private const val FULLSCREEN_FLAGS = (View.SYSTEM_UI_FLAG_IMMERSIVE
7+
// Set the content to appear under the system bars so that the
8+
// content doesn't resize when the system bars hide and show.
9+
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
10+
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
11+
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
12+
// Hide the nav bar and status bar
13+
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
14+
or View.SYSTEM_UI_FLAG_FULLSCREEN)
15+
16+
internal fun AppCompatActivity.triggerFullscreen() {
17+
window.decorView.systemUiVisibility = FULLSCREEN_FLAGS
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.kuama.documentscanner.extensions
2+
3+
import android.graphics.Bitmap
4+
import java.io.ByteArrayOutputStream
5+
6+
internal fun Bitmap.toByteArray(): ByteArray {
7+
ByteArrayOutputStream().apply {
8+
compress(Bitmap.CompressFormat.JPEG, 100, this)
9+
return toByteArray()
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.kuama.documentscanner.extensions
2+
3+
import android.content.Context
4+
import net.kuama.documentscanner.R
5+
import java.io.File
6+
7+
/** Use external media if it is available, our app's file directory otherwise */
8+
internal val Context.outputDirectory: File
9+
get() {
10+
val appContext = applicationContext
11+
val mediaDir = externalMediaDirs.firstOrNull()?.let {
12+
File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() }
13+
}
14+
return if (mediaDir != null && mediaDir.exists())
15+
mediaDir else appContext.filesDir
16+
}

app/src/main/java/net/kuama/documentscanner/support/MatOfPoint.kt app/src/main/java/net/kuama/documentscanner/extensions/MatOfPoint.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.kuama.documentscanner.support
1+
package net.kuama.documentscanner.extensions
22

33
import org.opencv.core.MatOfPoint
44
import org.opencv.core.MatOfPoint2f
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.kuama.documentscanner.extensions
2+
3+
import android.view.View
4+
import android.view.ViewTreeObserver.OnGlobalLayoutListener
5+
6+
internal inline fun View.waitForLayout(crossinline yourAction: () -> Unit) {
7+
val vto = viewTreeObserver
8+
vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
9+
override fun onGlobalLayout() {
10+
when {
11+
vto.isAlive -> {
12+
vto.removeOnGlobalLayoutListener(this)
13+
yourAction()
14+
}
15+
else -> viewTreeObserver.removeOnGlobalLayoutListener(this)
16+
}
17+
}
18+
})
19+
}

0 commit comments

Comments
 (0)