android - 媒体元数据检索器缩放图像
问题描述
有没有更好的解决方案来获取视频帧?
缩放图像部分有什么问题吗?
如果我不使用位图(删除代码)并运行相同的场景(打开视图 10 次),Profiler 显示的内存使用量比我使用从mediaMetadataRetriever
private fun getScaledBitmap(timeUs: Long): Bitmap? {
try {
mediaMetadataRetriever?.getFrameAtTime(timeUs, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)?.let {
val result = Bitmap.createBitmap(frameWidth, frameHeight, it.config)
val canvas = Canvas(result)
val scaleX = frameWidth.toFloat() / it.width.toFloat()
val scaleY = frameHeight.toFloat() / it.height.toFloat()
val scale = if (scaleX > scaleY) scaleX else scaleY
val w = (it.width * scale).toInt()
val h = (it.height * scale).toInt()
val srcRect = Rect(0, 0, it.width, it.height)
val destRect = Rect((frameWidth - w) / 2, (frameHeight - h) / 2, w, h)
canvas.drawBitmap(it, srcRect, destRect, null)
it.recycle()
return result
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
注意
frameWidth
和frameHeight
是所需的框架大小
解决方案
推荐阅读
- python - 在 Python 中尝试神经网络
- python - 如何删除行并重命名多索引表
- android - 小部件中的颤振迭代
- javascript - Discord 机器人无法运行
- javascript - 如何停止错误“未捕获的 TypeError”?
- javascript - 使用 node.js 将 HTML 表单转换为 PDF
- r - 如何使用 R 检查 JSON 文件中是否存在对象
- javascript - 使用 Jquery 在 prependTo 之后将列表恢复为先前的顺序
- circleci - 如何在 config.yaml 中获取拉取请求编号?(圈词)
- android - 备份和恢复用户数据的最佳方法?