Skip to content

CircleToSearch with multi search engine support for all android

Kotlin

added 1x1/2x2/3x3 Tiled Scanning to detect the small QR codes for more accuracy

AKS Labse82a58510a58
diff --git a/app/src/main/java/com/akslabs/circletosearch/utils/QrScanner.kt b/app/src/main/java/com/akslabs/circletosearch/utils/QrScanner.kt
index 24fa256..72d396c 100644
--- a/app/src/main/java/com/akslabs/circletosearch/utils/QrScanner.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/utils/QrScanner.kt
@@ -60,41 +60,53 @@ object QrScanner {
val w = bitmap.width
val h = bitmap.height
- // 1. FULL SCAN
- val fullPixels = IntArray(w * h)
- bitmap.getPixels(fullPixels, 0, w, 0, 0, w, h)
- processResults(scanLuminanceSource(RGBLuminanceSource(w, h, fullPixels)), 0, 0)
- android.util.Log.d("CircleToSearch", "QrScanner: Full scan found ${allResults.size} codes")
-
- // 2. TILED SCAN (2x2 Overlapping Quadrants)
- // Using ~65% size to ensure 15% middle overlap
- val tileW = (w * 0.65f).toInt()
- val tileH = (h * 0.65f).toInt()
-
- val tiles = listOf(
- android.graphics.Rect(0, 0, tileW, tileH), // Top Left
- android.graphics.Rect(w - tileW, 0, w, tileH), // Top Right
- android.graphics.Rect(0, h - tileH, tileW, h), // Bottom Left
- android.graphics.Rect(w - tileW, h - tileH, w, h) // Bottom Right
- )
-
- tiles.forEachIndexed { index, rect ->
+ // Load pixels once
+ val pixels = IntArray(w * h)
+ bitmap.getPixels(pixels, 0, w, 0, 0, w, h)
+ val baseSource = RGBLuminanceSource(w, h, pixels)
+
+ // Define all tiles for 3 levels of zoom
+ val tileRegions = mutableListOf<android.graphics.Rect>()
+
+ // Level 1: Full-screen (1 tile)
+ tileRegions.add(android.graphics.Rect(0, 0, w, h))
+
+ // Level 2: 2x2 grid (4 tiles, ~65% size for overlap)
+ val l2W = (w * 0.65f).toInt()
+ val l2H = (h * 0.65f).toInt()
+ tileRegions.add(android.graphics.Rect(0, 0, l2W, l2H))
+ tileRegions.add(android.graphics.Rect(w - l2W, 0, w, l2H))
+ tileRegions.add(android.graphics.Rect(0, h - l2H, l2W, h))
+ tileRegions.add(android.graphics.Rect(w - l2W, h - l2H, w, h))
+
+ // Level 3: 3x3 grid (9 tiles, ~42% size for overlap)
+ val l3W = (w * 0.42f).toInt()
+ val l3H = (h * 0.42f).toInt()
+ val xOffs = listOf(0, (w - l3W) / 2, w - l3W)
+ val yOffs = listOf(0, (h - l3H) / 2, h - l3H)
+ for (yo in yOffs) {
+ for (xo in xOffs) {
+ tileRegions.add(android.graphics.Rect(xo, yo, xo + l3W, yo + l3H))
+ }
+ }
+
+ // Execute all 14 passes
+ tileRegions.forEachIndexed { index, rect ->
try {
- val tw = rect.width()
- val th = rect.height()
- val tilePixels = IntArray(tw * th)
- bitmap.getPixels(tilePixels, 0, tw, rect.left, rect.top, tw, th)
-
- val results = scanLuminanceSource(RGBLuminanceSource(tw, th, tilePixels))
- val beforeCount = allResults.size
+ val subSource = baseSource.crop(rect.left, rect.top, rect.width(), rect.height())
+ val results = scanLuminanceSource(subSource)
+ val before = allResults.size
processResults(results, rect.left, rect.top)
- android.util.Log.d("CircleToSearch", "QrScanner: Tile $index found ${allResults.size - beforeCount} NEW codes")
+
+ if (allResults.size > before) {
+ android.util.Log.d("CircleToSearch", "QrScanner: Pass $index (Rect: $rect) found ${allResults.size - before} NEW codes")
+ }
} catch (e: Exception) {
- android.util.Log.e("CircleToSearch", "QrScanner: Tile $index failed", e)
+ android.util.Log.e("CircleToSearch", "QrScanner: Pass $index failed", e)
}
}
- android.util.Log.d("CircleToSearch", "QrScanner: Tiled scan COMPLETE. Total codes: ${allResults.size}")
+ android.util.Log.d("CircleToSearch", "QrScanner: Multi-res scan COMPLETE. Total codes: ${allResults.size}")
return allResults
} catch (e: Exception) {
android.util.Log.e("CircleToSearch", "QrScanner: Fatal error in scanBitmapAll", e)