抓取视频的一帧

准确的抓取视频的一帧
public void captureFrame() {
try {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(getApplicationContext(), mUri);
Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime((mSecVideoView.getCurrentPosition()) * 1000, MediaMetadataRetriever.OPTION_CLOSEST );

mImageViewCapture.setImageBitmap(bitmap);
mImageViewCapture.setVisibility(View.VISIBLE);
mSecMediaController.hide();
mImageViewCapture.postDelayed(new Runnable() {
@Override
public void run() {
mImageViewCapture.setVisibility(View.GONE);
}
}, 3000);

// save image in local
File file = new File(Utils.VIDEO_CAPTURE_DIR);
if (!file.exists()) {
file.mkdir();
}

if (bitmap != null) {
FileOutputStream fileOutputStream = new FileOutputStream(Utils.VIDEO_CAPTURE_DIR + mSecVideoView.getCurrentPosition() + ".jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.close();
}else{
Log.e(TAG, "SecVideoPlayer captureFrame() bitmap is null");
Toast.makeText(this, getResources().getString(R.string.capture_fail), Toast.LENGTH_SHORT).show();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

你可能感兴趣的:(视频)