如何在Android视频中捕获帧?

如何在Android视频中捕获帧?,第1张

概述嗨,我制作了一个自定义视频,在android中播放.带有一些简单的“播放”,“暂停”,“再次播放”和“捕捉”按钮.现在,我已经完成了除“捕捉”之外的所有这些功能.我已经找到了很多链接和谷歌搜索很多,但是我找不到如何从运行Android的视频中捕获帧?我的代码如下:请帮助我的是:Main.java

嗨,我制作了一个自定义视频,在android中播放.带有一些简单的“播放”,“暂停”,“再次播放”和“捕捉”按钮.现在,我已经完成了除“捕捉”之外的所有这些功能.我已经找到了很多链接和谷歌搜索很多,但是我找不到如何从运行Android的视频中捕获帧?我的代码如下:请帮助我的是:

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;            }        }    }

解决方法:

您可以使用MediaMetadataRetrIEver获得视频帧.基本用法如下.

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.

您可能要使用FFmpegMediaMetadataRetriever以获得更好的性能.

总结

以上是内存溢出为你收集整理的如何在Android视频中捕获帧?全部内容,希望文章能够帮你解决如何在Android视频中捕获帧?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1075875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存