From e32020e70b0efcd04e58ee38af63ac436aaf8485 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Sun, 15 Sep 2024 22:17:00 +0800 Subject: [PATCH] chore: reduce memory usage --- .../rtast/fancybot/commands/lookup/GithubUserCommand.kt | 1 + .../cn/rtast/fancybot/commands/lookup/WeatherCommand.kt | 9 ++++++--- .../cn/rtast/fancybot/commands/parse/BVParseCommand.kt | 9 ++++++--- .../rtast/fancybot/commands/parse/GitHubParseCommand.kt | 7 ++++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubUserCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubUserCommand.kt index c70296e..f3e4980 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubUserCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubUserCommand.kt @@ -61,6 +61,7 @@ class GithubUserCommand : BaseCommand() { } g2d.drawString("114514/1919810", 40, 560) g2d.drawCustomImage(userAvatar, 40, canvasHeight / 2 - 250, 170.0, 170.0, true) + g2d.dispose() return canvas.toByteArray().encodeToBase64() } diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt index c78d72a..3953d25 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt @@ -32,22 +32,25 @@ class WeatherCommand : BaseCommand() { private val canvasHeight = 600 private val font = Font("Serif", Font.PLAIN, 60).deriveFont(Font.BOLD or Font.ITALIC) private val locationFont = Font("Serif", Font.PLAIN, 50).deriveFont(Font.BOLD or Font.ITALIC) + private val backgroundColor = Color(209, 238, 238) + private val textColor = Color.BLACK + private val licenseColor = Color(155, 48, 255) private fun createWeatherCard(weather: Weather, geoResult: Geo.Location): String { 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/999.png")!! - g2d.color = Color(209, 238, 238) + g2d.color = backgroundColor g2d.fillRect(0, 0, canvasWidth, canvasHeight) g2d.drawCustomImage(weatherIcon.toBufferedImage(), canvasWidth / 2 - 230, canvasHeight / 2 - 80, 230.0, 230.0) - g2d.color = Color.BLACK + g2d.color = textColor g2d.font = font g2d.drawString("${weather.now.temp}℃", canvasWidth / 2, canvasHeight / 2 + 20) g2d.drawString(weather.now.text, canvasWidth / 2, canvasHeight / 2 + 100) g2d.font = locationFont g2d.drawCenteredText("${geoResult.adm1}-${geoResult.adm2}-${geoResult.name}", canvasWidth / 2, 90) - g2d.color = Color(155, 48, 255) + g2d.color = licenseColor g2d.drawCenteredText("数据来源: ${weather.refer.sources.joinToString(",")}", canvasWidth / 2, canvasHeight - 80) g2d.dispose() return emptyImage.toByteArray().encodeToBase64() diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt index 6ecc8f1..b132775 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt @@ -38,6 +38,9 @@ object BVParseCommand { private const val VIEW_COUNT_URL = "https://api.bilibili.com/x/player/online/total" private const val CANVAS_WIDTH = 1000 private const val CANVAS_HEIGHT = 600 + private val backgroundColor = Color(43, 43, 43) + private val upBarColor = Color(102, 102, 102) + private val textColor = Color.WHITE private val titleFont = Font("Serif", Font.ITALIC, 40) private val numberFont = Font("Serif", Font.ITALIC, 25) private val twoTwoLogo = ImageIO.read(Resources.loadFromResources("bili/22-coin.png")) @@ -97,12 +100,12 @@ object BVParseCommand { 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.color = backgroundColor g2d.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) // draw rect - g2d.color = Color(102, 102, 102) + g2d.color = upBarColor g2d.fillRect(0, 0, CANVAS_WIDTH, 120) - g2d.color = Color.WHITE + g2d.color = textColor // draw numbers g2d.font = numberFont g2d.drawString(like, 110, 200) diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt index 717575c..2da3e6f 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt @@ -40,6 +40,8 @@ object GitHubParseCommand { private val titleCustomFont = Font("Serif", Font.PLAIN, 75).deriveFont(Font.BOLD or Font.ITALIC) private val forkParentFont = Font("Serif", Font.PLAIN, 35).deriveFont(Font.ITALIC) private val descriptionCustomFont = Font("Serif", Font.PLAIN, 40).deriveFont(Font.BOLD or Font.ITALIC) + private val backgroundColor = Color.WHITE + private val textColor = Color(60, 60, 60) private val languageColors = mapOf( "Kotlin" to Color(169, 123, 255), @@ -80,7 +82,7 @@ object GitHubParseCommand { val g2d = canvas.createGraphics() g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) g2d.font = customFont - g2d.color = Color.WHITE + g2d.color = backgroundColor g2d.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) g2d.color = this.getLanguageColor(repoStat.language) g2d.fillRect(0, CANVAS_HEIGHT - 20, CANVAS_WIDTH, 35) @@ -88,7 +90,7 @@ object GitHubParseCommand { g2d.drawCustomImage(starIcon, 80, 785, 70.0, 70.0, false) // draw star icon g2d.drawCustomImage(issueIcon, 480, 785, 70.0, 70.0, false) // draw issue icon g2d.drawCustomImage(forkIcon, 880, 780, 70.0, 70.0, false) // draw fork icon - g2d.color = Color(60, 60, 60) + g2d.color = textColor g2d.drawString("Stars", 160, 840) g2d.drawString("Issues", 560, 840) g2d.drawString("Forks", 960, 840) @@ -101,7 +103,6 @@ object GitHubParseCommand { val truncatedParentFullName = setTruncat("Forked from: $parentName", g2d, 1000) g2d.drawString(truncatedParentFullName, 80, 435) } - g2d.color = Color(60, 60, 60) g2d.font = titleCustomFont val truncatedFullNameText = setTruncat(repoStat.fullName, g2d, 1000) g2d.drawString(truncatedFullNameText, 80, 280)