我有一个代表 PNG 的 byteArray,但我想将其转换为 webP。我有以下代码。我在代码中使用 com.sksamuel.scrimage.Image 并加载系统库 webp_jni。
val image = Image(content) //content of PNG of type Array[Byte]
extension match {
case "webp" => Ok(
libwebp.WebPEncodeRGB(
image.write,
image.width,
image.height,
image.width * 3,
80F)
).as("image/webp")
case _ => Ok(image.write).as("image/png")
}
如果我请求 png 图像,它一切正常,但是当请求 webP 图像时,我得到以下内容。
# SIGSEGV (0xb) at pc=0x00007fc008287377, pid=14883, tid=140461610465024
#
# JRE version: Java(TM) SE Runtime Environment (8.0_05-b13) (build 1.8.0_05-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.5-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libwebp.so.5+0x33377] WebPPictureAlloc+0x407
我尝试了步幅输入 image.width 的其他值,image.width 舍入为 4 的倍数,image.width 舍入为 3 的倍数。在这种情况下,它不会崩溃,但图像非常嘈杂,看起来一点也不像原来的。