From 0a5c9b63c2e46b19caa26302f256a9d3dcc12b34 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Sun, 6 Oct 2024 21:18:23 +0800 Subject: [PATCH] feat: add scan qrcode command --- src/main/kotlin/cn/rtast/fancybot/Const.kt | 4 +- src/main/kotlin/cn/rtast/fancybot/FancyBot.kt | 2 + .../fancybot/commands/misc/QRCodeCommand.kt | 54 ++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/cn/rtast/fancybot/Const.kt b/src/main/kotlin/cn/rtast/fancybot/Const.kt index 7b5b964..69b95db 100644 --- a/src/main/kotlin/cn/rtast/fancybot/Const.kt +++ b/src/main/kotlin/cn/rtast/fancybot/Const.kt @@ -92,7 +92,7 @@ val tasks = mapOf( val commands = listOf( EchoCommand(), JrrpCommand(), NiuziRedeemCommand(), HitokotoCommand(), - KFCCommand(), QRCodeCommand(), + KFCCommand(), GenerateQRCodeCommand(), AntiRevokeCommand(), MCPingCommand(), WeatherCommand(), CigaretteCommand(), RemakeCommand(), PixivCommand(), @@ -118,5 +118,5 @@ val commands = listOf( ShotSelfCommand(), TenSetuCommand(), ReactionCommand(), MCSkinCommand(), GithubLatestCommitCommand(), RandomMusicCommand(), - ZDJDCommand() + ZDJDCommand(), ScanQRCodeCommand() ) \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt index 4a72c15..3d45ddb 100644 --- a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt +++ b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt @@ -7,6 +7,7 @@ package cn.rtast.fancybot +import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand import cn.rtast.fancybot.commands.parse.* import cn.rtast.fancybot.enums.WSType import cn.rtast.fancybot.util.initCommandAndItem @@ -57,6 +58,7 @@ class FancyBot : OneBotListener { ReverseGIFCommand.callback(message) AsciiArtCommand.callback(message) + ScanQRCodeCommand.callback(message) if (message.rawMessage.toList().any { it in arrayListOf('*', '-', '/', '+', '=') }) { val calculateResult = CalculateCommand.parse(message.rawMessage) diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/misc/QRCodeCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/misc/QRCodeCommand.kt index 9d12487..2926194 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/misc/QRCodeCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/misc/QRCodeCommand.kt @@ -11,18 +11,26 @@ import cn.rtast.fancybot.annotations.CommandDescription import cn.rtast.fancybot.enums.CommandAction import cn.rtast.fancybot.util.file.insertActionRecord import cn.rtast.fancybot.util.str.encodeToBase64 +import cn.rtast.fancybot.util.toBufferedImage import cn.rtast.fancybot.util.toByteArray import cn.rtast.rob.entity.GroupMessage +import cn.rtast.rob.enums.ArrayMessageType import cn.rtast.rob.util.BaseCommand import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.OneBotListener import com.google.zxing.BarcodeFormat +import com.google.zxing.BinaryBitmap import com.google.zxing.EncodeHintType +import com.google.zxing.MultiFormatReader +import com.google.zxing.client.j2se.BufferedImageLuminanceSource import com.google.zxing.client.j2se.MatrixToImageWriter +import com.google.zxing.common.HybridBinarizer import com.google.zxing.qrcode.QRCodeWriter +import java.awt.image.BufferedImage +import java.net.URI @CommandDescription("生成二维码") -class QRCodeCommand : BaseCommand() { +class GenerateQRCodeCommand : BaseCommand() { override val commandNames = listOf("/qr") private fun generateQRCode(content: String): String { @@ -33,6 +41,10 @@ class QRCodeCommand : BaseCommand() { } override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { + if (args.isEmpty()) { + message.reply("发送`/qr <内容>`即可生成一张二维码的图片") + return + } val content = args.joinToString(" ") val image = this.generateQRCode(content) val msg = MessageChain.Builder() @@ -41,4 +53,44 @@ class QRCodeCommand : BaseCommand() { message.reply(msg) insertActionRecord(CommandAction.GenQRCode, message.sender.userId) } +} + +@CommandDescription("解析二维码中的内容!") +class ScanQRCodeCommand : BaseCommand() { + override val commandNames = listOf("/scan") + + companion object { + + private val waitingList = mutableListOf() + + private fun scanQRCode(image: BufferedImage): String { + val luminanceSource = BufferedImageLuminanceSource(image) + val binaryBitmap = BinaryBitmap(HybridBinarizer(luminanceSource)) + val result = MultiFormatReader().decode(binaryBitmap) + return result.text + } + + suspend fun callback(message: GroupMessage) { + if (message.sender() in waitingList) { + waitingList.remove(message.sender()) + if (!message.message.any { it.type == ArrayMessageType.image }) { + message.reply("回复错误已取消本次操作") + return + } + val image = message.message.find { it.type == ArrayMessageType.image }?.data?.file!! + val bufferedImage = URI(image).toURL().readBytes().toBufferedImage() + val result = this.scanQRCode(bufferedImage) + message.reply("二维码扫描结果: $result") + } + } + } + + override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { + if (message.sender() !in waitingList) { + waitingList.add(message.sender()) + message.reply("继续发送一张二维码图片") + } else { + message.reply("回复错误已经取消本次操作") + } + } } \ No newline at end of file