Files
QFT/android/app/src/main/java/com/qft/optical/LockRect.kt
T

113 lines
4.2 KiB
Kotlin

package com.qft.optical
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
/**
* Pure geometry used by the optical scanner. Kept Android-free so it can be
* unit tested on the host JVM.
*/
data class LockRect(
val left: Int,
val top: Int,
val width: Int,
val height: Int,
) {
val right: Int get() = left + width
val bottom: Int get() = top + height
fun clamp(frameWidth: Int, frameHeight: Int): LockRect {
val l = left.coerceIn(0, (frameWidth - 1).coerceAtLeast(0))
val t = top.coerceIn(0, (frameHeight - 1).coerceAtLeast(0))
val r = right.coerceIn(l + 1, frameWidth.coerceAtLeast(l + 1))
val b = bottom.coerceIn(t + 1, frameHeight.coerceAtLeast(t + 1))
return LockRect(l, t, r - l, b - t)
}
fun expand(frameWidth: Int, frameHeight: Int, factor: Float): LockRect {
val f = factor.coerceAtLeast(1f)
val cx = left + width / 2f
val cy = top + height / 2f
val w = (width * f).toInt().coerceAtLeast(96)
val h = (height * f).toInt().coerceAtLeast(96)
return LockRect((cx - w / 2f).toInt(), (cy - h / 2f).toInt(), w, h)
.clamp(frameWidth, frameHeight)
}
fun smoothToward(target: LockRect, alpha: Float): LockRect {
val a = alpha.coerceIn(0f, 1f)
fun mix(x: Int, y: Int) = (x + (y - x) * a).toInt()
return LockRect(
mix(left, target.left),
mix(top, target.top),
mix(width, target.width),
mix(height, target.height),
)
}
fun areaFraction(frameWidth: Int, frameHeight: Int): Float {
val total = frameWidth.toLong() * frameHeight.toLong()
if (total <= 0L) return 1f
return ((width.toLong() * height.toLong()).toDouble() / total.toDouble()).toFloat()
}
fun materiallyDiffers(other: LockRect?): Boolean {
if (other == null) return true
val d = abs(left - other.left) + abs(top - other.top) +
abs(width - other.width) + abs(height - other.height)
return d > max(width, height) / 18
}
/** Intersection-over-union for consecutive finder-point measurements. */
fun overlap(other: LockRect?): Float {
if (other == null) return 0f
val l = max(left, other.left)
val t = max(top, other.top)
val r = min(right, other.right)
val b = min(bottom, other.bottom)
val intersection = max(0, r - l).toLong() * max(0, b - t).toLong()
val union = width.toLong() * height.toLong() + other.width.toLong() * other.height.toLong() - intersection
return if (union <= 0L) 0f else (intersection.toDouble() / union.toDouble()).toFloat()
}
companion object {
/**
* ZXing normally returns the finder-pattern centers. Their bounding box
* is smaller than the full symbol, so enlarge to roughly 1.28x and force
* a square ROI. This keeps the complete quiet zone while discarding most
* of the camera frame after lock-on.
*/
fun fromPoints(
points: List<Pair<Float, Float>>,
frameWidth: Int,
frameHeight: Int,
): LockRect? {
if (points.size < 3 || frameWidth < 2 || frameHeight < 2) return null
val minX = points.minOf { it.first }
val maxX = points.maxOf { it.first }
val minY = points.minOf { it.second }
val maxY = points.maxOf { it.second }
val span = max(maxX - minX, maxY - minY)
if (span < 24f) return null
val side = (span * 1.28f).toInt().coerceAtLeast(128)
val cx = (minX + maxX) * 0.5f
val cy = (minY + maxY) * 0.5f
return LockRect(
(cx - side / 2f).toInt(),
(cy - side / 2f).toInt(),
side,
side,
).clamp(frameWidth, frameHeight)
}
fun centeredSearch(frameWidth: Int, frameHeight: Int, fraction: Float = 0.92f): LockRect {
val f = fraction.coerceIn(0.5f, 1f)
val w = (frameWidth * f).toInt().coerceAtLeast(1)
val h = (frameHeight * f).toInt().coerceAtLeast(1)
return LockRect((frameWidth - w) / 2, (frameHeight - h) / 2, w, h)
}
}
}