This commit is contained in:
Him188 2019-11-16 02:33:10 +08:00
parent 377e72332d
commit 0978d2846d

View File

@ -1,5 +1,6 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import io.ktor.util.cio.use
import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@ -10,6 +11,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.io.core.use import kotlinx.io.core.use
import kotlinx.io.core.writeFully
import java.awt.Image import java.awt.Image
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io.File import java.io.File
@ -30,13 +32,15 @@ actual var DefaultCaptchaSolver: CaptchaSolver = {
@Suppress("EXPERIMENTAL_API_USAGE") @Suppress("EXPERIMENTAL_API_USAGE")
MiraiLogger.info("需要验证码登录, 验证码为 4 字母") MiraiLogger.info("需要验证码登录, 验证码为 4 字母")
try { try {
tempFile.writeChannel().writeFully(it) tempFile.writeChannel().use { it.writeFully(it) }
MiraiLogger.info("若看不清字符图片, 请查看 ${tempFile.absolutePath}") MiraiLogger.info("若看不清字符图片, 请查看 ${tempFile.absolutePath}")
} catch (e: Exception) { } catch (e: Exception) {
MiraiLogger.info("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片") MiraiLogger.info("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片")
} }
MiraiLogger.info(ImageIO.read(tempFile.inputStream()).createCharImg()) tempFile.inputStream().use {
MiraiLogger.info(ImageIO.read(it).createCharImg())
}
} }
MiraiLogger.info("若要更换验证码, 请直接回车") MiraiLogger.info("若要更换验证码, 请直接回车")
readLine()?.takeUnless { it.isEmpty() || it.length != 4 } readLine()?.takeUnless { it.isEmpty() || it.length != 4 }
@ -60,7 +64,7 @@ private val captchaLock = Mutex()
* @author NaturalHG * @author NaturalHG
*/ */
@JvmOverloads @JvmOverloads
internal fun BufferedImage.createCharImg(outputWidth: Int = 100, ignoreRate: Double = 0.95): String { private fun BufferedImage.createCharImg(outputWidth: Int = 100, ignoreRate: Double = 0.95): String {
val newHeight = (this.height * (outputWidth.toDouble() / this.width)).toInt() val newHeight = (this.height * (outputWidth.toDouble() / this.width)).toInt()
val tmp = this.getScaledInstance(outputWidth, newHeight, Image.SCALE_SMOOTH) val tmp = this.getScaledInstance(outputWidth, newHeight, Image.SCALE_SMOOTH)
val image = BufferedImage(outputWidth, newHeight, BufferedImage.TYPE_INT_ARGB) val image = BufferedImage(outputWidth, newHeight, BufferedImage.TYPE_INT_ARGB)