机器人拍摄的视频帧 [英] android capture video frame
问题描述
我需要一个视频文件的帧(也可能是SD卡,缓存目录或应用程序目录)。我有包android.media在我的应用程序,里面我有类类MediaMetadataRetriever。为了得到第一帧到一个位图,我用code:
I need to get a frame of a video file (it may be on sdcard, cache dir or app dir). I have package android.media in my application and inside I have class MediaMetadataRetriever. To get first frame into a bitmap, I use code:
public static Bitmap getVideoFrame(Context context, Uri videoUri) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
retriever.setDataSource(context, videoUri);
return retriever.captureFrame();
} catch (IllegalArgumentException ex) {
throw new RuntimeException();
} catch (RuntimeException ex) {
throw new RuntimeException();
} finally {
retriever.release();
}
}
但是,这是不工作。它抛出一个异常(java.lang.RuntimeException的:的setDataSource失败:状态= 0x80000000的)当我设置数据源。你知道如何使这个code的工作?或者你有没有使用的ffmpeg或其他外部库中的任何类似的(简单的)解决方案? videoUri是一个有效的URI(媒体播放器,可以从URI播放视频)
But this it's not working. It throws an exception (java.lang.RuntimeException: setDataSource failed: status = 0x80000000) when I set data source. Do you know how to make this code to work? Or Do you have any similar (simple) solution without using ffmpeg or other external libraries? videoUri is a valid uri (media player can play video from that URI)
推荐答案
对我来说,以下工作:
public static Bitmap getVideoFrame(FileDescriptor FD) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(FD);
return retriever.getFrameAtTime();
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (RuntimeException ex) {
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
}
}
return null;
}
此外,如果您使用的是文件描述符的路径,而不是工作。
Also works if you use a path instead of a filedescriptor.
这篇关于机器人拍摄的视频帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!