How do I flip an image horizontally flip with glReadPixels() Bufferedimage and out put with ImageIO?

前端 未结 2 1245
迷失自我
迷失自我 2020-12-19 13:15

How do I flip an Screenshot image? I can\'t find my problem anywhere else.
Example code:

/*
*@param fileLoc //Location of fileoutput destination
*@param f         


        
2条回答
  •  生来不讨喜
    2020-12-19 14:01

    It's worth noting that it might be faster to simply read the pixels out of the buffer in the order you want them, rather than read them backwards and do a costly transform operation. Additionally, since you know for sure that the BufferedImage is TYPE_INT_RGB it should be safe to write directly into its raster.

    ByteBuffer fb = BufferUtils.createByteBuffer(WIDTH * HEIGHT * 3);
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb);
    int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    for (int i = pixels.length - 1; i >= 0; i--) {
        int x = i % WIDTH, y = i / WIDTH * WIDTH;
        pixels[y + WIDTH - 1 - x] = (fb.get() & 0xff) << 16 | (fb.get() & 0xff) << 8 | fb.get() & 0xff;
    }
    

提交回复
热议问题