feat: add gif speed up and speed down command

This commit is contained in:
2024-10-27 17:43:06 +08:00
parent c3dc3fa4e5
commit c0fa5c3a49
3 files changed
+56 -13

No files matched your search

@@ -12,7 +12,7 @@ import com.madgag.gif.fmsware.GifDecoder
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
fun GifDecoder.makeGif(frames: List<BufferedImage>, speed: Float? = null): ByteArray {
val estimatedSize = frames.size * 50 * 1024
val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize)
val encoder = AnimatedGifEncoder()
@@ -20,7 +20,13 @@ fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
encoder.setRepeat(0)
val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) }
frames.indices.forEach { i ->
encoder.setDelay(delays[i])
if (speed == null) {
encoder.setDelay(delays[i])
} else {
var delay = this.getDelay(i)
delay = (delay / speed).toInt()
encoder.setDelay(delay.coerceAtLeast(1))
}
encoder.addFrame(frames[i])
}
encoder.finish()