1.在onFrame方法中传入frame
public void onFrame(VideoFrame frame) { saveImgBitmap(frame); }
2.saveImgBitmap的实现
将frame转换为bitmap
public Bitmap saveImgBitmap(VideoFrame frame){ final Matrix drawMatrix = new Matrix(); // Used for bitmap capturing. final GlTextureFrameBuffer bitmapTextureFramebuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA); drawMatrix.reset(); drawMatrix.preTranslate(0.5f, 0.5f); //控制图片的方向 drawMatrix.preScale( -1f , -1f); drawMatrix.preScale(-1f, 1f); // We want the output to be upside down for Bitmap. drawMatrix.preTranslate(-0.5f, -0.5f); final int scaledWidth = (int) (1 * frame.getRotatedWidth()); final int scaledHeight = (int) (1 * frame.getRotatedHeight()); bitmapTextureFramebuffer.setSize(scaledWidth, scaledHeight); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, bitmapTextureFramebuffer.getFrameBufferId()); GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, bitmapTextureFramebuffer.getTextureId(), 0); GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); VideoFrameDrawer frameDrawer = new VideoFrameDrawer(); RendererCommon.GlDrawer drawer = new GlRectDrawer(); frameDrawer.drawFrame(frame, drawer, drawMatrix, 0 /* viewportX */, 0 /* viewportY */, scaledWidth, scaledHeight); final ByteBuffer bitmapBuffer = ByteBuffer.allocateDirect(scaledWidth * scaledHeight * 4); GLES20.glViewport(0, 0, scaledWidth, scaledHeight); GLES20.glReadPixels( 0, 0, scaledWidth, scaledHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bitmapBuffer); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); GlUtil.checkNoGLES2Error("EglRenderer.notifyCallbacks"); final Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(bitmapBuffer); try { File file = new File("/data/data/com.xxx.diagnose/files"+ "/test.jpg"); if (!file.exists()){ file.createNewFile(); } OutputStream outputStream=new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }