我正在寻找一种在Scala中检测图像mimetype的方法,该图像以Array[Byte]形式表示。在Scala中是否有任何好的库可用于此?
def detectMimeType(bytes: Array[Byte]): Either[String, String] = {
val c1 = if (bytes.length >= 1) bytes.apply(0) & 0xff else 0x00
val c2 = ...
if (c1 == 'G' && c2 == 'I' && c3 == 'F' && c4 == '8')
Right("image/gif")
else if (c1 == 137 && c2 == 80 && c3 == 78 && c4 == 71 && c5 == 13 && c6 == 10 && c7 == 26 && c8 == 10)
Right("image/png")
else if (c4 == 0xEE && c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF)
Right("image/jpg")
else if (c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF)
Right("image/jpeg")
else
Left("unknown/unknown")
}