1) Get your helper methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public static void saveBitmap(@NotNull Bitmap screenshot, @NotNull String filePath, @NotNull String fileName) { OutputStream outStream = null; filePath = Environment.getExternalStorageDirectory().toString() + "/" + filePath; File dir = new File( filePath ); dir.mkdirs(); File output = new File( filePath, fileName ); try { outStream = new FileOutputStream( output ); screenshot.compress( Bitmap.CompressFormat.PNG, 100, outStream ); outStream.flush(); outStream.close(); Logger.v( TAG, "Saving Screenshot [" + filePath + fileName + "]" ); } catch ( FileNotFoundException e ) { Logger.e( TAG, "" + e.getMessage() ); } catch ( IOException e ) { Logger.e( TAG, "" + e.getMessage() ); } } @NotNull public Bitmap captureScreenShot() { int width = Gdx.graphics.getWidth(); // use your favorite width int height = Gdx.graphics.getHeight(); // use your favorite height int screenshotSize = width * height; ByteBuffer bb = ByteBuffer.allocateDirect( screenshotSize * 4 ); bb.order( ByteOrder.nativeOrder() ); // any opengl context will do Gdx.graphics.getGL20().glReadPixels( 0, 0, width, height, Gdx.graphics.getGL20().GL_RGBA, Gdx.graphics.getGL20().GL_UNSIGNED_BYTE, bb ); int pixelsBuffer[] = new int[screenshotSize]; bb.asIntBuffer().get( pixelsBuffer ); bb = null; Bitmap bitmap = Bitmap.createBitmap( width, height, Bitmap.Config.RGB_565 ); bitmap.setPixels( pixelsBuffer, screenshotSize - width, -width, 0, 0, width, height ); pixelsBuffer = null; short sBuffer[] = new short[screenshotSize]; ShortBuffer sb = ShortBuffer.wrap( sBuffer ); bitmap.copyPixelsToBuffer( sb ); //Making created bitmap (from OpenGL points) compatible with Android bitmap for ( int i = 0; i < screenshotSize; ++i ) { short v = sBuffer[i]; sBuffer[i] = ( short ) ( ( ( v & 0x1f ) << 11 ) | ( v & 0x7e0 ) | ( ( v & 0xf800 ) >> 11 ) ); } sb.rewind(); bitmap.copyPixelsFromBuffer( sb ); return bitmap; } @Override public void captureScreenShot(@NotNull final String path, @NotNull final String fileName) { Bitmap screenshot = captureScreenShot(); saveBitmap( screenshot, path, fileName ); screenshot.recycle(); } |
2) get a queue ready
1 | protected ConcurrentLinkedQueue screenshotRequestedQueue = new ConcurrentLinkedQueue<>(); |
3) add new filepath the the queue via e.g. events:
1 2 3 4 5 | @Override public boolean tap(float x, float y, int count, int button) { screenshotRequestedQueue.add( Math.random() * Integer.MAX_VALUE + ".png" ); return false; } |
4) IMPORTANT! save the screen at the end of the opengl render process. (Note: If try to capture somewhere in the middle you might end up with a black screen or any other funny effects.)
1 2 3 4 5 6 7 8 9 10 11 | @Override public void render(float delta) { ... // render your stuff ... // capture screen on request if ( !screenshotRequestedQueue.isEmpty() ) captureScreenShot(SCREENSHOT_FOLDER, screenshotRequestedQueue.poll() ); } |
January 9, 2019 at 12:12 pm
what is gdx here
January 9, 2019 at 12:14 pm
iam getting black screen.when iam taking screenshot of video in my project.iam using exoplayer.will you suggest me the solution for solve it