使用 LWJGL 的桌面捕获
Desktop Capture using LWJGL
我想创建一个用于捕获桌面并将其保存到文件的应用程序。
我为此检查了一些 LWJGL 代码。
但是它只捕获 OpenGL window.
这是我使用的代码片段。
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);
}
}
我试过的另一个是:
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;
}
甚至可以使用 LWJGL 捕获桌面吗?
您无法使用 LWJGL 或 OpenGL 捕获桌面。 Opengl 只处理它自己的帧缓冲区,然后将其打印在屏幕上。任何不在帧缓冲区(或您自己的程序中的某个地方)的任何东西,例如其他程序、桌面等,都不会传递给 OpenGL,因此它无法读取该数据。但是,java.awt 有一个 class 可以让您捕获屏幕。参见 here。
我想创建一个用于捕获桌面并将其保存到文件的应用程序。
我为此检查了一些 LWJGL 代码。 但是它只捕获 OpenGL window.
这是我使用的代码片段。
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);
}
}
我试过的另一个是:
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;
}
甚至可以使用 LWJGL 捕获桌面吗?
您无法使用 LWJGL 或 OpenGL 捕获桌面。 Opengl 只处理它自己的帧缓冲区,然后将其打印在屏幕上。任何不在帧缓冲区(或您自己的程序中的某个地方)的任何东西,例如其他程序、桌面等,都不会传递给 OpenGL,因此它无法读取该数据。但是,java.awt 有一个 class 可以让您捕获屏幕。参见 here。