- Encode a series of android bitmap images to a static or an animated WebP image.
- Extract bitmap images from an animated WebP image.
// module level build.gradle.kts
dependencies {
implementation "com.aureusapps.android:webp-android:1.0.7"
}
git clone --branch release-1.12-hotfix --depth 1 git@github.com:UdaraWanasinghe/material-components-android.git
cd material-components-android
./gradlew publishToMavenLocal
// Create encoder instance
val webPEncoder = WebPEncoder(context, width, height)
// Configure the encoder
webPEncoder.configure(
config = WebPConfig(
lossless = WebPConfig.COMPRESSION_LOSSLESS,
quality = 75f
),
preset = WebPPreset.WEBP_PRESET_PICTURE
)
// Add progress listener if desired
webPEncoder.addProgressListener {
// Handle progress updates
true // Return true to continue encoding, false to cancel
}
// Encode frame
webPEncoder.encode(srcBitmap, dstUri)
// Release resources
webPEncoder.release()
val webPAnimEncoder = WebPAnimEncoder(
context = context,
width = width,
height = height,
options = WebPAnimEncoderOptions(
minimizeSize = true,
animParams = WebPMuxAnimParams(
backgroundColor = Color.GREEN,
loopCount = 3
)
)
)
webPAnimEncoder.configure(
config = WebPConfig(
lossless = WebPConfig.COMPRESSION_LOSSLESS,
quality = 75f
),
preset = WebPPreset.WEBP_PRESET_PICTURE
)
// Add progress listener if desired
webPAnimEncoder.addProgressListener {
// Handle progress updates
true // Return true to continue encoding, false to cancel
}
// Add frames to the animation
webPAnimEncoder.addFrame(timestamp, srcBitmap)
webPAnimEncoder.addFrame(timestamp, srcUri)
// Assemble the animation
webPAnimEncoder.assemble(timestamp, dstUri)
// Release resources
webPAnimEncoder.release()
// Create decoder instance
val webPDecoder = WebPDecoder(context)
// Set data source
webPDecoder.setDataSource(context, srcUri)
// Add decode listener to receive decoding events
webPDecoder.addDecodeListener(
object : WebPDecodeListener {
override fun onInfoDecoded(info: WebPInfo) {
// Handle image information
}
override fun onFrameDecoded(index: Int, timestamp: Long, bitmap: Bitmap, uri: Uri?) {
// Handle decoded frames
// Do not recycle the bitmap image as it is reused internally
}
}
)
// Configure decoder
webPDecoder.configure(
config = DecoderConfig(
name_prefix = "IMAGE_",
repeat_character = '0',
repeat_character_count = 6,
compressFormat = Bitmap.CompressFormat.PNG,
compress_quality = 100
)
)
// Check if frames are available to decode
val hasFrames = webPDecoder.hasNextFrame()
// Get next frame index
val nextIndex = webPDecoder.nextFrameIndex()
// Decode a single frame
val decodeResult = webPDecoder.decodeNextFrame()
// Decode frames from a WebP file
webPDecoder.decodeFrames(dstUri)
// Decode only the image information from a WebP file
val info = webPDecoder.decodeInfo(srcUri)
// Release resources
webPDecoder.release()
If you find this library useful, please consider buying me a coffee.