此应用程序(.jar文件)使用机器人类获取屏幕截图,将用户的文件名(URL)转换为JFrame输入框。截断并删除所有非法字符并使用带有时间戳作为前缀的另存为对话框进行保存。
我正在使用Windows低级KeyHook通过PrtSc键启动屏幕截图。
申请很慢。事件调用和事件操作之间的差距是1秒。
public class KeyHook {
private static HHOOK hhk;
private static LowLevelKeyboardProc keyboardHook;
static JFileChooser fileChooser = new JFileChooser();
public static void main(String[] args) {
final User32 lib = User32.INSTANCE;
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
keyboardHook = new LowLevelKeyboardProc()
{
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info)
{
if (nCode >= 0)
{
switch(wParam.intValue())
{
case WinUser.WM_KEYUP:
case WinUser.WM_KEYDOWN:
case WinUser.WM_SYSKEYUP:
case WinUser.WM_SYSKEYDOWN:
if (info.vkCode == 44)
{
try
{
Robot robot = new Robot();
BufferedImage bi=robot.createScreenCapture(new Rectangle(0,25,1366,744));
JFrame frame = new JFrame();
frame.toFront();
frame.requestFocus();
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String name = JOptionPane.showInputDialog(frame, "Enter file name");
frame.dispose();
String fileName= dovalidateFile(name);
FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG", ".png");
fileChooser.setFileFilter(filter);
fileChooser.setSelectedFile(new File (fileName));
int returnVal = fileChooser.showSaveDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION )
{
File file = fileChooser.getSelectedFile();
file = validateFile(file);
System.out.println(file);
ImageIO.write(bi, "png", file);
}
}
catch (NullPointerException e1) {e1.printStackTrace(); }
catch (AWTException e1) {e1.printStackTrace(); }
catch (IOException e1) {e1.printStackTrace();}
}
}
}
return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
}
private File validateFile(File file) {
DateFormat dateFormat = new SimpleDateFormat("HH.mm.ss.ddMMMMMyyyy");
Calendar cal = Calendar.getInstance();
String filePath = file.getAbsolutePath();
if (filePath.indexOf(".png") == -1)
{
filePath += "." + dateFormat.format(cal.getTime()) + ".png";
}
file = new File(filePath);
if (file.exists())
{
file.delete();
}
try
{
file.createNewFile();
}
catch (Exception e) {e.printStackTrace();}
return file;
}
private String dovalidateFile(String name) {
String input = name.replace("https://www.","");
input = input.replaceAll("http://www.","");
input = input.replaceAll("https://","");
input = input.replace("http://","");
input = input.replace("/?",".");
input = input.replace("/",".");
input = input.replace("|",".") ;
input = input.replace("%",".");
input = input.replace("<",".");
input = input.replace(">",".");
input = input.replaceAll("\\?",".");
input = input.replaceAll("\\*",".");
input = input.replace(":",".");
input = input.replace("\\",".");
input = Character.toUpperCase(input.charAt(0)) + input.substring(1);
return input;
}
};
hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
if(!SystemTray.isSupported())
{
return ;
}
SystemTray systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(KeyHook.class.getResource("/images/icon.png"));
PopupMenu trayPopupMenu = new PopupMenu();
MenuItem close = new MenuItem("Exit");
close.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.err.println("unhook and exit");
lib.UnhookWindowsHookEx(hhk);
System.exit(0);
}
});
trayPopupMenu.add(close);
TrayIcon trayIcon = new TrayIcon(image, "captur", trayPopupMenu);
trayIcon.setImageAutoSize(true);
try
{
systemTray.add(trayIcon);
}
catch(AWTException awtException){awtException.printStackTrace();}
int result;
MSG msg = new MSG();
while ((result = lib.GetMessage(msg, null, 0, 0)) != 0)
{
if (result == -1)
{
System.err.println("error in get message");
break;
}
else
{
System.err.println("got message");
lib.TranslateMessage(msg);
lib.DispatchMessage(msg);
}
}
lib.UnhookWindowsHookEx(hhk);
}
}