PrintScreen图像捕获 [英] PrintScreen image capturing

查看:111
本文介绍了PrintScreen图像捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来创建可运行的jar并分发.功能应如下所示:

I need to write a program that I will create a runnable jar and distribute. The functions should be like below:

当双击罐子时,它将打开一个窗口. 它将询问路径将图像文件保存在何处. 然后它将询问是否在每个图像上都添加任何前缀/后缀/以及唯一名称的时间戳. 它还会询问要使用哪种图像格式. 该应用程序可以最小化和关闭 每当按下并保存PrintScreen时,它将截取完整的屏幕截图. 请提供一个完整的程序.我已经收集了碎片,但无法将它们合二为一.这是我的代码:-

when double click the jar, it will open a window. it will ask the path where to save the image files. it will then ask whether to add any prefix / suffix / both on every image along with timestamp for unique name. it will also ask what image format to use. the app can be minimized and closed it will take a full screenshot whenever PrintScreen is pressed and save. Please provide a programme that is complete. I have gathered pieces but could not put them in one. Here is my code :-

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;

public class MainClass
{
static String location = "";
static String prefix = "";
static String format = "";
static Date timestamp = new Date();

public static void main(String args[])
{
    try 
    {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
JFrame f = new JFrame("Text Field Examples");
f.getContentPane().setLayout(new FlowLayout());

final JTextField textField1 = new JTextField("Enter Location To Save Image Files");
textField1.addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){
        textField1.setText("");
    }
});
textField1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        location = textField1.getText();
        System.out.println(location);
    }
});
f.getContentPane().add(textField1);

final JTextField textField2 = new JTextField("Enter Prefix or Leave Empty");
textField2.addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){
        textField2.setText("");
    }
});
textField2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        prefix = textField2.getText();
        System.out.println(prefix);
    }
});
f.getContentPane().add(textField2);

String  jlistData[] =
    {
        "GIF",
        "PNG",
        "JPG"
    };
final JComboBox  jlist = new JComboBox<String>( jlistData );
jlist.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        format = jlist.getSelectedItem().toString();
        System.out.println(format);

    }
});
f.getContentPane().add(jlist);

f.pack();
f.setVisible(true);
} 
catch (Exception evt) 
{
    evt.printStackTrace();
}



try
{
    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);


    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    RenderedImage image = (RenderedImage) t.getTransferData(DataFlavor.imageFlavor);

    ImageIO.write(image, format, new File(new String(location+prefix+image+timestamp)));
}
catch(Exception e)
{

}   

}
}

第一个try catch块可以打开一个窗口,获取图像格式,前缀和存储位置.当不按printscreen键运行时,仅第二个try catch块可以拍摄屏幕快照,但是第一个try catch不能打印任何内容.那么,当按下printscreen键时如何截取屏幕截图?

The first try catch block can open a window, take image format, prefix and storage location. The second try catch block alone can take screen shot when run not when printscreen key is pressed but with the first try catch it does not print anything. So, what to do to take the screenshot when printscreen key is pressed ?

推荐答案

我以另一种方式解决了该问题. 由于人们在在线会议上始终使用鼠标,因此我从键盘上删除了PrintScreen按钮的子句,而与会者可以单击旋转窗口按钮来捕获屏幕.

I have approached the solution in a little bit another way. As people always works with mouse while on online meeting, I removed the clause of PrintScreen button from keyboard and instead the attendees can click on swing window button to capture screen.

我的解决方法如下:

MainClass.java

MainClass.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainClass{
static String location = "";
static String prefix = "";
static String format = "";

public static void main(String args[])
{
    try 
    {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    final JFrame f = new JFrame("ENTER ALL DETAILS BELOW");
    f.setAlwaysOnTop(true);
    f.getContentPane().setLayout(new FlowLayout());

    final JTextField textField1 = new JTextField("Enter Location To Save Image Files");
    textField1.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            textField1.setText("");
        }
    });
    f.getContentPane().add(textField1);

    final JTextField textField2 = new JTextField("Enter MeetingID");
    textField2.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            textField2.setText("");
        }
    });
    f.getContentPane().add(textField2);

    String  jlistData[] =
        {
            "GIF",
            "PNG",
            "JPG"
        };
    final JComboBox  jlist = new JComboBox<String>( jlistData );
    f.getContentPane().add(jlist);

    final JButton jButton = new JButton("OKAY");
    jButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            location = textField1.getText();
            prefix = textField2.getText();
            format = jlist.getSelectedItem().toString();
            System.out.println(location);
            System.out.println(prefix);
            System.out.println(format);
            f.setVisible(false);
            PrintButton.printButton();
        }
    });
    f.getContentPane().add(jButton);

    f.pack();
    f.setVisible(true);
    } 
    catch (Exception evt) 
    {
        evt.printStackTrace();
    }
}
}

PrintButton.java

PrintButton.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;


public class PrintButton 
{
    static void printButton()
    {
        try 
    {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    //final JFrame f = new JFrame("Print Screen App");
    Dlg f = new Dlg(new JFrame(), "PRINT");
    f.setAlwaysOnTop(true);
    f.getContentPane().setLayout(new FlowLayout());

    final JButton jButton = new JButton("OKAY");
    jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            PrintScreen.printScreen();
        }
    });
    f.getContentPane().add(jButton);

    f.pack();
    f.setVisible(true);
    } 
    catch (Exception evt) 
    {
        evt.printStackTrace();
    }
}
}

class Dlg extends JDialog {
    public Dlg(JFrame frame, String str) {
    super(frame, str);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
}
}

PrintScreen.java

PrintScreen.java

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.awt.image.RenderedImage;
import java.io.File;

import javax.imageio.ImageIO;


public class PrintScreen 
{
static void printScreen()
{
    try
    {
        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_PRINTSCREEN);
        robot.keyRelease(KeyEvent.VK_PRINTSCREEN);

        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        RenderedImage image = (RenderedImage) t.getTransferData(DataFlavor.imageFlavor);

        ImageIO.write(image, MainClass.format, new File(new String(MainClass.location+ "\\" +MainClass.prefix+"_"+System.currentTimeMillis()+"."+MainClass.format)));
    }
    catch(Exception e)
    {

    }
}
}

我希望这对一些朋友有帮助.有没有改善的空间?

I hope this will be helpfull to some freinds. Is there any scope to improve this?

如何为Windows和Linux/Ubuntu和Linux/RedHat创建可安装的版本?

How to create a installable version for Windows and Linux/Ubuntu and Linux/RedHat ?

这篇关于PrintScreen图像捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆