实时从视频中提取帧 [英] Extract frames from a video in real-time android

查看:189
本文介绍了实时从视频中提取帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个应用程序来录制视频。我想要的是,当应用程序录制视频时,每一帧还保存在RGB值的数组列表中,以便从中提取特定信息。我知道这两个过程(视频记录和帧提取)是异步的,但这不是问题:提取过程可以在视频记录后完成。

I'm doing an app to record a video. What I would like is that when the app is recording the video, each frame is also saved in an arraylist of RGB values in order to extract particular information from it. I know that the two processes (video recording and extraction of frames) are asynchronous, but this is not a problem: the process of extraction can finish after the video recording.

有人可以告诉我如何从视频中提取帧吗?

Can someone tell me how I can extract the frames from the video?

非常感谢。

推荐答案

使用相机对象,您可以覆盖函数 setPreviewCallback 。从该函数,您将获得一个字节数据数组。您可以将其转换为位图,也可以将其保存在数组数组中。

Using the camera object you can override a function setPreviewCallback. from that function you would get an array of byte data. Which you can convert to bitmap or you can save them in an array of array.

假设您已扩展 SurfaceView 并实现了 SurfaceHolder.Callback

Assuming that you were extends SurfaceView and Implements SurfaceHolder.Callback

代码看起来像

mCamera.setPreviewCallback(new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {

        //do some operations using data

    }
});

如果要获取RGB值,可以使用此功能,

If you want to get the RGB value you can use this function,

static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,
            int height) {
        final int frameSize = width * height;

        for (int j = 0, yp = 0; j < height; j++) {
            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
            for (int i = 0; i < width; i++, yp++) {
                int y = (0xff & ((int) yuv420sp[yp])) - 16;
                if (y < 0)
                    y = 0;
                if ((i & 1) == 0) {
                    v = (0xff & yuv420sp[uvp++]) - 128;
                    u = (0xff & yuv420sp[uvp++]) - 128;
                }

                int y1192 = 1192 * y;
                int r = (y1192 + 1634 * v);
                int g = (y1192 - 833 * v - 400 * u);
                int b = (y1192 + 2066 * u);

                if (r < 0)
                    r = 0;
                else if (r > 262143)
                    r = 262143;
                if (g < 0)
                    g = 0;
                else if (g > 262143)
                    g = 262143;
                if (b < 0)
                    b = 0;
                else if (b > 262143)
                    b = 262143;

                rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
                        | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
            }
        }
    }

我想这就是你想要作为答案。如果我错了,请告诉我您的实际要求是什么

I think this is what you want as an answer. If i am wrong, please let me know whats is your actual requirement

要获得绿色价值,您可以使用以下代码,

For getting the green value, you can use the below code,

static public void calculateColor(int[] rgb, int[] color,
            int width, int height, int component) {
        for (int bin = 0; bin < 256; bin++) {
            color[bin] = 0;
        } // bin

        int start = 0;
        int end = width * height;

        if (component == 0) // red
        {
            for (int pix = start; pix < end; pix += 3) {
                int pixVal = (rgb[pix] >> 16) & 0xff;
                color[pixVal]++;
            } // pix
        } else if (component == 1) // green
        {
            for (int pix = start; pix < end; pix += 3) {
                int pixVal = (rgb[pix] >> 8) & 0xff;
                color[pixVal]++;
            } // pix
        } else // blue
        {
            for (int pix = start; pix < end; pix += 3) {
                int pixVal = rgb[pix] & 0xff;
                color[pixVal]++;
            } // pix
        }
    }

调用此函数将为每个像素返回绿色的值。如果要特定像素的绿色值,则必须修改此代码。

Calling this function would return the value of green for every pixel. If you wants the green value for a particular pixel, you have to modify this code.

这篇关于实时从视频中提取帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆