feat: add scan qrcode command
This commit is contained in:
3 files changed
+57
-3
No files matched your search
@@ -92,7 +92,7 @@ val tasks = mapOf(
|
|||||||
val commands = listOf(
|
val commands = listOf(
|
||||||
EchoCommand(), JrrpCommand(),
|
EchoCommand(), JrrpCommand(),
|
||||||
NiuziRedeemCommand(), HitokotoCommand(),
|
NiuziRedeemCommand(), HitokotoCommand(),
|
||||||
KFCCommand(), QRCodeCommand(),
|
KFCCommand(), GenerateQRCodeCommand(),
|
||||||
AntiRevokeCommand(), MCPingCommand(),
|
AntiRevokeCommand(), MCPingCommand(),
|
||||||
WeatherCommand(), CigaretteCommand(),
|
WeatherCommand(), CigaretteCommand(),
|
||||||
RemakeCommand(), PixivCommand(),
|
RemakeCommand(), PixivCommand(),
|
||||||
@@ -118,5 +118,5 @@ val commands = listOf(
|
|||||||
ShotSelfCommand(), TenSetuCommand(),
|
ShotSelfCommand(), TenSetuCommand(),
|
||||||
ReactionCommand(), MCSkinCommand(),
|
ReactionCommand(), MCSkinCommand(),
|
||||||
GithubLatestCommitCommand(), RandomMusicCommand(),
|
GithubLatestCommitCommand(), RandomMusicCommand(),
|
||||||
ZDJDCommand()
|
ZDJDCommand(), ScanQRCodeCommand()
|
||||||
)
|
)
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot
|
package cn.rtast.fancybot
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
|
||||||
import cn.rtast.fancybot.commands.parse.*
|
import cn.rtast.fancybot.commands.parse.*
|
||||||
import cn.rtast.fancybot.enums.WSType
|
import cn.rtast.fancybot.enums.WSType
|
||||||
import cn.rtast.fancybot.util.initCommandAndItem
|
import cn.rtast.fancybot.util.initCommandAndItem
|
||||||
@@ -57,6 +58,7 @@ class FancyBot : OneBotListener {
|
|||||||
|
|
||||||
ReverseGIFCommand.callback(message)
|
ReverseGIFCommand.callback(message)
|
||||||
AsciiArtCommand.callback(message)
|
AsciiArtCommand.callback(message)
|
||||||
|
ScanQRCodeCommand.callback(message)
|
||||||
|
|
||||||
if (message.rawMessage.toList().any { it in arrayListOf('*', '-', '/', '+', '=') }) {
|
if (message.rawMessage.toList().any { it in arrayListOf('*', '-', '/', '+', '=') }) {
|
||||||
val calculateResult = CalculateCommand.parse(message.rawMessage)
|
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.enums.CommandAction
|
||||||
import cn.rtast.fancybot.util.file.insertActionRecord
|
import cn.rtast.fancybot.util.file.insertActionRecord
|
||||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||||
|
import cn.rtast.fancybot.util.toBufferedImage
|
||||||
import cn.rtast.fancybot.util.toByteArray
|
import cn.rtast.fancybot.util.toByteArray
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
|
import cn.rtast.rob.enums.ArrayMessageType
|
||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.MessageChain
|
import cn.rtast.rob.util.ob.MessageChain
|
||||||
import cn.rtast.rob.util.ob.OneBotListener
|
import cn.rtast.rob.util.ob.OneBotListener
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
|
import com.google.zxing.BinaryBitmap
|
||||||
import com.google.zxing.EncodeHintType
|
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.client.j2se.MatrixToImageWriter
|
||||||
|
import com.google.zxing.common.HybridBinarizer
|
||||||
import com.google.zxing.qrcode.QRCodeWriter
|
import com.google.zxing.qrcode.QRCodeWriter
|
||||||
|
import java.awt.image.BufferedImage
|
||||||
|
import java.net.URI
|
||||||
|
|
||||||
@CommandDescription("生成二维码")
|
@CommandDescription("生成二维码")
|
||||||
class QRCodeCommand : BaseCommand() {
|
class GenerateQRCodeCommand : BaseCommand() {
|
||||||
override val commandNames = listOf("/qr")
|
override val commandNames = listOf("/qr")
|
||||||
|
|
||||||
private fun generateQRCode(content: String): String {
|
private fun generateQRCode(content: String): String {
|
||||||
@@ -33,6 +41,10 @@ class QRCodeCommand : BaseCommand() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
|
if (args.isEmpty()) {
|
||||||
|
message.reply("发送`/qr <内容>`即可生成一张二维码的图片")
|
||||||
|
return
|
||||||
|
}
|
||||||
val content = args.joinToString(" ")
|
val content = args.joinToString(" ")
|
||||||
val image = this.generateQRCode(content)
|
val image = this.generateQRCode(content)
|
||||||
val msg = MessageChain.Builder()
|
val msg = MessageChain.Builder()
|
||||||
@@ -42,3 +54,43 @@ class QRCodeCommand : BaseCommand() {
|
|||||||
insertActionRecord(CommandAction.GenQRCode, message.sender.userId)
|
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("回复错误已经取消本次操作")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user