Hello !
Is it possible to save the GLCanvas into a BufferedImage ?
If yes, what are the methods ?
Thank you very much.
Hello !
Is it possible to save the GLCanvas into a BufferedImage ?
If yes, what are the methods ?
Thank you very much.
I think this has been discussed before but this is what I have done:
float[] vPixelBuffer = new float[pWidth * pHeight * 3];
// First read the frame buffer
gl.glReadPixels(
0,
0,
pWidth,
pHeight,
GL.GL_RGB,
GL.GL_FLOAT,
vPixelBuffer);
BufferedImage vBufferedImage =
new BufferedImage(pWidth, pHeight,
BufferedImage.TYPE_INT_RGB);
// Copy pixel data into buffered image
for (int vRow = 0; vRow < pHeight; vRow++)
{
for (int vColumn = 0; vColumn < pWidth; vColumn++)
{
// Get separate color components
int vIndex = ((vRow * pWidth) + vColumn) * 3;
int r = (int) (vPixelBuffer[vIndex] * 255);
int g = (int) (vPixelBuffer[vIndex + 1] * 255);
int b = (int) (vPixelBuffer[vIndex + 2] * 255);
/*
* Set rgb color by shifting components into corresponding
* integer bits.
*/
int vRgb = (r << 16) + (g << 8) + b;
// Set buffer image pixel -- flip y coordinate
vBufferedImage.setRGB(
vColumn,
pHeight - vRow - 1,
vRgb);
}
}
// do something with vBufferedImage
Also, I would interested if there was a more efficient way of converting the float pixel buffer into a BufferedImage. This is the first thing I could think of and I have since moved on to more pressing things.
Sean
Very well Sylvie ! ;D
Thank you very much, your code is working very well !
Bye bye !
Hello everyone,
I am dealing with the same problem, but somehow i can’t manage to get the code running right. When i create a picture from the BufferedImage, i only get a screen cotaining
the backgroundcolor from the glcanvas. It looks like the buffer is not read properly. I tried to copy the image several times while moving the object on the canvas. When the objects move outside the visible screen, somthing is showing up inside the bufferedimage, but it’s only a square containing colors of the original image. Are there any parameters in the code i possibly have to adjust?
Thank you very much in advance,
Chris
I am not sure much,
but i had the same problem, where in glReadPixels was returnning all ‘zeros’.
so I put glReadPixels in display() immediately after glFlush(). and things seem to work properly.
-j