Skip to content

Commit c7ec33e

Browse files
committed
feat: qr detection with timeout (resolve #2)
1 parent b8295a1 commit c7ec33e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

server/src/main/kotlin/vip/cdms/maimaihelper/QrCapture.kt

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,24 @@ object QrCapture {
2121
delay(3000)
2222
frameCard.click()
2323
delay(1500)
24-
val screenshot = frameQr.capture()
24+
val screenshot = waitForQr { frameQr.capture() }
2525
frameClose.click()
2626
screenshot
2727
}
28+
29+
private fun ScreenRobot.waitForQr(
30+
timeout: Int = 15000,
31+
interval: Int = 300,
32+
capture: () -> BufferedImage
33+
): BufferedImage {
34+
val begin = System.currentTimeMillis()
35+
lateinit var screenshot: BufferedImage
36+
do {
37+
screenshot = capture()
38+
val refactored = QrRefactor.refactor(screenshot)
39+
if (refactored != null) return refactored
40+
delay(interval)
41+
} while (System.currentTimeMillis() - begin <= timeout)
42+
return screenshot
43+
}
2844
}

server/src/main/kotlin/vip/cdms/maimaihelper/QrRefactor.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.awt.image.BufferedImage
1010
object QrRefactor {
1111
private const val SIZE = 300
1212

13-
fun decode(qr: BufferedImage): String {
13+
private fun decode(qr: BufferedImage): String {
1414
val hints = mapOf(
1515
DecodeHintType.CHARACTER_SET to "UTF-8",
1616
DecodeHintType.TRY_HARDER to true,
@@ -23,7 +23,7 @@ object QrRefactor {
2323
return result.text
2424
}
2525

26-
fun encode(content: String): BufferedImage {
26+
private fun encode(content: String): BufferedImage {
2727
val hints = mapOf(
2828
EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.M,
2929
EncodeHintType.QR_VERSION to 4,
@@ -34,4 +34,10 @@ object QrRefactor {
3434
val matrix = writer.encode(content, BarcodeFormat.QR_CODE, SIZE, SIZE, hints)
3535
return MatrixToImageWriter.toBufferedImage(matrix)
3636
}
37+
38+
fun refactor(origin: BufferedImage) = try {
39+
encode(decode(origin))
40+
} catch (e: NotFoundException) {
41+
null
42+
}
3743
}

server/src/main/kotlin/vip/cdms/maimaihelper/Routing.kt

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package vip.cdms.maimaihelper
22

3-
import com.google.zxing.NotFoundException
43
import io.ktor.http.*
54
import io.ktor.server.application.*
65
import io.ktor.server.response.*
76
import io.ktor.server.routing.*
8-
import java.awt.image.BufferedImage
97
import java.io.ByteArrayOutputStream
108
import javax.imageio.ImageIO
119

@@ -19,17 +17,9 @@ fun Application.helperRouting() {
1917
if (config.token != null && call.queryParameters["token"] != config.token)
2018
return@get call.respondText("Invalid token", status = HttpStatusCode.Unauthorized)
2119

22-
lateinit var output: BufferedImage
23-
2420
desktopSwitcher.switchTo()
2521

26-
val screenshot = QrCapture.shot()
27-
try {
28-
val content = QrRefactor.decode(screenshot)
29-
output = QrRefactor.encode(content)
30-
} catch (e: NotFoundException) {
31-
output = screenshot
32-
}
22+
val output = QrCapture.shot()
3323

3424
desktopSwitcher.switchBack()
3525

0 commit comments

Comments
 (0)