Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.someLibgdxTests;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import com.badlogic.gdx.graphics.Pixmap;
- import com.badlogic.gdx.graphics.Pixmap.Format;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Batch;
- import com.badlogic.gdx.graphics.glutils.FrameBuffer;
- import com.badlogic.gdx.scenes.scene2d.ui.Image;
- public class ImageToBitmapConverter {
- /** draws the given {@link Image} on a {@link FrameBuffer} and creates a {@link Bitmap} from the {@link FrameBuffer#getColorBufferTexture() texture}.
- * @param image the {@link Image} to draw
- * @param batch needed to draw the given {@link Image}
- * @return a {@link Bitmap} representation of the {@link Image} */
- public static Bitmap convert(Image image, Batch batch) {
- // draw on FBO
- FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, (int) image.getImageWidth(), (int) image.getImageHeight(), false);
- fbo.begin();
- batch.begin();
- image.draw(batch, 1);
- batch.end();
- fbo.end();
- Texture texture = fbo.getColorBufferTexture();
- fbo.dispose();
- // get a Pixmap from the texture
- texture.getTextureData().prepare(); // consumePixmap() says "A call to prepare() must precede a call to this method."
- Pixmap pixmap = texture.getTextureData().consumePixmap();
- texture.dispose();
- // create a Bitmap from the pixmap
- Bitmap bm = Bitmap.createBitmap((int) image.getImageWidth(), (int) image.getImageHeight(), Config.ARGB_8888); // maybe this will be an issue since libdgx's Format enum just provides RGBA_X formats and this is ARGB
- bm.copyPixelsFromBuffer(pixmap.getPixels());
- pixmap.dispose();
- return bm;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment