使用AndroidFrameConverter将javacv框架转换为位图

我正在尝试使用javacv AndroidFrameConverter将大小为1280×720的帧转换为位图,并且转换需要很长时间.这是转换的示例代码.

FrameGrabber grabber = new FFmpegFrameGrabber(videoUrl);
frame = grabber.grab();
AndroidFrameConverter converter = new AndroidFrameConverter();
Bitmap originalBitmap = converter.convert(frame);

还有其他比这更快的解决方案吗?

解决方法:

您可以使用此代码并根据需要更改过滤器.

public class FilterAsync extends AsyncTask {

    private FFmpegFrameGrabber VIDEO_GRABBER;
    private FFmpegFrameRecorder videoRecorder;
    private File file = new File(Environment.getExternalStorageDirectory() + "/Download/Abc.mp4");
    private Context mContext;
    private FFmpegFrameFilter filter;
    private boolean isTrue = false;
    private ArrayList<String> videoPaths;
    private File myDirectory;


    public FilterAsync(Context context) {
        mContext = context;
        VIDEO_GRABBER = new FFmpegFrameGrabber(file);
        myDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder/");
        if (!myDirectory.exists()) {
            myDirectory.mkdirs();
        }
        videoPaths = new ArrayList<>();
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Object doInBackground(Object[] params) {
        Frame tempVideoFrame;
        try {
            VIDEO_GRABBER.start();
            initVideoRecorder(myDirectory + "/video" + System.currentTimeMillis() + ".mp4");
            filter.start();
            while (VIDEO_GRABBER.grab() != null) {
                tempVideoFrame = VIDEO_GRABBER.grabImage();
                if (tempVideoFrame != null) {
                    filter.push(tempVideoFrame);
                    tempVideoFrame = filter.pull();
                    videoRecorder.record(tempVideoFrame);
                }
            }
            videoRecorder.stop();
            filter.stop();
            videoRecorder.release();
            VIDEO_GRABBER.stop();
            VIDEO_GRABBER.release();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        } catch (FrameRecorder.Exception e) {
            e.printStackTrace();
        } catch (FrameFilter.Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
    }


    private void initVideoRecorder(String path) {
        try {
            filter = new FFmpegFrameFilter("transpose=clock ,crop=w=640:h=480:x=0:y=0", VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());

            videoRecorder = FFmpegFrameRecorder.createDefault(path, VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
           videoRecorder.setAudioChannels(VIDEO_GRABBER.getAudioChannels());
            videoRecorder.start();
        } catch (FrameRecorder.Exception e) {
            e.printStackTrace();
        }
    }
}
上一篇:javaCV开发详解之12:视频文件转apng动态图片实现,视频生成apng,也支持摄像机、桌面屏幕、流媒体等视频源录制apng动态图


下一篇:在javacv中将IplImage转换为Mat