How to flip an image horizontally using glReadPixels () Bufferedimage and out using ImageIO?

How to flip a screenshot? I can not find the problem elsewhere. Code example:

/* *@param fileLoc //Location of fileoutput destination *@param format //"png" *@param WIDTH //Display.width(); *@param HEIGHT //Display.height(); */ private void getScreenImage(){ int[] pixels = new int[WIDTH * HEIGHT]; int bindex; // allocate space for RBG pixels ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3);//.order(ByteOrder.nativeOrder()); // grab a copy of the current frame contents as RGB glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb); BufferedImage image = 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)); } try { //Create a BufferedImage with the RGB pixels then save as PNG image.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH); ImageIO.write(image, format , fileLoc); } catch (Exception e) { System.out.println("ScreenShot() exception: " +e); } } 

Basically, the code works to capture the screen and save in the format of "png".
But it displays the image horizontally because glReadPixels(); ,
read from bottom left to top right.

So, as I flip the image horizontally until I ImageIO.write(); ?

Thanks, Rose.

+4
source share
2 answers

eg. Click the image horizontally using AffineTransform .

flipping an image horizontally

 import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import javax.swing.*; public class Test001 { public static BufferedImage getFlippedImage(BufferedImage bi) { BufferedImage flipped = new BufferedImage( bi.getWidth(), bi.getHeight(), bi.getType()); AffineTransform tran = AffineTransform.getTranslateInstance(bi.getWidth(), 0); AffineTransform flip = AffineTransform.getScaleInstance(-1d, 1d); tran.concatenate(flip); Graphics2D g = flipped.createGraphics(); g.setTransform(tran); g.drawImage(bi, 0, 0, null); g.dispose(); return flipped; } Test001(BufferedImage bi) { JPanel gui = new JPanel(new GridLayout(1,2,2,2)); gui.add(new JLabel(new ImageIcon(bi))); gui.add(new JLabel(new ImageIcon(getFlippedImage(bi)))); JOptionPane.showMessageDialog(null, gui); } public static void main(String[] args) throws AWTException { final Robot robot = new Robot(); Runnable r = new Runnable() { @Override public void run() { final BufferedImage bi = robot.createScreenCapture( new Rectangle(0, 360, 200, 100)); new Test001(bi); } }; SwingUtilities.invokeLater(r); } } 
+6
source

It's worth noting that it’s faster to just read the pixels from the buffer in the order you need them, instead of reading them back and performing an expensive conversion operation. In addition, since you know for sure that BufferedImage is TYPE_INT_RGB , you should safely write directly to its raster file.

 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; } 
0
source

All Articles