Android通过OpenGL截取视频画面

Android通过OpenGL截取视频画面

截取视频图片

    private void getVideoScreenShot(int frameBuffer) {
        if (!isScreenShot) return;
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer);
        Bitmap result = null;
        try {
            IntBuffer pixBuffer = IntBuffer.allocate(mSurfaceView.getWidth() * mSurfaceView.getHeight());
            GLES20.glReadPixels(0, 0, mSurfaceView.getWidth(), mSurfaceView.getHeight(), GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixBuffer);
            int[] glPixel = pixBuffer.array();
            int[] argbPixel = new int[mSurfaceView.getWidth() * mSurfaceView.getHeight()];
            GLHelper.openGLToBitmapColor(glPixel, argbPixel, mSurfaceView.getWidth(), mSurfaceView.getHeight());
            result = Bitmap.createBitmap(argbPixel,
                    mSurfaceView.getWidth(),
                    mSurfaceView.getHeight(),
                    Bitmap.Config.ARGB_8888);
            FileUtils.saveBitmap(result, new File(Environment.getExternalStorageDirectory(), "scrren.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
            isScreenShot = false;
        }
    }

格式转换

因为bitmap的存储格式和从OpenGL中获取到到buffer不一致,所以需要转换一下。

public static native void openGLToBitmapColor(int[] src,int[] dst, int width,int height);
JNIEXPORT void JNICALL Java_com_dong_opencamera_utils_GLHelper_openGLToBitmapColor
        (JNIEnv *env, jclass jclass, jintArray srcArray, jintArray dstArray, jint width,
         jint height) {
    unsigned int *src = (unsigned int *) (*env)->GetIntArrayElements(env, srcArray, 0);
    unsigned int *dst = (unsigned int *) (*env)->GetIntArrayElements(env, dstArray, 0);
    openGLToBitmapColor(src, dst, width, height);
    (*env)->ReleaseIntArrayElements(env, srcArray, src, JNI_ABORT);
    (*env)->ReleaseIntArrayElements(env, dstArray, dst, JNI_ABORT);
    return;
}
#include "colorConvert.h"
#include "log.h"

void openGLToBitmapColor(const unsigned int *src, unsigned int *dst, int width, int height) {
    int x, y;
    unsigned char *srcucur;
    unsigned char *dstucur;
    unsigned char *dstu = dst;
    unsigned char *srcu = src;
    for (y = 0; y < height; y++) {
        srcucur = (srcu + y * width * 4);
        int step = (height - y - 1) * width * 4;
        dstucur = (dstu + step);
        dstucur += 3;
        for (x = 0; x < width; x++) {
            (*dstucur) = (unsigned char) (*(srcucur + 3));
            (*(dstucur + 1)) = (unsigned char) (*(srcucur + 2));
            (*(dstucur + 2)) = (unsigned char) (*(srcucur + 1));
            (*(dstucur + 3)) = (unsigned char) (*(srcucur));
            srcucur += 4;
            dstucur += 4;
        }
    }
}

到此,完成全部流程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值