如何拍摄当前播放视频的屏幕截图?

时间:2011-11-15 17:36:24

标签: android video

我正在尝试截取当前正在播放的视频。我正在尝试使用能够成功获取网络视图截图的代码,但却没有成功拍摄当前播放视频的照片。

网络视图的代码如下。

WebView w = new WebView(this);

w.setWebViewClient(new WebViewClient()
{
    public void onPageFinished(WebView view, String url)
    {
         Picture picture = view.capturePicture();
         Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
         picture.getHeight(), Bitmap.Config.ARGB_8888);

         Canvas c = new Canvas( b );

         picture.draw( c );

         FileOutputStream fos = null;

         try {
             fos = new FileOutputStream( "/sdcard/yahoo_" +System.currentTimeMillis() + ".jpg" );

             if ( fos != null )
             {
                 b.compress(Bitmap.CompressFormat.JPEG, 90, fos );

                 fos.close();
             }

         } catch( Exception e )
         {
             //...
         }
    }
});

setContentView( w );

w.loadUrl( "http://www.yahoo.com");

4 个答案:

答案 0 :(得分:1)

更简单的解决方案(但不保证始终有效):MediaMetadataRetriever

来自另一个SO question的解决方案,您可以修改该解决方案以获得所需的输出。找到要捕获的时间(以毫秒为单位),并将此时间传递给MediaMetadataRetriever以获取帧。

不那么容易解决方案(如果支持编解码器,总是会工作):FFMPEG

答案 1 :(得分:1)

要扩展66CLSjY的答案,FFmpegMediaMetadataRetrieverMediaMetadataRetriever具有相同的界面,但它使用FFmpeg作为后端。如果默认配置不适用于您的视频格式,则可以通过重新编译来启用/禁用编解码器。以下是一些示例代码:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_CODEC);
Bitmap b = getFrameAtTime(3000);
mmr.release();

答案 2 :(得分:0)

尝试此操作,它会为您的应用屏幕提供位图

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();

答案 3 :(得分:0)

这对我有用:

首先将视图转换为位图的方法

public static Bitmap getBitmapFromView(View view) {

    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

然后保存到SD中,例如:

static private boolean saveImage(Bitmap bm, String absolutePath)
{
    FileOutputStream fos = null;
    try 
    {
    String absolutePath = "your path"
        File file = new File(absolutePath);
        fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, fos); //PNG ignora la calidad
    } catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            if (fos != null)
                fos.close();
        } 
        catch (Exception e)
        { 
            e.printStackTrace(); 
        }
    }
    return true;
}

祝你好运!

相关问题