diff --git a/app/src/androidTest/java/org/mydomain/myscan/DocumentDetectionTest.kt b/app/src/androidTest/java/org/mydomain/myscan/DocumentDetectionTest.kt new file mode 100644 index 0000000..e8eb4f6 --- /dev/null +++ b/app/src/androidTest/java/org/mydomain/myscan/DocumentDetectionTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2025 Pierre-Yves Nicolas + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ +package org.mydomain.myscan + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.util.Log +import androidx.test.core.app.ApplicationProvider +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* +import org.opencv.android.OpenCVLoader +import java.io.File +import java.io.FileOutputStream + +@RunWith(AndroidJUnit4::class) +class DocumentDetectionTest { + @Test + fun extractDocumentFromImage() { + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("org.mydomain.myscan", appContext.packageName) + + val context = ApplicationProvider.getApplicationContext() + val segmentationService = ImageSegmentationService(context) + segmentationService.initialize() + OpenCVLoader.initLocal() + + listOf("img01.jpg", "img02.jpg", "img03.jpg").forEach { imageFileName -> + val inputStream = context.assets.open("uncropped/$imageFileName") + val bitmap = BitmapFactory.decodeStream(inputStream) + var outputBitmap: Bitmap? = null + + val segmentationResult = segmentationService.runSegmentationAndReturn(bitmap, 0) + if (segmentationResult != null) { + val mask = segmentationResult.segmentation.toBinaryMask() + val quad = detectDocumentQuad(mask) + if (quad != null) { + val resizedQuad = + quad.scaledTo(mask.width, mask.height, bitmap.width, bitmap.height) + outputBitmap = extractDocument(bitmap, resizedQuad) + val file = File(context.getExternalFilesDir(null), imageFileName) + FileOutputStream(file).use { + outputBitmap.compress(Bitmap.CompressFormat.JPEG, 95, it) + } + Log.i("DocumentDetectionTest", "Image saved to ${file.absolutePath}") + } + } + if (outputBitmap == null) { + fail("Failed to extract document from image $imageFileName") + } + } + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/org/mydomain/myscan/ExampleInstrumentedTest.kt b/app/src/androidTest/java/org/mydomain/myscan/ExampleInstrumentedTest.kt deleted file mode 100644 index cc9892e..0000000 --- a/app/src/androidTest/java/org/mydomain/myscan/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2025 Pierre-Yves Nicolas - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ -package org.mydomain.myscan - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("org.mydomain.myscan", appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/debug/assets/uncropped/img01.jpg b/app/src/debug/assets/uncropped/img01.jpg new file mode 100644 index 0000000..56ab9a9 Binary files /dev/null and b/app/src/debug/assets/uncropped/img01.jpg differ diff --git a/app/src/debug/assets/uncropped/img02.jpg b/app/src/debug/assets/uncropped/img02.jpg new file mode 100644 index 0000000..f6d7a40 Binary files /dev/null and b/app/src/debug/assets/uncropped/img02.jpg differ diff --git a/app/src/debug/assets/uncropped/img03.jpg b/app/src/debug/assets/uncropped/img03.jpg new file mode 100644 index 0000000..cd65960 Binary files /dev/null and b/app/src/debug/assets/uncropped/img03.jpg differ