Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full screen capture of a DirectX program in java (Javacv ?)

[For Windows] I know it is possible to capture screen of a DirectX program running under C# language, but do you know some sample code for Java?

I am actually facing this same problem than this Take screen shots inside of full screen applications with java?. Robot class didn't helped and neither worked.

But yet I didn't found any sample of java code on the internet concerning this. Thanks for any help you could provide on this topic.

like image 948
Arsenic Avatar asked Nov 03 '22 16:11

Arsenic


1 Answers

Since I worked on it more, see also:

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;

import javax.imageio.ImageIO;
import java.io.File;

public class ClipboardScreenshot {

    public static void main(String[] args) throws Exception {
        // get the screenshot
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_PRINTSCREEN);
        robot.delay(40);
        robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
        robot.delay(404);

        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        DataFlavor[] flavors = cb.getAvailableDataFlavors();
        System.out.println("After: ");
        for (DataFlavor flavor : flavors) {
            System.out.println(flavor);
            if (flavor.toString().indexOf("java.awt.Image")>0) {
                Object o = cb.getData(flavor);
                Image i = (Image)o;
                // ImageIO will not write an Image
                // It will write a BufferedImage (a type of RenderedImage)
                BufferedImage bi = new BufferedImage(
                        i.getWidth(null),
                        i.getHeight(null),
                        BufferedImage.TYPE_INT_RGB);
                Graphics2D g = bi.createGraphics();
                g.drawImage(i, 0, 0, null);
                g.dispose();

                JScrollPane sp = new JScrollPane(new JLabel(new ImageIcon(bi)));
                sp.setPreferredSize(new Dimension(800,600));
                JOptionPane.showMessageDialog(null, sp);
                File f = new File(
                        System.getProperty("user.home") + 
                        File.separator + 
                        "the.png");
                ImageIO.write(bi, "png", f);
            }
        }
    }
}
like image 74
Andrew Thompson Avatar answered Nov 15 '22 01:11

Andrew Thompson