我有一个表示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倍。在这种情况下,它不会崩溃,但图像非常嘈杂,与原始图像完全不一样。
发布于 2014-06-04 14:14:42
我将我的项目切换到使用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")
}https://stackoverflow.com/questions/24032034
复制相似问题