feat: add scan qrcode command

This commit is contained in:
2024-10-06 21:18:23 +08:00
parent 262cfa532c
commit 0a5c9b63c2
3 files changed
+57 -3

No files matched your search

+2 -2
View File
@@ -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()
)
@@ -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)
@@ -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<String>) {
if (args.isEmpty()) {
message.reply("发送`/qr <内容>`即可生成一张二维码的图片")
return
}
val content = args.joinToString(" ")
val image = this.generateQRCode(content)
val msg = MessageChain.Builder()
@@ -42,3 +54,43 @@ class QRCodeCommand : BaseCommand() {
insertActionRecord(CommandAction.GenQRCode, message.sender.userId)
}
}
@CommandDescription("解析二维码中的内容!")
class ScanQRCodeCommand : BaseCommand() {
override val commandNames = listOf("/scan")
companion object {
private val waitingList = mutableListOf<Long>()
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<String>) {
if (message.sender() !in waitingList) {
waitingList.add(message.sender())
message.reply("继续发送一张二维码图片")
} else {
message.reply("回复错误已经取消本次操作")
}
}
}