[英]How to capture a frame from video in android?
Hi i have made a custom video played in android.with some simple "play","pause","play again" and "capture" button.NOw i have done all this functionalities except "capture".I have seacrched lot many links and googled a lot,But i can't find how to capture a frame from running video in android?嗨,我制作了一个在 android 中播放的自定义视频。有一些简单的“播放”、“暂停”、“再次播放”和“捕获”按钮。现在我已经完成了除“捕获”之外的所有这些功能。我已经搜索了很多链接并在谷歌上搜索了很多,但我找不到如何从 android 中运行的视频中捕获帧? my code is as below: please help me for is:
我的代码如下:请帮助我是:
Main.java主程序
package org.apache.android.media;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private ImageButton mcaptur;
private String current;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mcaptur = (ImageButton) findViewById(R.id.Captur);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
mcaptur.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
You can get a video frame with MediaMetadataRetriever
.您可以使用
MediaMetadataRetriever
获取视频帧。 The basic usage is as follows.基本用法如下。
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// Set data source to retriever.
// From your code, you might want to use your 'String path' here.
retriever.setDataSource(yourPath);
// Get a frame in Bitmap by specifying time.
// Be aware that the parameter must be in "microseconds", not milliseconds.
Bitmap bitmap = retriever.getFrameAtTime(timeInMicroSeconds);
// Do something with your bitmap.
You might want to use FFmpegMediaMetadataRetriever
for better performance.您可能希望使用
FFmpegMediaMetadataRetriever
以获得更好的性能。
I too have searched the same for some days and finally i came across opencv library through that we can able to read the frames from a live video and do the operations accordingly.我也搜索了几天,最后我遇到了 opencv 库,通过它我们可以从实时视频中读取帧并进行相应的操作。
I have did successfully and keeping the project in github for your reference.我已经成功并将项目保存在 github 中供您参考。
https://github.com/Karthi96/Video-Recorder-with-Frames-Analysis https://github.com/Karthi96/Video-Recorder-with-Frames-Analysis
Let me know if you still need a help.如果您还需要帮助,请告诉我。
you can use ffmpeg 你可以使用ffmpeg
Refer this link for more details 有关详细信息,请参阅此链接
How to extract images from a Video using FFmpeg 如何使用FFmpeg从视频中提取图像
you need to run this ffmpeg command in your activity to achieve this 您需要在活动中运行此ffmpeg命令才能实现此目的
you can also pad video,crop video using ffmpeg 你也可以使用ffmpeg填充视频,裁剪视频
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.