如何在 Android 中捕获 VideoView 的屏幕截图或视频帧
Posted
技术标签:
【中文标题】如何在 Android 中捕获 VideoView 的屏幕截图或视频帧【英文标题】:How to capture screenshot or video frame of VideoView in Android 【发布时间】:2014-12-11 23:05:07 【问题描述】:我尝试了此博客 (http://android-er.blogspot.kr/2013/05/get-current-frame-in-videoview-using.html) 中的以下教程,该教程展示了如何使用 MediaMetadataRetriever 从视频源捕获视频帧。 但是,它仅在视频位于手机本地时才有效。
有没有办法在 VideoView 通过 IP 流式传输视频时捕获视频帧?
【问题讨论】:
【参考方案1】:Video View 是 SurfaceView 的子类,因此 Pixel Copy 也可以截取视频 View 的屏幕截图
把这个方法放在某个Util类中
/**
* Pixel copy to copy SurfaceView/VideoView into BitMap
*/
fun usePixelCopy(videoView: SurfaceView, callback: (Bitmap?) -> Unit)
val bitmap: Bitmap = Bitmap.createBitmap(
videoView.width,
videoView.height,
Bitmap.Config.ARGB_8888
);
try
// Create a handler thread to offload the processing of the image.
val handlerThread = HandlerThread("PixelCopier");
handlerThread.start();
PixelCopy.request(
videoView, bitmap,
PixelCopy.OnPixelCopyFinishedListener copyResult ->
if (copyResult == PixelCopy.SUCCESS)
callback(bitmap)
handlerThread.quitSafely();
,
Handler(handlerThread.looper)
)
catch (e: IllegalArgumentException)
callback(null)
// PixelCopy may throw IllegalArgumentException, make sure to handle it
e.printStackTrace()
用法:
usePixelCopy(videoView) bitmap: Bitmap? ->
processBitMp(bitmap)
【讨论】:
酷!非常感谢你。注意使用 PixelCopy 时的 @TargetApi(24)。【参考方案2】:我已经找到了解决这个问题的方法。在使用 SurfaceView 时,由于低级硬件 GPU 原因,VideoView 似乎不允许这样做。
解决方案是使用 TextureView 并使用 MediaPlayer 在其中播放视频。 Activity 将需要实现 TextureView.SurfaceTextureListener。使用此解决方案截屏时,视频会冻结一小会儿。此外,TextureView 不显示播放进度条的默认 UI(播放、暂停、FF/RW、播放时间等)。这是一个缺点。如果您有其他解决方案,请告诉我:)
解决办法如下:
public class TextureViewActivity extends Activity
implements TextureView.SurfaceTextureListener,
OnBufferingUpdateListener,
OnCompletionListener,
OnPreparedListener,
OnVideoSizeChangedListener
private MediaPlayer mp;
private TextureView tv;
public static String MY_VIDEO = "https://www.blahblahblah.com/myVideo.mp4";
public static String TAG = "TextureViewActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_texture_view);
tv = (TextureView) findViewById(R.id.textureView1);
tv.setSurfaceTextureListener(this);
public void getBitmap(TextureView vv)
String mPath = Environment.getExternalStorageDirectory().toString()
+ "/Pictures/" + Utilities.getDayTimeString() + ".png";
Toast.makeText(getApplicationContext(), "Capturing Screenshot: " + mPath, Toast.LENGTH_SHORT).show();
Bitmap bm = vv.getBitmap();
if(bm == null)
Log.e(TAG,"bitmap is null");
OutputStream fout = null;
File imageFile = new File(mPath);
try
fout = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.PNG, 90, fout);
fout.flush();
fout.close();
catch (FileNotFoundException e)
Log.e(TAG, "FileNotFoundException");
e.printStackTrace();
catch (IOException e)
Log.e(TAG, "IOException");
e.printStackTrace();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.media_player_video, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
Surface s = new Surface(surface);
try
mp = new MediaPlayer();
mp.setDataSource(MY_VIDEO);
mp.setSurface(s);
mp.prepare();
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setOnVideoSizeChangedListener(this);
mp.setAudiostreamType(AudioManager.STREAM_MUSIC);
mp.start();
Button b = (Button) findViewById(R.id.textureViewButton);
b.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
TextureViewActivity.this.getBitmap(tv);
);
catch (IllegalArgumentException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (SecurityException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
【讨论】:
这是否适用于来自 IP 摄像机的 .cgi 格式流图像?以上是关于如何在 Android 中捕获 VideoView 的屏幕截图或视频帧的主要内容,如果未能解决你的问题,请参考以下文章
无法在android的videoview中播放来自url的视频如何在videoview中播放来自URL的视频?
如何在 android 中对 VideoView 执行放大/缩小?
如何在android中播放视频时拍摄videoview的快照?
如何检测 VideoView 何时开始播放(Android)?