Hi all!
I want to put some screen activity into gif, I achieved this, but it take a lot of time.
How I do it: I want to record gif 100x100 with 25 frames:
1) I take 25 screenshots: In render method I set scalingViewport 100x100, then I take screenshots:
gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
final Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
byte[] bytes = new byte[pixels.remaining()];
gl.glReadPixels(0, 0, width, height, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);
pixels.get(bytes);
int bytesSize = bytes.length;
int[] arrayWitoutAlpha = new int[bytesSize/4];
int arrayPos = 0;
for(int i = 0; i < bytesSize; i+=4) {
arrayWitoutAlpha[arrayPos] = ((bytes[i]&0x0ff) << 16) | ((bytes[i+1]&0x0ff) << 8) | (bytes[i+2]&0x0ff);
arrayPos++;
}
return arrayWitoutAlpha;
and add this int arrays into one two-dimensional int array. Taking 25 screenshots 100x100 takes about 500ms.
2) After that I combine screenshots with different gif encoders (like this and others). But sometimes frame can be added in encoder in 50ms, or sometimes it can be added in 1000ms, and gif with that size can be generated for 1500ms, or can be generated for 30 seconds or more.
Example of code how i generate gif with gif encoder from Square:
GifEncoder encoder = new GifEncoder(Gdx.files.external("testgif.gif").write(false),
gifWidth, gifHeight, 1);
ImageOptions options = new ImageOptions();
for (int i = 0; i < framesArray.length; i++) { //framesArray - two-dimentional int array with frames
encoder.addImage(framesArray[i], gifWidth, options);
}
encoder.finishEncoding();
I think even 1500ms for this little gif it is very long. I would be very grateful to that who knows how to deal with it. Thanks in advance!