截取视频图片
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;
}
}
}
到此,完成全部流程。