简体   繁体   中英

Convert LibGDX pixmap to Android bitmap

I'm just trying to convert Pixmap to the Bitmap. Here is how I'm creating the pixmap

screenShot = ScreenUtils.getFrameBufferTexture().getTexture().getTextureData().consumePixmap();

And that's how I'm trying to convert Pixmap to Bitmap.

Bitmap mBitmap = BitmapFactory.decodeByteArray(screenshot.getPixels().array(), 0, screenshot.getPixels().array().length);

But that's crashes on the second line I gave you there. I would really appreciate any help. Thanks.

Simple and fast

PNG writer = new PNG((int)(pixmap.getWidth() * pixmap.getHeight() * 1.5 f));
writer.setFlipY(false);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
 writer.write(output, pixmap);
} finally {
 StreamUtils.closeQuietly(output);
 writer.dispose();
 pixmap.dispose();
}
byte[] bytes = output.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

Okey. I just didnt find the soulution exactly to my answer. But I've found another way to do what I needed. Thanks to Tenfour04.

I did the following steps:

  1. Created the pixmap.
  2. Saved that with PixmapIO.writePNG();
  3. And linked that to Android method.

That works for a custom static pixmap, but not working with created screenshot(but I think it's a problem with screenshot getting). (Fixed that, I was getting this error because was trying to take a screenshot outside of render function).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM