I want to create an application using to capture desktop and save it to a file.
I went through a few LWJGL codes for this. However it only captures OpenGL window.
Here are the code snippets I used.
private static void screenShot(){
//Creating an rbg array of total pixels
int[] pixels = new int[WIDTH * HEIGHT];
int bindex;
// allocate space for RBG pixels
ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3);
// grab a copy of the current frame contents as RGB
GL11.glReadPixels(0, 0, WIDTH, HEIGHT, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, fb);
BufferedImage imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
// convert RGB data in ByteBuffer to integer array
for (int i=0; i < pixels.length; i++) {
bindex = i * 3;
pixels[i] =
((fb.get(bindex) << 16)) +
((fb.get(bindex+1) << 8)) +
((fb.get(bindex+2) << 0));
}
//Allocate colored pixel to buffered Image
imageIn.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH);
//Creating the transformation direction (horizontal)
AffineTransform at = AffineTransform.getScaleInstance(1, -1);
at.translate(0, -imageIn.getHeight(null));
//Applying transformation
AffineTransformOp opRotated = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
BufferedImage imageOut = opRotated.filter(imageIn, null);
try {//Try to screate image, else show exception.
File output1 = new File("image.png");
ImageIO.write(imageOut, "PNG" , output1);
}
catch (Exception e) {
System.out.println("ScreenShot() exception: " +e);
}
}
Another one I tried was:
public static BufferedImage glScreenshot() {
GL11.glReadBuffer(GL11.GL_FRONT);
int w = Display.getDisplayMode().getWidth();
int h = Display.getDisplayMode().getHeight();
int bpp = 4;
ByteBuffer buffer = BufferUtils.createByteBuffer(w * h * bpp);
GL11.glReadPixels(0, 0, w, h, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int i = (x + (w * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, h - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}
return image;
}
Is it even possible to capture the desktop using LWJGL?