You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin项目使用im4java/ImageMagick报错:无对应图片格式解码委托

Kotlin中使用im4java调用ImageMagick报错:no decode delegate for this image format

运行以下Kotlin代码时,始终抛出异常:

org.im4java.core.CommandException: convert-im6.q16: 
no decode delegate for this image format `' @ error/constitute.c/ReadImage/581

问题代码

object ImageOptimizer {

    fun optimizeAndResize(inputBytes: ByteArray, resizeTo: Int = 128): ByteArray =               
ByteArrayInputStream(inputBytes).use { inputStream ->
    
        // 读取输入图片
        val inputImage: BufferedImage = ImageIO.read(inputStream)

        // 计算目标尺寸
        val rawWidth = inputImage.width
        val rawHeight = inputImage.height
        val (targetWidth, targetHeight) = if (rawWidth > rawHeight) {
            resizeTo to (rawHeight / (rawWidth.toDouble() / resizeTo)).toInt()
        } else {
            (rawWidth / (rawHeight.toDouble() / resizeTo)).toInt() to resizeTo
        }
        println("原始图片尺寸: $rawWidth X $rawHeight")
        println("目标图片尺寸: $targetWidth X $targetHeight")

        // 创建ImageMagick命令
        val cmd = ConvertCmd()
        // 配置操作参数
        val operation = IMOperation().apply {
            addImage("-") // 从标准输入读取
            resize(targetWidth, targetHeight)
            strip()
            addImage("jpeg:-") // 输出到标准输出(JPEG格式)
        }

        ByteArrayOutputStream().use { outputStream -> 
            // 设置管道
            val pipeIn = Pipe(inputStream, null)
            val pipeOut = Pipe(null, outputStream)
            cmd.setInputProvider(pipeIn)
            cmd.setOutputConsumer(pipeOut)

            // 执行命令
            try {
                cmd.run(operation)
            } catch (e: Exception) {
                e.printStackTrace()
            }

            // 返回处理后的图片字节数组
            outputStream.toByteArray()
        }
    }
}

fun main() {
    val inputBytes = Files.readAllBytes(Paths.get("test.jpg"))
    println("输入文件大小: ${inputBytes.size}")
    val result = ImageOptimizer.optimizeAndResize(inputBytes)
 
    val file = File("out.jpg")
    Files.write(file.toPath(), result)
    println("输出文件: ${file.path}, 大小: ${file.length()}")
}

环境与测试情况

  • 运行环境:Ubuntu 24.04 LTS,通过apt安装官方仓库的ImageMagick
  • 对比测试:Java版本的相同逻辑代码可正常运行;使用临时文件替代数据流的Kotlin示例也能正常工作
  • ImageMagick的JPEG格式支持:执行convert -list format得到的相关输出如下:
JP2* JP2       rw-   JPEG-2000 File Format Syntax (2.5.0)
      JPC* JP2       rw-   JPEG-2000 Code Stream Syntax (2.5.0)
      JPE* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.5)
     JPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.5)
      JPG* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.5)
      JPM* JP2       rw-   JPEG-2000 Code Stream Syntax (2.5.0)
      JPS* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.5)
      JPT* JP2       rw-   JPEG-2000 File Format Syntax (2.5.0)

内容的提问来源于stack exchange,提问作者Hantsy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:22:03