Is there any way to convert a BufferedImage to a Texture?
If you are using LibGDX, I have to say that I don’t recommend also using BufferedImages (from Java2D), instead you could use a Pixmap, or if you really do need BufferedImages, then I guess you could use ImageIO to save the image to a file, bring the file back in as a texture, then delete the file, but that seems quite hacky and inefficient.
Try to not mix libgdx with java2d. +1 for pixmap.
If you are using libgdx, you should NEVER EVER touch anything from java2d.
Have fun.
PS:
You should be able to get a pixel array from the bufferedimage and create a libgdx texture from that. Look at how to take a bufferedimage and make it into a opengl texture. Hell, you could just use that search box with the cob webs on it.
we wrote our own screenshot method which uses bufferedimages sort of:
public static void takeScreenshot(String outputfile)
{
ByteBuffer screenContents = ByteBuffer.allocateDirect(Display.getWidth() * Display.getHeight() * 4).order(ByteOrder.LITTLE_ENDIAN);
GL11.glReadPixels(0, 0, Display.getWidth(), Display.getHeight(), GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, screenContents);
BufferedImage img = new BufferedImage(Display.getWidth(), Display.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageUtil.putToBufferedImage(img, screenContents, false, true);
// save the image
ImageUtil.saveBufferedImage(img, outputfile);
}
Because this: http://code.google.com/p/libgdx-users/wiki/Screenshots
Has some kind of color error.
some colors are wrong and stuff using that code
What is wrong with PixmapIO?
The problem with BufferedImage is that it won’t work with iOS, Android, WebGL, or GLFW (maybe useful for OSX 10.7 users). It’s also generally slow.
The problem is that I have to read in images from a inputstream and after a few google searches, I didn’t see a clear way to do this.
Never actually tried this but I suppose you could read the bufferedimage pixel by pixel into the texture (does it use a raster or something? idk)
Thanks for the suggestions. I managed to fix it. I create a Gdx2DPixmap which can load from input streams, then I use that to create a pixmap, then I use the pixmap to create a texture.
Gdx2DPixmap gpm = new Gdx2DPixmap(menuBackground, Gdx2DPixmap.GDX2D_FORMAT_RGB888);
Pixmap pixmap = new Pixmap(gpm);
Texture background = new Texture(bgPixmap);
I think this bug could be fixed as there are already 2 helpers in JOGL to do that:
- com.jogamp.opengl.util.awt.Screenshot
- com.jogamp.opengl.util.GLReadBufferUtil (AWT-free, you can use it with TextureData and TextureIO)