簡體   English   中英

自動捕獲剪貼板數據並保存到文件中

[英]Automatically capture clip board data and save it into a file

每當按下 Print screen 時從剪貼板中捕獲圖像並使用 java 將其保存在文件 (.doc) 中

主要目的是從剪貼板復制數據並自動將其保存到本地磁盤,而無需轉到所需程序(即 MS Word)-單擊新建-按 (Ctrl+V) 粘貼並使用名稱保存。

代碼應自動執行上述所有三個步驟。

我的源代碼

public class CaptureScreenShot {

    private static String DIR  ="C:\\QUIS\\";
    private static JTextField txtDocNumber;
    public static void main(String[] args) throws Exception{
        txtDocNumber = new JTextField();
        Robot robot = new Robot();


        Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
        int width = (int) d.getWidth();
        int height = (int) d.getHeight();

        robot.delay(5000);

        Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
                height));
        BufferedImage  bi = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);

        String fileNameToSaveTo = "C:/QUIS/screenCapture_" + createTimeStampStr() + ".PNG";
        String newFile = "C:/QUIS/x" + ".org";
        File newFilee = new File(newFile);

        writeImage(bi, fileNameToSaveTo, "PNG");

        System.out.println("Screen Captured Successfully and Saved to:\n"+fileNameToSaveTo);

        Desktop desktop = Desktop.getDesktop();  
        writeImage(bi, newFile, "org");
        desktop.open(newFilee); 
    }

    public static int writeImage(BufferedImage img, String fileLocation,
            String extension) {
        try {
            BufferedImage bi = img;
            File outputfile = new File(fileLocation);
            ImageIO.write(bi, extension, outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    } 
    public static String createTimeStampStr() throws Exception {
        Calendar mycalendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
        String timeStamp = formatter.format(mycalendar.getTime());

        return timeStamp;
    }
}

如果您對 Java 代碼不挑剔,則可以使用一些屏幕截圖工具代替。 Snagit 是一個很好的工具。 你可以在http://www.techsmith.com/snagit.html上找到它

試試這個示例代碼,根據您的問題,它將從剪貼板復制內容並生成圖像文件

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    try {
        //Get data from clipboard and assign it to an image.
        //clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
        BufferedImage image = (BufferedImage)clipboard.getData(DataFlavor.imageFlavor);

        //file that we'll save to disk.
        File file = new File("image.jpg");

        //class to write image to disk.  You specify the image to be saved, its type,
        // and then the file in which to write the image data.
        ImageIO.write(image, "jpg", file);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM