V0.4.0加入锁定二维码
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.qft.optical
|
||||
|
||||
/** RFC 9285 Base45 decoder. Alphabet maps directly to QR alphanumeric mode. */
|
||||
object Base45 {
|
||||
private const val ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
||||
private val reverse = IntArray(128) { -1 }.apply {
|
||||
ALPHABET.forEachIndexed { index, c -> this[c.code] = index }
|
||||
}
|
||||
|
||||
fun decode(text: String): ByteArray? {
|
||||
if (text.isEmpty()) return ByteArray(0)
|
||||
if (text.length % 3 == 1) return null
|
||||
val out = ByteArray((text.length / 3) * 2 + if (text.length % 3 == 2) 1 else 0)
|
||||
var i = 0
|
||||
var o = 0
|
||||
while (i + 2 < text.length) {
|
||||
val a = value(text[i]) ?: return null
|
||||
val b = value(text[i + 1]) ?: return null
|
||||
val c = value(text[i + 2]) ?: return null
|
||||
val n = a + b * 45 + c * 45 * 45
|
||||
if (n > 0xFFFF) return null
|
||||
out[o++] = (n / 256).toByte()
|
||||
out[o++] = (n % 256).toByte()
|
||||
i += 3
|
||||
}
|
||||
if (i < text.length) {
|
||||
val a = value(text[i]) ?: return null
|
||||
val b = value(text[i + 1]) ?: return null
|
||||
val n = a + b * 45
|
||||
if (n > 0xFF) return null
|
||||
out[o++] = n.toByte()
|
||||
}
|
||||
return if (o == out.size) out else out.copyOf(o)
|
||||
}
|
||||
|
||||
private fun value(c: Char): Int? {
|
||||
if (c.code >= reverse.size) return null
|
||||
val v = reverse[c.code]
|
||||
return if (v >= 0) v else null
|
||||
}
|
||||
}
|
||||
@@ -2,42 +2,63 @@ package com.qft.optical
|
||||
|
||||
import androidx.camera.core.ImageProxy
|
||||
|
||||
/**
|
||||
* Copies only the Y plane.
|
||||
*
|
||||
* QFT deliberately keeps the camera buffer in sensor orientation. QR finder
|
||||
* patterns are rotation independent, and the optical 2x2 layout is still a 2x2
|
||||
* layout after a 90° rotation. Avoiding a second full-frame rotate/copy removes
|
||||
* almost half of the memory traffic in the scanner hot path on portrait phones.
|
||||
*/
|
||||
data class GrayFrame(val width: Int, val height: Int, val luma: ByteArray) {
|
||||
companion object {
|
||||
fun from(image: ImageProxy): GrayFrame {
|
||||
val plane = image.planes[0]
|
||||
val src = plane.buffer.duplicate()
|
||||
val rowStride = plane.rowStride
|
||||
val pixelStride = plane.pixelStride
|
||||
val w = image.width
|
||||
val h = image.height
|
||||
val raw = ByteArray(w * h)
|
||||
/** Dense Y-plane crop used by ZXing. */
|
||||
data class GrayFrame(
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
val luma: ByteArray,
|
||||
val originX: Int,
|
||||
val originY: Int,
|
||||
val fullWidth: Int,
|
||||
val fullHeight: Int,
|
||||
val rotationDegrees: Int,
|
||||
)
|
||||
|
||||
if (pixelStride == 1 && rowStride == w) {
|
||||
src.position(0)
|
||||
src.get(raw, 0, raw.size)
|
||||
} else if (pixelStride == 1) {
|
||||
for (y in 0 until h) {
|
||||
src.position(y * rowStride)
|
||||
src.get(raw, y * w, w)
|
||||
}
|
||||
} else {
|
||||
for (y in 0 until h) {
|
||||
val rowStart = y * rowStride
|
||||
for (x in 0 until w) {
|
||||
raw[y * w + x] = src.get(rowStart + x * pixelStride)
|
||||
}
|
||||
/**
|
||||
* Reusable luminance scratch buffer. ImageAnalysis calls the analyzer serially,
|
||||
* so one grow-only byte array is enough for the full hot path. This avoids a
|
||||
* ByteArray allocation for every camera frame and materially reduces GC on
|
||||
* Android 7/8 class devices.
|
||||
*/
|
||||
class GrayFrameBuffer {
|
||||
private var scratch = ByteArray(0)
|
||||
|
||||
fun capture(image: ImageProxy, requested: LockRect? = null): GrayFrame {
|
||||
val plane = image.planes[0]
|
||||
val src = plane.buffer.duplicate()
|
||||
val rowStride = plane.rowStride
|
||||
val pixelStride = plane.pixelStride
|
||||
val fullW = image.width
|
||||
val fullH = image.height
|
||||
val crop = (requested ?: LockRect(0, 0, fullW, fullH)).clamp(fullW, fullH)
|
||||
val needed = crop.width * crop.height
|
||||
if (scratch.size < needed) scratch = ByteArray(needed)
|
||||
|
||||
if (pixelStride == 1) {
|
||||
for (y in 0 until crop.height) {
|
||||
val sourceOffset = (crop.top + y) * rowStride + crop.left
|
||||
src.position(sourceOffset)
|
||||
src.get(scratch, y * crop.width, crop.width)
|
||||
}
|
||||
} else {
|
||||
for (y in 0 until crop.height) {
|
||||
val rowStart = (crop.top + y) * rowStride
|
||||
val dst = y * crop.width
|
||||
for (x in 0 until crop.width) {
|
||||
scratch[dst + x] = src.get(rowStart + (crop.left + x) * pixelStride)
|
||||
}
|
||||
}
|
||||
return GrayFrame(w, h, raw)
|
||||
}
|
||||
|
||||
return GrayFrame(
|
||||
width = crop.width,
|
||||
height = crop.height,
|
||||
luma = scratch,
|
||||
originX = crop.left,
|
||||
originY = crop.top,
|
||||
fullWidth = fullW,
|
||||
fullHeight = fullH,
|
||||
rotationDegrees = image.imageInfo.rotationDegrees,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,13 @@ import android.content.pm.PackageManager
|
||||
import android.content.res.ColorStateList
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.hardware.camera2.CameraCharacteristics
|
||||
import android.hardware.camera2.CameraManager
|
||||
import android.hardware.camera2.CaptureRequest
|
||||
import android.net.Network
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Range
|
||||
import android.util.Size
|
||||
import android.view.Gravity
|
||||
import android.view.ViewGroup
|
||||
@@ -19,6 +23,10 @@ import android.widget.ScrollView
|
||||
import android.widget.TextView
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.camera.camera2.interop.Camera2Interop
|
||||
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
|
||||
import androidx.camera.core.Camera
|
||||
import androidx.camera.core.CameraSelector
|
||||
import androidx.camera.core.FocusMeteringAction
|
||||
import androidx.camera.core.ImageAnalysis
|
||||
@@ -39,8 +47,14 @@ class MainActivity : ComponentActivity() {
|
||||
private lateinit var assembler: SessionAssembler
|
||||
|
||||
private var cameraProvider: ProcessCameraProvider? = null
|
||||
private var activeCamera: Camera? = null
|
||||
private var lastLockFocusAt = 0L
|
||||
private var lastLockFocusX = -1f
|
||||
private var lastLockFocusY = -1f
|
||||
private var currentAnalyzer: QrAnalyzer? = null
|
||||
private var qrLockOverlay: QrLockOverlay? = null
|
||||
private var scannerStatsText: TextView? = null
|
||||
private var scannerLockText: TextView? = null
|
||||
private var scannerMode = ScannerMode.NONE
|
||||
private var pendingScannerMode = ScannerMode.NONE
|
||||
private val pairConsumed = AtomicBoolean(false)
|
||||
@@ -126,8 +140,8 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
root.addView(modeCard(
|
||||
title = "电脑 → 手机",
|
||||
badge = "光学零网络 · 4码并行 · 60FPS显示",
|
||||
body = "手机摄像头连续读取电脑动态二维码。Wi-Fi、蓝牙、局域网和互联网都不参与文件数据传输。",
|
||||
badge = "光学零网络 · 单码锁定 · QFT2低密度字母数字QR · 最高60FPS显示",
|
||||
body = "手机先搜索并锁定一个大二维码,之后只分析锁定区域。QFT2 使用 Base45 让 QR 保持字母数字模式并降低模块密度;Wi-Fi、蓝牙、局域网和互联网都不参与文件数据传输。",
|
||||
buttonLabel = "开始光学接收",
|
||||
) { requestScanner(ScannerMode.OPTICAL) })
|
||||
root.addView(space(16))
|
||||
@@ -139,7 +153,7 @@ class MainActivity : ComponentActivity() {
|
||||
) { requestScanner(ScannerMode.PAIR) })
|
||||
root.addView(space(22))
|
||||
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
||||
root.addView(text("QFT v0.3.0-rc3 · Android 7+ 到最新", 12f, MUTED))
|
||||
root.addView(text("QFT v0.4.0-rc2 · Android 7+ 到最新", 12f, MUTED))
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
}
|
||||
|
||||
@@ -158,6 +172,7 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(markerClass = [ExperimentalCamera2Interop::class])
|
||||
private fun startScanner(mode: ScannerMode) {
|
||||
scannerMode = mode
|
||||
pairConsumed.set(false)
|
||||
@@ -166,19 +181,30 @@ class MainActivity : ComponentActivity() {
|
||||
future.addListener({
|
||||
val provider = future.get()
|
||||
cameraProvider = provider
|
||||
val preview = Preview.Builder().build().also { it.surfaceProvider = previewView.surfaceProvider }
|
||||
val profile = if (mode == ScannerMode.PAIR) QrAnalyzer.Profile.PAIR_FAST else QrAnalyzer.Profile.OPTICAL_MULTI
|
||||
val requestedFps = chooseCameraFpsRange()
|
||||
|
||||
// CameraX 1.4.x ImageAnalysis.Builder has no public setTargetFrameRate().
|
||||
// Apply a range actually advertised by the selected back camera via
|
||||
// Camera2Interop to both bound use cases.
|
||||
val previewBuilder = Preview.Builder()
|
||||
Camera2Interop.Extender(previewBuilder).setCaptureRequestOption(
|
||||
CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
|
||||
requestedFps,
|
||||
)
|
||||
val preview = previewBuilder.build().also { it.surfaceProvider = previewView.surfaceProvider }
|
||||
|
||||
val profile = if (mode == ScannerMode.PAIR) QrAnalyzer.Profile.PAIR_FAST else QrAnalyzer.Profile.OPTICAL_LOCKED
|
||||
currentAnalyzer?.close()
|
||||
val analyzer = QrAnalyzer(
|
||||
profile = profile,
|
||||
onTexts = { values ->
|
||||
onCode = { code ->
|
||||
when (scannerMode) {
|
||||
ScannerMode.OPTICAL -> values.forEach(assembler::acceptText)
|
||||
ScannerMode.PAIR -> values.firstOrNull { PairingProtocol.isPairingText(it) }?.let(::acceptPairCode)
|
||||
ScannerMode.OPTICAL -> assembler.acceptText(code.text)
|
||||
ScannerMode.PAIR -> if (PairingProtocol.isPairingText(code.text)) acceptPairCode(code.text)
|
||||
ScannerMode.NONE -> Unit
|
||||
}
|
||||
},
|
||||
onStats = { cameraFps, decodedFps ->
|
||||
onStats = { cameraFps, decodedFps, distinctFps, decodeMs ->
|
||||
runOnUiThread {
|
||||
val quality = when (mode) {
|
||||
ScannerMode.PAIR -> when {
|
||||
@@ -187,38 +213,152 @@ class MainActivity : ComponentActivity() {
|
||||
else -> "相机帧率偏低" to WARNING
|
||||
}
|
||||
ScannerMode.OPTICAL -> when {
|
||||
decodedFps >= cameraFps.coerceAtLeast(1.0) * 1.5 -> "光学链路稳定" to SUCCESS
|
||||
decodedFps >= 5.0 -> "可接收 · 建议保持手机稳定" to ACCENT
|
||||
else -> "识别偏低 · 靠近屏幕或降低 Windows FPS" to WARNING
|
||||
distinctFps >= 18.0 -> "新帧吞吐良好" to SUCCESS
|
||||
distinctFps >= 8.0 -> "可接收 · 继续保持二维码稳定" to ACCENT
|
||||
decodedFps >= 8.0 -> "重复帧较多 · 尝试 Windows 30/45 FPS" to WARNING
|
||||
else -> "识别偏低 · 放大 Windows 二维码或降到 30 FPS" to WARNING
|
||||
}
|
||||
ScannerMode.NONE -> "空闲" to MUTED
|
||||
}
|
||||
scannerStatsText?.text = "%s · 相机 %.1f FPS · 有效 QR %.1f/s".format(quality.first, cameraFps, decodedFps)
|
||||
scannerStatsText?.text = if (mode == ScannerMode.OPTICAL) {
|
||||
"%s · 相机 %.1f FPS · 解码 %.1f/s · 新帧 %.1f/s · %.1fms/次".format(
|
||||
quality.first, cameraFps, decodedFps, distinctFps, decodeMs
|
||||
)
|
||||
} else {
|
||||
"%s · 相机 %.1f FPS · 解码 %.1f/s · %.1fms/次".format(quality.first, cameraFps, decodedFps, decodeMs)
|
||||
}
|
||||
scannerStatsText?.setTextColor(quality.second)
|
||||
}
|
||||
},
|
||||
onLock = { lock ->
|
||||
runOnUiThread {
|
||||
qrLockOverlay?.update(lock)
|
||||
if (mode == ScannerMode.OPTICAL) {
|
||||
if (lock.locked && lock.misses == 0) maybeRefocusLockedQr()
|
||||
scannerLockText?.text = if (lock.locked) {
|
||||
val lockHint = if (lock.misses == 0) "稳定锁定" else "保持锁定 · miss ${lock.misses}"
|
||||
"$lockHint · 仅分析 ${(lock.searchAreaFraction * 100f).toInt().coerceIn(1, 100)}% 画面"
|
||||
} else {
|
||||
"区域锁定:搜索中 · 让单个大二维码占取景框 50% 以上"
|
||||
}
|
||||
scannerLockText?.setTextColor(
|
||||
if (!lock.locked) ACCENT else if (lock.misses == 0) SUCCESS else WARNING
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
currentAnalyzer = analyzer
|
||||
val targetSize = if (mode == ScannerMode.PAIR) Size(640, 480) else Size(1280, 720)
|
||||
val analysis = ImageAnalysis.Builder()
|
||||
|
||||
// v0.4 single large QR is intentionally analyzed at 640x480. With the
|
||||
// QR occupying most of the view this still leaves several camera
|
||||
// pixels/module, while substantially reducing Y-copy + ZXing work.
|
||||
val targetSize = Size(640, 480)
|
||||
val analysisBuilder = ImageAnalysis.Builder()
|
||||
.setTargetResolution(targetSize)
|
||||
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
||||
.build()
|
||||
.also { it.setAnalyzer(cameraExecutor, analyzer) }
|
||||
Camera2Interop.Extender(analysisBuilder).setCaptureRequestOption(
|
||||
CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
|
||||
requestedFps,
|
||||
)
|
||||
val analysis = analysisBuilder.build().also { it.setAnalyzer(cameraExecutor, analyzer) }
|
||||
|
||||
provider.unbindAll()
|
||||
val camera = provider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview, analysis)
|
||||
// Screen QR benefits from an immediate center AF/AE metering pass.
|
||||
try {
|
||||
val point = previewView.meteringPointFactory.createPoint(0.5f, 0.5f)
|
||||
val action = FocusMeteringAction.Builder(
|
||||
point,
|
||||
FocusMeteringAction.FLAG_AF or FocusMeteringAction.FLAG_AE,
|
||||
).setAutoCancelDuration(5, TimeUnit.SECONDS).build()
|
||||
camera.cameraControl.startFocusAndMetering(action)
|
||||
} catch (_: Exception) {}
|
||||
val camera = try {
|
||||
provider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview, analysis).also {
|
||||
scannerStatsText?.text = "相机请求 ${requestedFps.lower}~${requestedFps.upper} FPS · 等待首帧…"
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// Some old/OEM devices advertise a Camera2 FPS range that is not
|
||||
// accepted together with the selected CameraX stream combination.
|
||||
// Retry without the explicit AE range rather than failing scanner
|
||||
// startup; KEEP_ONLY_LATEST + ROI still provides the fast path.
|
||||
provider.unbindAll()
|
||||
val fallbackPreview = Preview.Builder().build().also {
|
||||
it.surfaceProvider = previewView.surfaceProvider
|
||||
}
|
||||
val fallbackAnalysis = ImageAnalysis.Builder()
|
||||
.setTargetResolution(targetSize)
|
||||
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
||||
.build().also { it.setAnalyzer(cameraExecutor, analyzer) }
|
||||
scannerStatsText?.text = "设备拒绝显式高帧率请求 · 已回退相机自动 FPS · 等待首帧…"
|
||||
provider.bindToLifecycle(
|
||||
this,
|
||||
CameraSelector.DEFAULT_BACK_CAMERA,
|
||||
fallbackPreview,
|
||||
fallbackAnalysis,
|
||||
)
|
||||
}
|
||||
activeCamera = camera
|
||||
|
||||
previewView.post {
|
||||
if (previewView.width <= 0 || previewView.height <= 0) return@post
|
||||
try {
|
||||
val point = previewView.meteringPointFactory.createPoint(
|
||||
previewView.width * 0.5f,
|
||||
previewView.height * 0.5f,
|
||||
)
|
||||
val action = FocusMeteringAction.Builder(
|
||||
point,
|
||||
FocusMeteringAction.FLAG_AF or FocusMeteringAction.FLAG_AE,
|
||||
).setAutoCancelDuration(5, TimeUnit.SECONDS).build()
|
||||
camera.cameraControl.startFocusAndMetering(action)
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
}, mainExecutor)
|
||||
}
|
||||
|
||||
private fun maybeRefocusLockedQr() {
|
||||
val camera = activeCamera ?: return
|
||||
val center = qrLockOverlay?.lockedCenterInView() ?: return
|
||||
if (previewView.width <= 0 || previewView.height <= 0) return
|
||||
|
||||
// Do not force an AF/AE cycle every few seconds while the phone is
|
||||
// stationary: focus pumping can temporarily destroy QR contrast.
|
||||
// Re-meter only on the first lock or after the locked region moved
|
||||
// materially in the preview.
|
||||
val threshold = minOf(previewView.width, previewView.height) * 0.10f
|
||||
val dx = center.x - lastLockFocusX
|
||||
val dy = center.y - lastLockFocusY
|
||||
val moved = lastLockFocusX < 0f || dx * dx + dy * dy >= threshold * threshold
|
||||
if (!moved) return
|
||||
|
||||
val now = android.os.SystemClock.elapsedRealtime()
|
||||
if (now - lastLockFocusAt < 1200L) return
|
||||
lastLockFocusAt = now
|
||||
lastLockFocusX = center.x
|
||||
lastLockFocusY = center.y
|
||||
try {
|
||||
val point = previewView.meteringPointFactory.createPoint(center.x, center.y)
|
||||
val action = FocusMeteringAction.Builder(
|
||||
point,
|
||||
FocusMeteringAction.FLAG_AF or FocusMeteringAction.FLAG_AE,
|
||||
).setAutoCancelDuration(2, TimeUnit.SECONDS).build()
|
||||
camera.cameraControl.startFocusAndMetering(action)
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
|
||||
private fun chooseCameraFpsRange(): Range<Int> {
|
||||
return try {
|
||||
val manager = getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
|
||||
val characteristics = manager.cameraIdList
|
||||
.asSequence()
|
||||
.map { manager.getCameraCharacteristics(it) }
|
||||
.firstOrNull { it.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK }
|
||||
val ranges = characteristics?.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
|
||||
?.toList().orEmpty()
|
||||
ranges
|
||||
.filter { it.upper <= 60 && it.lower <= 30 }
|
||||
.maxWithOrNull(compareBy<Range<Int>> { it.upper }.thenBy { it.lower })
|
||||
?: ranges.filter { it.upper <= 60 }
|
||||
.maxWithOrNull(compareBy<Range<Int>> { it.upper }.thenBy { it.lower })
|
||||
?: ranges.maxWithOrNull(compareBy<Range<Int>> { it.upper }.thenBy { it.lower })
|
||||
?: Range(30, 30)
|
||||
} catch (_: Exception) {
|
||||
Range(30, 30)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildCameraUi(mode: ScannerMode): FrameLayout {
|
||||
val root = FrameLayout(this).apply { setBackgroundColor(Color.BLACK) }
|
||||
previewView = PreviewView(this).apply {
|
||||
@@ -226,6 +366,8 @@ class MainActivity : ComponentActivity() {
|
||||
scaleType = PreviewView.ScaleType.FILL_CENTER
|
||||
}
|
||||
root.addView(previewView, FrameLayout.LayoutParams(-1, -1))
|
||||
qrLockOverlay = QrLockOverlay(this)
|
||||
root.addView(qrLockOverlay, FrameLayout.LayoutParams(-1, -1))
|
||||
|
||||
val top = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
@@ -245,20 +387,25 @@ class MainActivity : ComponentActivity() {
|
||||
background = rounded(0xE80C0E12.toInt(), 18)
|
||||
}
|
||||
deviceText = text(
|
||||
if (mode == ScannerMode.OPTICAL) "接收设备:$receiverDeviceLabel\n发送设备:等待 QFT1 MANIFEST…"
|
||||
if (mode == ScannerMode.OPTICAL) "接收设备:$receiverDeviceLabel\n发送设备:等待 QFT2 MANIFEST…"
|
||||
else "目标:等待 QFTPAIR2 配对二维码",
|
||||
14f, Color.WHITE
|
||||
)
|
||||
statusText = text(
|
||||
if (mode == ScannerMode.OPTICAL) "把电脑动态二维码完整放进取景框"
|
||||
if (mode == ScannerMode.OPTICAL) "让电脑的单个大二维码占取景框 50% 以上,识别后会自动锁定区域"
|
||||
else "扫描 Windows“手机传电脑”页面上的二维码",
|
||||
14f, MUTED
|
||||
)
|
||||
progress = ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal).apply { max = 1000; progress = 0 }
|
||||
panel.addView(deviceText)
|
||||
panel.addView(statusText, matchWrap(top = 7))
|
||||
scannerLockText = text(
|
||||
if (mode == ScannerMode.OPTICAL) "区域锁定:搜索中" else "配对识别:快速模式",
|
||||
12f, ACCENT
|
||||
)
|
||||
panel.addView(scannerLockText, matchWrap(top = 6))
|
||||
scannerStatsText = text("相机分析:等待首帧…", 12f, ACCENT)
|
||||
panel.addView(scannerStatsText, matchWrap(top = 6))
|
||||
panel.addView(scannerStatsText, matchWrap(top = 4))
|
||||
panel.addView(progress, LinearLayout.LayoutParams(-1, dp(8)).apply { topMargin = dp(12) })
|
||||
root.addView(panel, FrameLayout.LayoutParams(-1, -2, Gravity.BOTTOM).apply {
|
||||
leftMargin = dp(12); rightMargin = dp(12); bottomMargin = dp(12)
|
||||
@@ -514,9 +661,16 @@ class MainActivity : ComponentActivity() {
|
||||
private fun stopCamera() {
|
||||
try { cameraProvider?.unbindAll() } catch (_: Exception) {}
|
||||
cameraProvider = null
|
||||
activeCamera = null
|
||||
lastLockFocusAt = 0L
|
||||
lastLockFocusX = -1f
|
||||
lastLockFocusY = -1f
|
||||
currentAnalyzer?.close()
|
||||
currentAnalyzer = null
|
||||
qrLockOverlay?.clear()
|
||||
qrLockOverlay = null
|
||||
scannerStatsText = null
|
||||
scannerLockText = null
|
||||
scannerMode = ScannerMode.NONE
|
||||
}
|
||||
|
||||
|
||||
@@ -32,18 +32,30 @@ data class Manifest(
|
||||
)
|
||||
|
||||
object QftProtocol {
|
||||
const val PREFIX = "QFT1:"
|
||||
private const val VERSION = 1
|
||||
const val PREFIX = "QFT2:"
|
||||
private const val LEGACY_PREFIX = "QFT1:"
|
||||
|
||||
/** Active QFT2 path: Base45 keeps the QR in compact alphanumeric mode. */
|
||||
fun parseText(text: String): QftFrame? {
|
||||
if (!text.startsWith(PREFIX)) return null
|
||||
val binary = try {
|
||||
Base64Url.decode(text.substring(PREFIX.length)) ?: return null
|
||||
} catch (_: IllegalArgumentException) {
|
||||
return null
|
||||
return when {
|
||||
text.startsWith(PREFIX) -> {
|
||||
val binary = Base45.decode(text.substring(PREFIX.length)) ?: return null
|
||||
parseBinary(binary, expectedMagic = "QFT2", expectedVersion = 2)
|
||||
}
|
||||
text.startsWith(LEGACY_PREFIX) -> {
|
||||
val binary = try {
|
||||
Base64Url.decode(text.substring(LEGACY_PREFIX.length)) ?: return null
|
||||
} catch (_: IllegalArgumentException) {
|
||||
return null
|
||||
}
|
||||
parseBinary(binary, expectedMagic = "QFT1", expectedVersion = 1)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
if (binary.size < 28) return null
|
||||
}
|
||||
|
||||
private fun parseBinary(binary: ByteArray, expectedMagic: String, expectedVersion: Int): QftFrame? {
|
||||
if (binary.size < 28) return null
|
||||
val bodySize = binary.size - 4
|
||||
val expectedCrc = ByteBuffer.wrap(binary, bodySize, 4)
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
@@ -52,12 +64,10 @@ object QftProtocol {
|
||||
if (crc != expectedCrc) return null
|
||||
|
||||
val bb = ByteBuffer.wrap(binary, 0, bodySize).order(ByteOrder.BIG_ENDIAN)
|
||||
val magic = ByteArray(4).also(bb::get)
|
||||
if (!magic.contentEquals(byteArrayOf('Q'.code.toByte(), 'F'.code.toByte(), 'T'.code.toByte(), '1'.code.toByte()))) {
|
||||
return null
|
||||
}
|
||||
val magic = ByteArray(4).also(bb::get).toString(Charsets.US_ASCII)
|
||||
if (magic != expectedMagic) return null
|
||||
val version = bb.get().toInt() and 0xff
|
||||
if (version != VERSION) return null
|
||||
if (version != expectedVersion) return null
|
||||
val kind = try { FrameKind.fromWire(bb.get().toInt() and 0xff) } catch (_: Exception) { return null }
|
||||
val sessionId = bb.long
|
||||
val seq = bb.int.toLong() and 0xffff_ffffL
|
||||
@@ -90,7 +100,6 @@ object QftProtocol {
|
||||
.trim()
|
||||
.ifBlank { "Windows-PC" }
|
||||
} else {
|
||||
// Backward compatibility with v0.1/v0.1.2 manifests.
|
||||
"Windows-PC"
|
||||
}
|
||||
|
||||
|
||||
@@ -2,40 +2,76 @@ package com.qft.optical
|
||||
|
||||
import androidx.camera.core.ImageAnalysis
|
||||
import androidx.camera.core.ImageProxy
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.BinaryBitmap
|
||||
import com.google.zxing.DecodeHintType
|
||||
import com.google.zxing.MultiFormatReader
|
||||
import com.google.zxing.PlanarYUVLuminanceSource
|
||||
import com.google.zxing.Result
|
||||
import com.google.zxing.common.GlobalHistogramBinarizer
|
||||
import com.google.zxing.common.HybridBinarizer
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.Executors
|
||||
import com.google.zxing.qrcode.QRCodeReader
|
||||
|
||||
/** One decoded QR result. QFT2 optical frames are consumed from text (Base45). */
|
||||
data class DecodedQr(
|
||||
val text: String,
|
||||
val rawBytes: ByteArray?,
|
||||
)
|
||||
|
||||
/**
|
||||
* Single-channel high-frequency scanner.
|
||||
*
|
||||
* Acquisition: centered crop -> occasional full-frame search.
|
||||
* Locked: only copy/decode the QR ROI, with progressive expansion on misses.
|
||||
*
|
||||
* The old four-reader/2x2 path is intentionally gone. One large QR gives the
|
||||
* camera more pixels/module and lets the decoder spend CPU on one useful code.
|
||||
*/
|
||||
class QrAnalyzer(
|
||||
private val profile: Profile,
|
||||
private val onTexts: (List<String>) -> Unit,
|
||||
private val onStats: (cameraFramesPerSecond: Double, decodedPerSecond: Double) -> Unit = { _, _ -> },
|
||||
private val onCode: (DecodedQr) -> Unit,
|
||||
private val onStats: (
|
||||
cameraFramesPerSecond: Double,
|
||||
decodedPerSecond: Double,
|
||||
distinctPerSecond: Double,
|
||||
averageDecodeMs: Double,
|
||||
) -> Unit = { _, _, _, _ -> },
|
||||
private val onLock: (ScannerLockState) -> Unit = {},
|
||||
) : ImageAnalysis.Analyzer {
|
||||
enum class Profile { PAIR_FAST, OPTICAL_MULTI }
|
||||
enum class Profile { PAIR_FAST, OPTICAL_LOCKED }
|
||||
|
||||
private val readers = Array(5) { newReader() }
|
||||
private val decodePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors().coerceIn(2, 4))
|
||||
private val reader = QRCodeReader()
|
||||
private val grayBuffer = GrayFrameBuffer()
|
||||
private var frameCounter = 0L
|
||||
private var decodedCounter = 0L
|
||||
private var distinctCounter = 0L
|
||||
private var lastFingerprint: Int? = null
|
||||
private var decodeNanos = 0L
|
||||
private var decodeAttempts = 0L
|
||||
private var windowStarted = android.os.SystemClock.elapsedRealtime()
|
||||
|
||||
private var lockRegion: LockRect? = null
|
||||
private var lockMisses = 0
|
||||
private var lastReportedLock: LockRect? = null
|
||||
private var lastLockFlag = false
|
||||
|
||||
override fun analyze(image: ImageProxy) {
|
||||
try {
|
||||
val gray = GrayFrame.from(image)
|
||||
frameCounter++
|
||||
val results = when (profile) {
|
||||
Profile.PAIR_FAST -> decodePair(gray)
|
||||
Profile.OPTICAL_MULTI -> decodeOptical(gray)
|
||||
}.distinct()
|
||||
if (results.isNotEmpty()) {
|
||||
decodedCounter += results.size
|
||||
onTexts(results)
|
||||
val result = when (profile) {
|
||||
Profile.PAIR_FAST -> decodePair(image)
|
||||
Profile.OPTICAL_LOCKED -> decodeOptical(image)
|
||||
}
|
||||
if (result != null) {
|
||||
decodedCounter++
|
||||
val text = result.text.orEmpty()
|
||||
val fingerprint = text.hashCode()
|
||||
val isNew = lastFingerprint != fingerprint
|
||||
if (isNew) {
|
||||
distinctCounter++
|
||||
lastFingerprint = fingerprint
|
||||
// Dynamic optical frames often remain on screen for more than
|
||||
// one camera exposure. Do not feed duplicates into the file
|
||||
// assembler/UI; decoding stats still count the successful hit.
|
||||
onCode(DecodedQr(text, result.rawBytes))
|
||||
}
|
||||
}
|
||||
reportStatsIfNeeded()
|
||||
} finally {
|
||||
@@ -43,120 +79,180 @@ class QrAnalyzer(
|
||||
}
|
||||
}
|
||||
|
||||
private fun decodePair(gray: GrayFrame): List<String> {
|
||||
// QFTPAIR2 is intentionally compact. Try a cheap centered GlobalHistogram
|
||||
// pass first, then the full frame every time so off-axis scans do not wait
|
||||
// several frames. HybridBinarizer is reserved for every third miss.
|
||||
val marginX = (gray.width * 0.08).toInt()
|
||||
val marginY = (gray.height * 0.08).toInt()
|
||||
decodeRegion(
|
||||
gray, marginX, marginY, gray.width - marginX * 2, gray.height - marginY * 2,
|
||||
readers[0], hybridFallback = false,
|
||||
)?.let { return listOf(it) }
|
||||
decodeRegion(gray, 0, 0, gray.width, gray.height, readers[0], hybridFallback = false)
|
||||
?.let { return listOf(it) }
|
||||
if (frameCounter % 3L == 0L) {
|
||||
decodeRegion(gray, 0, 0, gray.width, gray.height, readers[0], hybridFallback = true)
|
||||
?.let { return listOf(it) }
|
||||
private fun decodePair(image: ImageProxy): Result? {
|
||||
// Pairing QR is static. Center-first keeps the common path cheap while
|
||||
// still allowing off-center framing every second frame.
|
||||
val center = LockRect.centeredSearch(image.width, image.height, 0.82f)
|
||||
val centered = grayBuffer.capture(image, center)
|
||||
decodeDense(centered, hybrid = false)?.let { return it }
|
||||
|
||||
if (frameCounter % 2L == 0L) {
|
||||
val full = grayBuffer.capture(image)
|
||||
decodeDense(full, hybrid = false)?.let { return it }
|
||||
if (frameCounter % 6L == 0L) decodeDense(full, hybrid = true)?.let { return it }
|
||||
}
|
||||
return emptyList()
|
||||
return null
|
||||
}
|
||||
|
||||
private fun decodeOptical(gray: GrayFrame): List<String> {
|
||||
// v0.3 Windows can present a 2x2 grid. Decode quadrants concurrently.
|
||||
// 8% overlap tolerates perspective/centering error around the middle.
|
||||
val overlapX = (gray.width * 0.08).toInt()
|
||||
val overlapY = (gray.height * 0.08).toInt()
|
||||
val halfW = gray.width / 2
|
||||
val halfH = gray.height / 2
|
||||
val regions = arrayOf(
|
||||
intArrayOf(0, 0, (halfW + overlapX).coerceAtMost(gray.width), (halfH + overlapY).coerceAtMost(gray.height)),
|
||||
intArrayOf((halfW - overlapX).coerceAtLeast(0), 0, gray.width - (halfW - overlapX).coerceAtLeast(0), (halfH + overlapY).coerceAtMost(gray.height)),
|
||||
intArrayOf(0, (halfH - overlapY).coerceAtLeast(0), (halfW + overlapX).coerceAtMost(gray.width), gray.height - (halfH - overlapY).coerceAtLeast(0)),
|
||||
intArrayOf((halfW - overlapX).coerceAtLeast(0), (halfH - overlapY).coerceAtLeast(0), gray.width - (halfW - overlapX).coerceAtLeast(0), gray.height - (halfH - overlapY).coerceAtLeast(0)),
|
||||
private fun decodeOptical(image: ImageProxy): Result? {
|
||||
val current = lockRegion
|
||||
if (current != null) {
|
||||
// Fast hot path: only copy/decode the locked ROI. Most phones spend
|
||||
// far less time here than repeatedly scanning the entire frame.
|
||||
val crop = grayBuffer.capture(image, current)
|
||||
decodeDense(crop, hybrid = false)?.let { result ->
|
||||
lockMisses = 0
|
||||
updateLockFromResult(result, crop)
|
||||
return result
|
||||
}
|
||||
|
||||
lockMisses++
|
||||
// HybridBinarizer is slower but more robust against moire/exposure.
|
||||
// Use it sparingly rather than doubling work on every camera frame.
|
||||
if (lockMisses % 4 == 0) {
|
||||
decodeDense(crop, hybrid = true)?.let { result ->
|
||||
lockMisses = 0
|
||||
updateLockFromResult(result, crop)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// Follow small hand movement without immediately returning to a
|
||||
// costly full-frame search. Only unlock after sustained misses.
|
||||
if (lockMisses == 3 || lockMisses == 6) {
|
||||
lockRegion = current.expand(
|
||||
image.width,
|
||||
image.height,
|
||||
if (lockMisses == 3) 1.20f else 1.45f,
|
||||
)
|
||||
publishLock(image, locked = true)
|
||||
}
|
||||
if (lockMisses >= 10) {
|
||||
lockRegion = null
|
||||
lockMisses = 0
|
||||
publishLock(image, locked = false)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Acquire: center-first, full frame every second camera frame. At the
|
||||
// default 640x480 analysis size this is substantially cheaper than the
|
||||
// previous 960x540 + four-region search.
|
||||
val search = LockRect.centeredSearch(image.width, image.height, 0.84f)
|
||||
val centered = grayBuffer.capture(image, search)
|
||||
decodeDense(centered, hybrid = false)?.let { result ->
|
||||
updateLockFromResult(result, centered)
|
||||
return result
|
||||
}
|
||||
|
||||
if (frameCounter % 2L == 0L) {
|
||||
val full = grayBuffer.capture(image)
|
||||
decodeDense(full, hybrid = false)?.let { result ->
|
||||
updateLockFromResult(result, full)
|
||||
return result
|
||||
}
|
||||
if (frameCounter % 6L == 0L) {
|
||||
decodeDense(full, hybrid = true)?.let { result ->
|
||||
updateLockFromResult(result, full)
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
publishLock(image, locked = false)
|
||||
return null
|
||||
}
|
||||
|
||||
private fun decodeDense(gray: GrayFrame, hybrid: Boolean): Result? {
|
||||
if (gray.width < 96 || gray.height < 96) return null
|
||||
val source = PlanarYUVLuminanceSource(
|
||||
gray.luma,
|
||||
gray.width,
|
||||
gray.height,
|
||||
0,
|
||||
0,
|
||||
gray.width,
|
||||
gray.height,
|
||||
false,
|
||||
)
|
||||
val tasks = regions.mapIndexed { index, r ->
|
||||
Callable {
|
||||
decodeRegion(gray, r[0], r[1], r[2], r[3], readers[index], hybridFallback = false)
|
||||
}
|
||||
}
|
||||
val found = try {
|
||||
decodePool.invokeAll(tasks).mapNotNull { future ->
|
||||
try { future.get() } catch (_: Exception) { null }
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
if (found.isNotEmpty()) return found
|
||||
|
||||
// Backward-compatible single-QR fallback. Limit the expensive hybrid
|
||||
// pass to every third frame so low-end Android 7 devices stay responsive.
|
||||
return if (frameCounter % 3L == 0L) {
|
||||
decodeRegion(gray, 0, 0, gray.width, gray.height, readers[4], hybridFallback = true)
|
||||
?.let(::listOf) ?: emptyList()
|
||||
} else emptyList()
|
||||
}
|
||||
|
||||
private fun decodeRegion(
|
||||
gray: GrayFrame,
|
||||
left: Int,
|
||||
top: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
reader: MultiFormatReader,
|
||||
hybridFallback: Boolean,
|
||||
): String? {
|
||||
if (width < 80 || height < 80) return null
|
||||
val source = try {
|
||||
PlanarYUVLuminanceSource(
|
||||
gray.luma, gray.width, gray.height,
|
||||
left.coerceAtLeast(0), top.coerceAtLeast(0),
|
||||
width.coerceAtMost(gray.width - left.coerceAtLeast(0)),
|
||||
height.coerceAtMost(gray.height - top.coerceAtLeast(0)),
|
||||
false,
|
||||
)
|
||||
} catch (_: Exception) { return null }
|
||||
|
||||
try {
|
||||
return reader.decodeWithState(BinaryBitmap(GlobalHistogramBinarizer(source)))?.text
|
||||
} catch (_: Exception) {
|
||||
if (!hybridFallback) return null
|
||||
} finally {
|
||||
reader.reset()
|
||||
}
|
||||
val started = System.nanoTime()
|
||||
decodeAttempts++
|
||||
return try {
|
||||
reader.decodeWithState(BinaryBitmap(HybridBinarizer(source)))?.text
|
||||
val bitmap = if (hybrid) {
|
||||
BinaryBitmap(HybridBinarizer(source))
|
||||
} else {
|
||||
BinaryBitmap(GlobalHistogramBinarizer(source))
|
||||
}
|
||||
reader.decode(bitmap)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
} finally {
|
||||
decodeNanos += System.nanoTime() - started
|
||||
reader.reset()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLockFromResult(result: Result, gray: GrayFrame) {
|
||||
if (profile != Profile.OPTICAL_LOCKED) return
|
||||
val points = result.resultPoints
|
||||
?.map { (it.x + gray.originX) to (it.y + gray.originY) }
|
||||
.orEmpty()
|
||||
val measured = LockRect.fromPoints(points, gray.fullWidth, gray.fullHeight)
|
||||
?: lockRegion
|
||||
?: LockRect.centeredSearch(gray.fullWidth, gray.fullHeight, 0.62f)
|
||||
// Smooth small finder-point jitter so the crop and on-screen overlay do
|
||||
// not visibly vibrate while a dynamic QR is changing every frame.
|
||||
val next = lockRegion?.smoothToward(measured, 0.30f) ?: measured
|
||||
val changed = next.materiallyDiffers(lockRegion)
|
||||
lockRegion = next
|
||||
lockMisses = 0
|
||||
if (changed || !lastLockFlag) {
|
||||
publishLock(
|
||||
frameWidth = gray.fullWidth,
|
||||
frameHeight = gray.fullHeight,
|
||||
rotationDegrees = gray.rotationDegrees,
|
||||
locked = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun publishLock(image: ImageProxy, locked: Boolean) {
|
||||
publishLock(image.width, image.height, image.imageInfo.rotationDegrees, locked)
|
||||
}
|
||||
|
||||
private fun publishLock(frameWidth: Int, frameHeight: Int, rotationDegrees: Int, locked: Boolean) {
|
||||
val region = lockRegion
|
||||
val changed = locked != lastLockFlag || region?.materiallyDiffers(lastReportedLock) == true ||
|
||||
(!locked && lastReportedLock != null)
|
||||
if (!changed && frameCounter % 15L != 0L) return
|
||||
lastLockFlag = locked
|
||||
lastReportedLock = region
|
||||
onLock(
|
||||
ScannerLockState(
|
||||
locked = locked,
|
||||
region = region,
|
||||
frameWidth = frameWidth,
|
||||
frameHeight = frameHeight,
|
||||
rotationDegrees = rotationDegrees,
|
||||
misses = lockMisses,
|
||||
searchAreaFraction = region?.areaFraction(frameWidth, frameHeight) ?: 1f,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun reportStatsIfNeeded() {
|
||||
val now = android.os.SystemClock.elapsedRealtime()
|
||||
val elapsed = now - windowStarted
|
||||
if (elapsed < 1000L) return
|
||||
val seconds = elapsed / 1000.0
|
||||
onStats(frameCounter / seconds, decodedCounter / seconds)
|
||||
val averageMs = if (decodeAttempts == 0L) 0.0 else decodeNanos / 1_000_000.0 / decodeAttempts
|
||||
onStats(frameCounter / seconds, decodedCounter / seconds, distinctCounter / seconds, averageMs)
|
||||
frameCounter = 0
|
||||
decodedCounter = 0
|
||||
distinctCounter = 0
|
||||
decodeNanos = 0
|
||||
decodeAttempts = 0
|
||||
windowStarted = now
|
||||
}
|
||||
|
||||
fun close() {
|
||||
decodePool.shutdownNow()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun newReader() = MultiFormatReader().apply {
|
||||
setHints(
|
||||
mapOf(
|
||||
DecodeHintType.POSSIBLE_FORMATS to listOf(BarcodeFormat.QR_CODE),
|
||||
DecodeHintType.TRY_HARDER to false,
|
||||
DecodeHintType.CHARACTER_SET to "UTF-8",
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
fun close() = Unit
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.qft.optical
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Path
|
||||
import android.graphics.PointF
|
||||
import android.graphics.RectF
|
||||
import android.view.View
|
||||
import kotlin.math.max
|
||||
|
||||
/** Visualizes the ROI that the decoder is actually using. */
|
||||
class QrLockOverlay(context: Context) : View(context) {
|
||||
private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.STROKE
|
||||
strokeWidth = resources.displayMetrics.density * 3f
|
||||
}
|
||||
private val shade = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.FILL
|
||||
color = 0x66000000
|
||||
}
|
||||
private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.FILL
|
||||
color = 0x1600E5A8
|
||||
}
|
||||
private val sweep = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.STROKE
|
||||
strokeWidth = resources.displayMetrics.density
|
||||
color = 0x9920E3A2.toInt()
|
||||
}
|
||||
private var state: ScannerLockState? = null
|
||||
|
||||
fun update(next: ScannerLockState) {
|
||||
state = next
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
state = null
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun lockedCenterInView(): PointF? {
|
||||
val s = state ?: return null
|
||||
val region = s.region ?: return null
|
||||
if (!s.locked || width <= 0 || height <= 0) return null
|
||||
val rect = mapRegionToView(region, s)
|
||||
return PointF(rect.centerX(), rect.centerY())
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
super.onDraw(canvas)
|
||||
val s = state
|
||||
if (s == null || width <= 0 || height <= 0) {
|
||||
drawSearchGuide(canvas)
|
||||
return
|
||||
}
|
||||
val region = s.region
|
||||
if (!s.locked || region == null) {
|
||||
drawSearchGuide(canvas)
|
||||
return
|
||||
}
|
||||
|
||||
val rect = mapRegionToView(region, s)
|
||||
val radius = resources.displayMetrics.density * 14f
|
||||
stroke.color = if (s.misses <= 2) 0xFF20E3A2.toInt() else 0xFFFFC857.toInt()
|
||||
val pulse = ((android.os.SystemClock.uptimeMillis() / 180L) % 2L).toFloat()
|
||||
stroke.strokeWidth = resources.displayMetrics.density * (if (pulse < 0.5f) 2.6f else 3.4f)
|
||||
canvas.drawRoundRect(rect, radius, radius, fill)
|
||||
|
||||
val outside = Path().apply {
|
||||
fillType = Path.FillType.EVEN_ODD
|
||||
addRect(0f, 0f, width.toFloat(), height.toFloat(), Path.Direction.CW)
|
||||
addRoundRect(rect, radius, radius, Path.Direction.CW)
|
||||
}
|
||||
canvas.drawPath(outside, shade)
|
||||
canvas.drawRoundRect(rect, radius, radius, stroke)
|
||||
drawCorners(canvas, rect, stroke)
|
||||
|
||||
// Lightweight dynamic sweep shows that the *locked ROI* is the active
|
||||
// decoder area. This only animates the overlay; it never touches camera
|
||||
// pixels or the ImageAnalysis thread.
|
||||
val phase = (android.os.SystemClock.uptimeMillis() % 1200L) / 1200f
|
||||
val scanY = rect.top + rect.height() * phase
|
||||
sweep.color = if (s.misses <= 2) 0x9920E3A2.toInt() else 0x99FFC857.toInt()
|
||||
canvas.drawLine(rect.left + rect.width() * 0.08f, scanY, rect.right - rect.width() * 0.08f, scanY, sweep)
|
||||
postInvalidateDelayed(100L)
|
||||
}
|
||||
|
||||
private fun drawSearchGuide(canvas: Canvas) {
|
||||
val side = minOf(width, height) * 0.72f
|
||||
val rect = RectF(
|
||||
(width - side) / 2f,
|
||||
(height - side) / 2f,
|
||||
(width + side) / 2f,
|
||||
(height + side) / 2f,
|
||||
)
|
||||
stroke.color = 0xCCFFFFFF.toInt()
|
||||
stroke.strokeWidth = resources.displayMetrics.density * 2f
|
||||
drawCorners(canvas, rect, stroke)
|
||||
}
|
||||
|
||||
private fun drawCorners(canvas: Canvas, rect: RectF, paint: Paint) {
|
||||
val len = minOf(rect.width(), rect.height()) * 0.12f
|
||||
val p = Path()
|
||||
p.moveTo(rect.left, rect.top + len); p.lineTo(rect.left, rect.top); p.lineTo(rect.left + len, rect.top)
|
||||
p.moveTo(rect.right - len, rect.top); p.lineTo(rect.right, rect.top); p.lineTo(rect.right, rect.top + len)
|
||||
p.moveTo(rect.left, rect.bottom - len); p.lineTo(rect.left, rect.bottom); p.lineTo(rect.left + len, rect.bottom)
|
||||
p.moveTo(rect.right - len, rect.bottom); p.lineTo(rect.right, rect.bottom); p.lineTo(rect.right, rect.bottom - len)
|
||||
canvas.drawPath(p, paint)
|
||||
}
|
||||
|
||||
private fun mapRegionToView(region: LockRect, s: ScannerLockState): RectF {
|
||||
val fw = s.frameWidth.toFloat().coerceAtLeast(1f)
|
||||
val fh = s.frameHeight.toFloat().coerceAtLeast(1f)
|
||||
val l = region.left / fw
|
||||
val t = region.top / fh
|
||||
val r = region.right / fw
|
||||
val b = region.bottom / fh
|
||||
|
||||
val corners = arrayOf(
|
||||
rotate(l, t, s.rotationDegrees), rotate(r, t, s.rotationDegrees),
|
||||
rotate(l, b, s.rotationDegrees), rotate(r, b, s.rotationDegrees),
|
||||
)
|
||||
val minX = corners.minOf { it.first }
|
||||
val maxX = corners.maxOf { it.first }
|
||||
val minY = corners.minOf { it.second }
|
||||
val maxY = corners.maxOf { it.second }
|
||||
|
||||
val rotatedW = if (s.rotationDegrees % 180 == 0) s.frameWidth.toFloat() else s.frameHeight.toFloat()
|
||||
val rotatedH = if (s.rotationDegrees % 180 == 0) s.frameHeight.toFloat() else s.frameWidth.toFloat()
|
||||
// PreviewView uses FILL_CENTER: scale to fill then crop equally.
|
||||
val scale = max(width / rotatedW.coerceAtLeast(1f), height / rotatedH.coerceAtLeast(1f))
|
||||
val renderedW = rotatedW * scale
|
||||
val renderedH = rotatedH * scale
|
||||
val offsetX = (width - renderedW) / 2f
|
||||
val offsetY = (height - renderedH) / 2f
|
||||
|
||||
return RectF(
|
||||
offsetX + minX * renderedW,
|
||||
offsetY + minY * renderedH,
|
||||
offsetX + maxX * renderedW,
|
||||
offsetY + maxY * renderedH,
|
||||
)
|
||||
}
|
||||
|
||||
private fun rotate(x: Float, y: Float, degrees: Int): Pair<Float, Float> = when ((degrees % 360 + 360) % 360) {
|
||||
90 -> (1f - y) to x
|
||||
180 -> (1f - x) to (1f - y)
|
||||
270 -> y to (1f - x)
|
||||
else -> x to y
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.qft.optical
|
||||
|
||||
data class ScannerLockState(
|
||||
val locked: Boolean,
|
||||
val region: LockRect?,
|
||||
val frameWidth: Int,
|
||||
val frameHeight: Int,
|
||||
val rotationDegrees: Int,
|
||||
val misses: Int,
|
||||
val searchAreaFraction: Float,
|
||||
)
|
||||
@@ -30,9 +30,11 @@ class SessionAssembler(
|
||||
|
||||
@Synchronized
|
||||
fun acceptText(text: String) {
|
||||
val frame = QftProtocol.parseText(text) ?: return
|
||||
if (completedSession == frame.sessionId) return
|
||||
acceptFrame(QftProtocol.parseText(text) ?: return)
|
||||
}
|
||||
|
||||
private fun acceptFrame(frame: QftFrame) {
|
||||
if (completedSession == frame.sessionId) return
|
||||
when (frame.kind) {
|
||||
FrameKind.MANIFEST -> acceptManifest(frame)
|
||||
FrameKind.DATA -> acceptData(frame)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.qft.optical
|
||||
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class LockRectTest {
|
||||
@Test fun finderPointsProduceBoundedSquareRoi() {
|
||||
val lock = LockRect.fromPoints(
|
||||
listOf(300f to 180f, 660f to 180f, 300f to 540f),
|
||||
960,
|
||||
720,
|
||||
) ?: error("lock missing")
|
||||
assertTrue(lock.width == lock.height)
|
||||
assertTrue(lock.left >= 0 && lock.top >= 0)
|
||||
assertTrue(lock.right <= 960 && lock.bottom <= 720)
|
||||
assertTrue(lock.areaFraction(960, 720) < 0.36f)
|
||||
}
|
||||
|
||||
@Test fun missesCanExpandRoiWithoutLeavingFrame() {
|
||||
val base = LockRect(250, 100, 420, 420)
|
||||
val expanded = base.expand(960, 540, 1.35f)
|
||||
assertTrue(expanded.width >= base.width)
|
||||
assertTrue(expanded.left >= 0 && expanded.top >= 0)
|
||||
assertTrue(expanded.right <= 960 && expanded.bottom <= 540)
|
||||
}
|
||||
|
||||
@Test fun smoothingReducesLockJitter() {
|
||||
val a = LockRect(100, 100, 300, 300)
|
||||
val b = LockRect(140, 130, 340, 340)
|
||||
val smoothed = a.smoothToward(b, 0.25f)
|
||||
assertTrue(smoothed.left in 101..139)
|
||||
assertTrue(smoothed.top in 101..129)
|
||||
assertTrue(smoothed.width in 301..339)
|
||||
}
|
||||
}
|
||||
@@ -4,35 +4,30 @@ import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.Base64
|
||||
import java.util.zip.CRC32
|
||||
|
||||
class ProtocolTest {
|
||||
@Test
|
||||
fun parsesDataFrameAndRejectsCorruption() {
|
||||
fun base45KnownVectors() {
|
||||
assertArrayEquals("AB".toByteArray(), Base45.decode("BB8"))
|
||||
assertArrayEquals("Hello!!".toByteArray(), Base45.decode("%69 VD92EX0"))
|
||||
assertArrayEquals("base-45".toByteArray(), Base45.decode("UJCLQE7W581"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesQft2DataFrameAndRejectsCorruption() {
|
||||
val payload = byteArrayOf(1, 2, 3, 4, 5)
|
||||
val text = encode(kind = 2, session = 7L, seq = 9, total = 11, payload = payload)
|
||||
val text = encodeQft2(kind = 2, session = 7L, seq = 9, total = 11, payload = payload)
|
||||
val f = QftProtocol.parseText(text)!!
|
||||
assertEquals(FrameKind.DATA, f.kind)
|
||||
assertEquals(7L, f.sessionId)
|
||||
assertEquals(9L, f.seq)
|
||||
assertArrayEquals(payload, f.payload)
|
||||
|
||||
val corrupted = text.dropLast(1) + if (text.last() == 'A') "B" else "A"
|
||||
val corrupted = text.dropLast(1) + if (text.last() == '0') "1" else "0"
|
||||
assertNull(QftProtocol.parseText(corrupted))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesCrossLanguageKnownVector() {
|
||||
val text = "QFT1:UUZUMQECAQIDBAUGBwgAAAADAAAACgAJaGVsbG8tcWZ0XhNTAg"
|
||||
val f = QftProtocol.parseText(text)!!
|
||||
assertEquals(FrameKind.DATA, f.kind)
|
||||
assertEquals(0x0102030405060708L, f.sessionId)
|
||||
assertEquals(3L, f.seq)
|
||||
assertEquals(10L, f.totalChunks)
|
||||
assertEquals("hello-qft", f.payload.toString(Charsets.UTF_8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesManifestSenderDevice() {
|
||||
val name = "test.txt".toByteArray()
|
||||
@@ -41,26 +36,25 @@ class ProtocolTest {
|
||||
val payload = ByteBuffer.allocate(8 + 2 + 32 + 2 + name.size + 2 + device.size)
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
.putLong(1234L)
|
||||
.putShort(700.toShort())
|
||||
.putShort(450.toShort())
|
||||
.put(sha)
|
||||
.putShort(name.size.toShort())
|
||||
.put(name)
|
||||
.putShort(device.size.toShort())
|
||||
.put(device)
|
||||
.array()
|
||||
val text = encode(kind = 1, session = 77L, seq = 0, total = 2, payload = payload)
|
||||
val frame = QftProtocol.parseText(text)!!
|
||||
val manifest = QftProtocol.parseManifest(frame)!!
|
||||
val text = encodeQft2(kind = 1, session = 77L, seq = 0, total = 3, payload = payload)
|
||||
val manifest = QftProtocol.parseManifest(QftProtocol.parseText(text)!!)!!
|
||||
assertEquals("DESKTOP-ZHY", manifest.senderDevice)
|
||||
assertEquals("test.txt", manifest.fileName)
|
||||
assertEquals(1234L, manifest.fileSize)
|
||||
assertEquals(700, manifest.chunkSize)
|
||||
assertEquals(450, manifest.chunkSize)
|
||||
}
|
||||
|
||||
private fun encode(kind: Int, session: Long, seq: Int, total: Int, payload: ByteArray): String {
|
||||
private fun encodeQft2(kind: Int, session: Long, seq: Int, total: Int, payload: ByteArray): String {
|
||||
val bb = ByteBuffer.allocate(24 + payload.size + 4).order(ByteOrder.BIG_ENDIAN)
|
||||
bb.put("QFT1".toByteArray())
|
||||
bb.put(1)
|
||||
bb.put("QFT2".toByteArray())
|
||||
bb.put(2)
|
||||
bb.put(kind.toByte())
|
||||
bb.putLong(session)
|
||||
bb.putInt(seq)
|
||||
@@ -70,6 +64,25 @@ class ProtocolTest {
|
||||
val bodyLen = bb.position()
|
||||
val crc = CRC32().apply { update(bb.array(), 0, bodyLen) }.value
|
||||
bb.putInt(crc.toInt())
|
||||
return "QFT1:" + Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array())
|
||||
return "QFT2:" + base45Encode(bb.array())
|
||||
}
|
||||
|
||||
private fun base45Encode(data: ByteArray): String {
|
||||
val alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
||||
val out = StringBuilder((data.size * 3 + 1) / 2)
|
||||
var i = 0
|
||||
while (i + 1 < data.size) {
|
||||
val value = (data[i].toInt() and 0xff) * 256 + (data[i + 1].toInt() and 0xff)
|
||||
out.append(alphabet[value % 45])
|
||||
out.append(alphabet[(value / 45) % 45])
|
||||
out.append(alphabet[value / 2025])
|
||||
i += 2
|
||||
}
|
||||
if (i < data.size) {
|
||||
val value = data[i].toInt() and 0xff
|
||||
out.append(alphabet[value % 45])
|
||||
out.append(alphabet[value / 45])
|
||||
}
|
||||
return out.toString()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user