12

所以我正在尝试做一个 VideoView 的屏幕截图。我想最简单的方法是:

videoView.setDrawingCacheEnabled(true);

然后当我需要截图时:

Bitmap screenshot = videoView.getDrawingCache();

但由于某种原因,我得到的位图每次都是黑色的。有人在这方面取得过成功吗?我也试过:

Bitmap bitmap = Bitmap.createBitmap(videoView.getWidth(), videoView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
videoView.draw(canvas);

但再一次,这给我返回了一个黑色图像。我可以看到 Android javadocs 中几乎没有记录 VideoView。有什么帮助吗?

4

2 回答 2

13

来自View#setDrawingCacheEnabled的文档:

启用绘图缓存类似于关闭硬件加速时设置图层。打开硬件加速时,启用绘图缓存对渲染没有影响,因为系统使用不同的加速机制来忽略该标志。如果您想为视图使用位图,即使启用了硬件加速,请参阅 setLayerType(int, android.graphics.Paint) 以获取有关如何启用软件和硬件层的信息。

VideoView 可能使用硬件加速运行并绕过缓存机制。一次好的源潜水可能会有所启发。

编辑-这篇文章似乎澄清了一些事情:

抱歉,从本质上讲,SurfaceView 不会在普通视图层次结构更新系统中绘制,因此不会在其中绘制。

(VideoView 是 SurfaceView 的子级)

于 2011-03-11T22:10:08.673 回答
0

从 API 10 开始,您可以使用MediaMetadataRetriever在给定时间(以微秒为单位)检索帧。这是一个示例代码:

public Bitmap videoFrame(String uri, long msec) {       
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {                       
        retriever.setDataSource(uri);            
        return retriever.getFrameAtTime(msec);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}
于 2012-10-28T09:48:17.917 回答