2024-09-21 16:03:28 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/9/21
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2024-10-09 13:06:43 +08:00
|
|
|
package cn.rtast.fancybot.util.misc
|
2024-09-21 16:03:28 +08:00
|
|
|
|
|
|
|
|
import com.madgag.gif.fmsware.AnimatedGifEncoder
|
|
|
|
|
import com.madgag.gif.fmsware.GifDecoder
|
|
|
|
|
import java.awt.image.BufferedImage
|
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
|
|
|
|
|
|
|
|
fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
|
2024-09-26 17:38:28 +08:00
|
|
|
val estimatedSize = frames.size * 50 * 1024
|
|
|
|
|
val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize)
|
2024-09-21 16:03:28 +08:00
|
|
|
val encoder = AnimatedGifEncoder()
|
|
|
|
|
encoder.start(byteArrayOutputStream)
|
|
|
|
|
encoder.setRepeat(0)
|
2024-09-26 17:38:28 +08:00
|
|
|
val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) }
|
|
|
|
|
frames.indices.forEach { i ->
|
|
|
|
|
encoder.setDelay(delays[i])
|
2024-09-21 16:03:28 +08:00
|
|
|
encoder.addFrame(frames[i])
|
|
|
|
|
}
|
|
|
|
|
encoder.finish()
|
|
|
|
|
return byteArrayOutputStream.toByteArray()
|
|
|
|
|
}
|