How to make a simple screenshot method using LWJGL?

房东的猫 提交于 2020-01-06 07:55:41

问题


So basically I was messing about with LWJGL for a while now, and I came to a sudden stop with with annoyances surrounding glReadPixels().
And why it will only read from left-bottom -> top-right.
So I am here to answer my own question since I figured all this stuff out,
And I am hoping my discoveries might be of some use to someone else.

As a side-note I am using:

glOrtho(0, WIDTH, 0 , HEIGHT, 1, -1);

回答1:


So here it is my screen-capture code which can be implemented in any LWJGL application C:

//=========================getScreenImage==================================//
    private 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
             glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, 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.
                 ImageIO.write(imageOut, format , fileLoc);
             }
             catch (Exception e) {
                 System.out.println("ScreenShot() exception: " +e);
             }
         }

I hope this has been useful.
For any questions or comments on the code, ask/suggest as you like. C:
Hugs,
Rose.




回答2:


sorry for the late reply but this is for anybody still looking for a solution.

public static void saveScreenshot() throws Exception {
    System.out.println("Saving screenshot!");
    Rectangle screenRect = new Rectangle(Display.getX(), Display.getY(), Display.getWidth(), Display.getHeight());
    BufferedImage capture = new Robot().createScreenCapture(screenRect);
    ImageIO.write(capture, "png", new File("doc/saved/screenshot.png"));
}


来源:https://stackoverflow.com/questions/13819030/how-to-make-a-simple-screenshot-method-using-lwjgl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!