首页 > 解决方案 > 媒体元数据检索器缩放图像

问题描述

有没有更好的解决方案来获取视频帧?

缩放图像部分有什么问题吗?

如果我不使用位图(删除代码)并运行相同的场景(打开视图 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
}

注意frameWidthframeHeight是所需的框架大小

标签: androidimagememorybitmapkotlin

解决方案


推荐阅读