-
21-12-2019 - |
题
我有一个代表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
.
我尝试了scride输入映像的其他值.Width,image.width舍入到4,image.width舍入到3的倍数。在这种情况下,它不会崩溃,但图像非常嘈杂,但是看起来很吵没有什么比原来的。
解决方案
我切换了我的项目,使用 webp-imageio 库,并能够实现它以下方式。
val image = Image(content) //content of PNG of type Array[Byte]
extension match {
case "webp" => {
val out = new ByteArrayOutputStream()
ImageIO.write(resizedImage.awt, "webp", out)
Ok(out.toByteArray).as("image/webp")
case _ => Ok(image.write).as("image/png")
}
. 不隶属于 StackOverflow