有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

windows如何评测Java应用程序以发现性能瓶颈?

这个应用程序(.jar文件)使用robot类截图,将用户的文件名(URL)输入到JFrame输入框中。截断并删除其中的所有非法字符,并使用以时间戳为前缀的“另存为”对话框进行保存

我正在使用Windows Low Level 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);

}
}

共 (0) 个答案