如果有人需要,这是我在 kotlin 中的解决方案
fun convertShutterSpeed(value: String?): String {
if (value.isNullOrBlank()) {
return ""
}
val split = value.split("/")
val ed: Float = split[0].toFloat()
val ed1: Float = split[1].toFloat()
val fl = ed / ed1
return if (ed < 0) {
Math.round(1 / Math.pow(2.toDouble(), fl.toDouble())).toString() + "s"
} else {
"1/" + Math.round(Math.pow(2.toDouble(), fl.toDouble()))
}
}
这里有一些测试
@Test
fun convertShutterSpeed() {
assertEquals("1/8", imageExif.convertShutterSpeed("3/1"))
assertEquals("1/10", imageExif.convertShutterSpeed("3321928/1000000"))
assertEquals("1/20", imageExif.convertShutterSpeed("4321928/1000000"))
assertEquals("1/125", imageExif.convertShutterSpeed("6965784f/1000000"))
assertEquals("1/250", imageExif.convertShutterSpeed("7965784/1000000"))
assertEquals("1/320", imageExif.convertShutterSpeed("8321928/1000000"))
assertEquals("1/400", imageExif.convertShutterSpeed("8643856/1000000"))
assertEquals("1/640", imageExif.convertShutterSpeed("9321928/1000000"))
assertEquals("1/800", imageExif.convertShutterSpeed("9643856/1000000"))
assertEquals("1/1000", imageExif.convertShutterSpeed("9965784/1000000"))
assertEquals("2s", imageExif.convertShutterSpeed("-1/1"))
assertEquals("6s", imageExif.convertShutterSpeed("-2584963/1000000"))
}
我得到的值来自佳能图像,但它也适用于其他相机型号。