本文介绍了如何使用LWJGL制作简单的屏幕截图方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

所以基本上我现在已经和LWJGL纠缠了一段时间,但由于glReadPixels()周围的烦恼,我突然停了下来.
以及为什么只从左下角读取->右上角.
所以我在这里回答我自己的问题,因为我已经弄清楚了所有这些东西,
我希望我的发现可能对其他人有用.

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

推荐答案

所以这是我的屏幕捕获代码,可以在任何LWJGL应用程序C中实现:

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

我希望这对您有所帮助.
对代码有任何疑问或意见,请根据自己的喜好提出建议. C:
拥抱,
玫瑰.

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

这篇关于如何使用LWJGL制作简单的屏幕截图方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:49