chore: improve code and add image type option
This commit is contained in:
8 files changed
+27
-15
No files matched your search
@@ -8,6 +8,7 @@
|
||||
package cn.rtast.fancybot
|
||||
|
||||
import cn.rtast.fancybot.entity.Config
|
||||
import cn.rtast.fancybot.entity.enums.ImageType
|
||||
import cn.rtast.fancybot.entity.enums.WSType
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
@@ -27,7 +28,8 @@ val DEFAULT_CONFIG = Config(
|
||||
port = 6760,
|
||||
listeningGroups = listOf(114514, 1919810),
|
||||
qweatherKey = "114514",
|
||||
githubKey = "1919810"
|
||||
githubKey = "1919810",
|
||||
imageType = ImageType.PNG
|
||||
)
|
||||
|
||||
val ADMINS = listOf(
|
||||
|
||||
@@ -37,7 +37,7 @@ class WeatherCommand : BaseCommand() {
|
||||
val emptyImage = BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB)
|
||||
val g2d = emptyImage.createGraphics()
|
||||
val weatherIcon = Resources.loadFromResourcesAsBytes("qweather/${weather.now.icon}.png")
|
||||
?: Resources.loadFromResourcesAsBytes("qweather/100.png")!!
|
||||
?: Resources.loadFromResourcesAsBytes("qweather/999.png")!!
|
||||
g2d.color = Color(209, 238, 238)
|
||||
g2d.fillRect(0, 0, canvasWidth, canvasHeight)
|
||||
g2d.drawCustomImage(weatherIcon.toBufferedImage(), canvasWidth / 2 - 230, canvasHeight / 2 - 80, 230.0, 230.0)
|
||||
|
||||
@@ -15,6 +15,7 @@ import cn.rtast.fancybot.util.drawCustomImage
|
||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||
import cn.rtast.fancybot.util.str.formatNumber
|
||||
import cn.rtast.fancybot.util.str.setTruncat
|
||||
import cn.rtast.fancybot.util.toByteArray
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.ob.MessageChain
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
@@ -24,7 +25,6 @@ import java.awt.Color
|
||||
import java.awt.Font
|
||||
import java.awt.RenderingHints
|
||||
import java.awt.image.BufferedImage
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.net.URI
|
||||
import javax.imageio.ImageIO
|
||||
|
||||
@@ -69,8 +69,8 @@ object BVParseCommand {
|
||||
): String {
|
||||
val coverImage = ImageIO.read(URI(picUrl).toURL())
|
||||
val faceImage = ImageIO.read(URI(authorFace).toURL())
|
||||
val backgroundImage = BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_RGB)
|
||||
val g2d = backgroundImage.createGraphics()
|
||||
val canvas = BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_RGB)
|
||||
val g2d = canvas.createGraphics()
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
|
||||
g2d.color = Color(43, 43, 43)
|
||||
g2d.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)
|
||||
@@ -109,10 +109,7 @@ object BVParseCommand {
|
||||
// draw cover image
|
||||
g2d.drawCustomImage(coverImage, 430, 140, 600.0, 300.0, true)
|
||||
g2d.dispose()
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
ImageIO.write(backgroundImage, "png", byteArrayOutputStream)
|
||||
val imageBytes = byteArrayOutputStream.toByteArray()
|
||||
return imageBytes.encodeToBase64()
|
||||
return canvas.toByteArray().encodeToBase64()
|
||||
}
|
||||
|
||||
suspend fun parse(listener: OBMessage, bvid: String, message: GroupMessage) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import cn.rtast.fancybot.util.Resources
|
||||
import cn.rtast.fancybot.util.drawCustomImage
|
||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||
import cn.rtast.fancybot.util.str.setTruncat
|
||||
import cn.rtast.fancybot.util.toByteArray
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.ob.MessageChain
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
@@ -21,7 +22,6 @@ import java.awt.Color
|
||||
import java.awt.Font
|
||||
import java.awt.RenderingHints
|
||||
import java.awt.image.BufferedImage
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.net.URI
|
||||
import javax.imageio.ImageIO
|
||||
|
||||
@@ -117,10 +117,7 @@ object GitHubParseCommand {
|
||||
g2d.drawString(truncatedDescription, 80, 450)
|
||||
val avatarImage = ImageIO.read(URI(repoStat.owner.avatarUrl).toURL())
|
||||
g2d.drawCustomImage(avatarImage, 1200, 160, 300.0, 300.0, true)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
ImageIO.write(canvas, "png", byteArrayOutputStream)
|
||||
val imageBytes = byteArrayOutputStream.toByteArray()
|
||||
return imageBytes.encodeToBase64()
|
||||
return canvas.toByteArray().encodeToBase64()
|
||||
}
|
||||
|
||||
suspend fun parse(listener: OBMessage, message: GroupMessage) {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
import cn.rtast.fancybot.entity.enums.ImageType
|
||||
import cn.rtast.fancybot.entity.enums.WSType
|
||||
|
||||
data class Config(
|
||||
@@ -18,4 +19,5 @@ data class Config(
|
||||
val listeningGroups: List<Long>,
|
||||
val qweatherKey: String,
|
||||
val githubKey: String,
|
||||
val imageType: ImageType,
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/9/14
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity.enums
|
||||
|
||||
enum class ImageType(val typeName: String) {
|
||||
PNG("png"), JPG("jpg")
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import cn.rtast.fancybot.configManager
|
||||
import java.awt.Graphics2D
|
||||
import java.awt.geom.RoundRectangle2D
|
||||
import java.awt.image.BufferedImage
|
||||
@@ -58,7 +59,7 @@ fun ByteArray.toBufferedImage(): BufferedImage {
|
||||
|
||||
fun BufferedImage.toByteArray(): ByteArray {
|
||||
ByteArrayOutputStream().use { outputStream ->
|
||||
ImageIO.write(this, "png", outputStream)
|
||||
ImageIO.write(this, configManager.imageType.typeName, outputStream)
|
||||
return outputStream.toByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,5 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
|
||||
val listeningGroups get() = this.read<Config>().listeningGroups
|
||||
val qweatherKey get() = this.read<Config>().qweatherKey
|
||||
val githubKey get() = this.read<Config>().githubKey
|
||||
val imageType get() = this.read<Config>().imageType
|
||||
}
|
||||
Reference in New Issue
Block a user