Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (581)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX BufferedImage to Texture  (Read 4234 times)
0 Members and 1 Guest are viewing this topic.
Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Posted 2013-04-10 01:13:57 »

Is there any way to convert a BufferedImage to a Texture?

Offline BurntPizza

« JGO Bitwise Duke »

Medals: 388
Exp: 6 years

« Reply #1 - Posted 2013-04-10 01:16:23 »

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.
Offline ReBirth
« Reply #2 - Posted 2013-04-10 02:01:28 »

Try to not mix libgdx with java2d. +1 for pixmap.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2013-04-10 06:38:18 »

If you are using libgdx, you should NEVER EVER touch anything from java2d.

Have fun.  Cheesy


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.

Offline Cero
« Reply #4 - Posted 2013-04-10 13:49:39 »

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:
Has some kind of color error.
some colors are wrong and stuff using that code

Offline davedes
« Reply #5 - Posted 2013-04-10 16:17:36 »

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.

Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Reply #6 - Posted 2013-04-10 23:45:47 »

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.

Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 161
Projects: 5
Exp: 3 years

« Reply #7 - Posted 2013-04-11 04:51:43 »

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)
Offline Nate

« JGO Bitwise Duke »

Medals: 164
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #8 - Posted 2013-04-11 05:20:12 »

Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Reply #9 - Posted 2013-04-11 10:33:54 »

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);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #10 - Posted 2013-04-11 11:41:25 »

Because this:
Has some kind of color error.
some colors are wrong and stuff using that code
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)

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

MrMapcom (16 views)
2015-05-23 20:26:16

MrMapcom (22 views)
2015-05-23 20:23:34

Waterwolf (29 views)
2015-05-20 15:01:45

chrislo27 (36 views)
2015-05-20 03:42:21

BurntPizza (71 views)
2015-05-10 15:53:18

FrozenShade (56 views)
2015-05-07 09:11:21

TheLopais (219 views)
2015-05-06 13:36:48

TheLopais (202 views)
2015-05-06 13:35:14

TheLopais (207 views)
2015-05-06 13:33:39

TheLopais (228 views)
2015-05-06 13:32:48
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!