打印屏幕图像捕获



我需要编写一个程序,我将创建一个可运行的jar并分发。函数应如下所示:

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

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)
{
}   
}
}

第一次尝试捕获块可以打开一个窗口,获取图像格式,前缀和存储位置。第二次尝试捕获块单独可以在运行时拍摄屏幕截图,而不是在按下打印屏幕键时,但第一次尝试捕获它不会打印任何内容。那么,按下打印屏幕键时该怎么做才能截取屏幕截图?

我以另一种方式接近了解决方案。由于人们在联机会议中总是使用鼠标工作,因此我从键盘中删除了PrintScreen按钮的条款,相反,与会者可以单击旋转窗口按钮来捕获屏幕。

我的解决方案如下:

主类.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();
    }
}
}

打印按钮.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);
        }
    });
}
}

打印屏幕.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)
    {
    }
}
}

我希望这对一些朋友有所帮助。是否有任何改进的余地?

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

相关内容

  • 没有找到相关文章

最新更新