fix direct pairing probe timeout and speed up QR scanning

This commit is contained in:
zhy200308
2026-08-12 11:26:59 +08:00
parent f9e25819ee
commit eccd93266e
6 changed files with 111 additions and 24 deletions
@@ -310,10 +310,10 @@ class MainActivity : ComponentActivity() {
)
currentAnalyzer = analyzer
// 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)
// Pairing QR codes carry network/file metadata and can be much denser
// than one optical QFT2 frame. Give PAIR_FAST more sensor pixels per
// module; the continuous optical path stays at 640x480 to preserve FPS.
val targetSize = if (mode == ScannerMode.PAIR) Size(960, 720) else Size(640, 480)
val analysisBuilder = ImageAnalysis.Builder()
.setTargetResolution(targetSize)
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
@@ -124,26 +124,29 @@ class QrAnalyzer(
return null
}
// Acquisition is center-first, but unlike the old path it establishes
// a QR ROI immediately after the first hit. Full-frame work is only a
// recovery path for deliberately off-center QR codes.
val center = LockRect.centeredSearch(image.width, image.height, 0.82f)
// Pairing needs one reliable first hit, not sustained decode FPS. Keep a
// generous center crop so a large desktop QR is not clipped at the quiet
// zone, and prefer Hybrid thresholding on every frame: it is markedly
// more tolerant of monitor glare, PWM bands and uneven exposure.
val center = LockRect.centeredSearch(image.width, image.height, 0.92f)
val centered = grayBuffer.capture(image, center)
decodeDense(centered, hybrid = false)?.let { result ->
decodeDense(centered, hybrid = true)?.let { result ->
updatePairRegion(result, centered)
return result
}
if (frameCounter % 3L == 0L) {
decodeDense(centered, hybrid = true)?.let { result ->
// Global thresholding remains a cheap fallback for very clean displays,
// but no longer delays the robust path until the third camera frame.
if (frameCounter % 2L == 0L) {
decodeDense(centered, hybrid = false)?.let { result ->
updatePairRegion(result, centered)
return result
}
}
if (frameCounter % 4L == 0L) {
if (frameCounter % 3L == 0L) {
val full = grayBuffer.capture(image)
decodeDense(full, hybrid = false)?.let { result ->
decodeDense(full, hybrid = true)?.let { result ->
updatePairRegion(result, full)
return result
}
@@ -30,6 +30,33 @@ data class SelectedFile(
val uploadKey: String = UUID.randomUUID().toString(),
)
/**
* Wait for completed probes, rather than counting empty queue polls as failed
* addresses. This matters immediately after joining QFT Direct: the first ARP
* and Windows firewall transition can take longer than a short poll slice even
* though the authenticated HTTP request is already reaching the desktop.
*/
internal fun <T> awaitFirstNonNull(
completion: ExecutorCompletionService<T?>,
submitted: Int,
timeoutMs: Long,
): T? {
val deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMs.coerceAtLeast(0L))
var completed = 0
while (completed < submitted) {
val remainingNanos = deadline - System.nanoTime()
if (remainingNanos <= 0L) return null
val future = completion.poll(remainingNanos, TimeUnit.NANOSECONDS) ?: return null
completed += 1
try {
future.get()?.let { return it }
} catch (_: Exception) {
// One broken address must not discard successful probes still queued.
}
}
return null
}
object UploadClient {
private const val TAG = "QftUpload"
private const val DISCOVERY_PORT = 39392
@@ -203,15 +230,11 @@ object UploadClient {
return try {
val overallMs = (connectMs + readMs + 150).toLong() *
((candidates.size + workers - 1) / workers).coerceAtLeast(1)
val deadline = android.os.SystemClock.elapsedRealtime() + overallMs.coerceAtMost(2_400L)
repeat(candidates.size) {
val remaining = deadline - android.os.SystemClock.elapsedRealtime()
if (remaining <= 0) return null
val future = completion.poll(remaining.coerceAtMost(350L), TimeUnit.MILLISECONDS) ?: return@repeat
val result = future.get()
if (result != null) return result
}
null
awaitFirstNonNull(
completion = completion,
submitted = candidates.size,
timeoutMs = overallMs.coerceAtMost(2_400L),
)
} catch (_: Exception) {
null
} finally {