原貼發表在個人我的博客haha.tk | Ha?Have a Talk!上,轉載請註明出處,謝謝。html
一切都源於這個表情,後面這個表情就以不可預料的狀況演化了。那時想着作一個能加蒼蠅的截圖工具,程序是用JAVA實現的,項目託管在GitHub https://github.com/Bigcaitou/HEHEHEScreenshotjava
talk is cheap, show you the code.git
public RectD() { snapshot(); setVisible(true); // setSize(d);//最大化窗口 //用 JDialog 作一個無控制條的窗口,大小設置成滿屏,把截取的這個滿屏的圖片貼到這個 JDiaglog 裏 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addMouseListener(new MouseAdapter() {// 獲得鼠標箭頭初始座標 @Override public void mousePressed(MouseEvent e) { orgx = e.getX(); orgy = e.getY(); } }); //鼠標圈定區域, 對圈定的區域截屏 this.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { endx = e.getX(); endy = e.getY(); g = getGraphics(); g.drawImage(tempImage, 0, 0, RectD.this); int x = Math.min(orgx, endx); int y = Math.min(orgy, endy); int width = Math.abs(endx - orgx) + 1; int height = Math.abs(endy - orgy) + 1; // 加上1,防止width或height爲0 g.setColor(Color.BLUE); g.drawRect(x - 1, y - 1, width + 1, height + 1); // 減1,加1都是爲了防止圖片將矩形框覆蓋掉 saveImage = image.getSubimage(x, y, width, height); g.drawImage(saveImage, x, y, RectD.this); } }); //雙擊鼠標截圖 this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int clickTimes = e.getClickCount(); if (clickTimes == 2) { saveToFile(); } } }); }
public void snapshot() { try { Robot robot = new Robot(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); image = robot.createScreenCapture(new Rectangle(0, 0, d.width, d.height)); } catch (AWTException e) { e.printStackTrace(); } }
利用Robot的createScreenCapture能夠截圖,可是有個問題是遇到多個屏幕,這個方法只能在默認屏幕截圖。github
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 獲得畫筆對象 Graphics2D g = buffImg.createGraphics(); // 設置對線段的鋸齒狀邊緣處理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (null != degree) { // 設置水印旋轉 g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 水印圖象的路徑 水印通常爲gif或者png的,這樣可設置透明度 ImageIcon imgIcon = new ImageIcon(iconPath); // ImageIcon imgIcon = new ImageIcon(iconPath); // 獲得Image對象。 Image img = imgIcon.getImage(); float alpha = 1f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 表示水印圖片的位置 g.drawImage(img, 50, 50, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os = new FileOutputStream(targerPath); // 生成圖片 ImageIO.write(buffImg, "PNG", os); setClipboardImage(buffImg); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } }
蒼蠅的大小要合適,太大的話太假,過小的話素材太難摳了,最後蒼蠅素材是從一個蒼蠅畫刷工具而來的,我還找了會PS的同窗幫我畫上蒼蠅腿。ide
public static void setClipboardImage(final Image image) { Transferable trans = new Transferable() { public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] { DataFlavor.imageFlavor }; } public boolean isDataFlavorSupported(DataFlavor flavor) { return DataFlavor.imageFlavor.equals(flavor); } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported(flavor)) return image; throw new UnsupportedFlavorException(flavor); } }; Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null); }
public static class SystemTrayDemo extends JFrame { private static final long serialVersionUID = 1L; private TrayIcon trayIcon = null; public SystemTrayDemo() { if (SystemTray.isSupported()) { // 檢查當前系統是否支持系統托盤 SystemTray tray = SystemTray.getSystemTray(); // 獲取表示桌面托盤區的SystemTray實例。 Image image = this.getToolkit().getImage(this.getClass().getResource("/image/logo.png")); PopupMenu popupMenu = new PopupMenu(); MenuItem exitItem = new MenuItem("退出"); MenuItem menuItema = new MenuItem("截圖"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.exit(0); } catch (Exception ex) { ex.printStackTrace(); } } }); popupMenu.add(menuItema); popupMenu.add(exitItem); trayIcon = new TrayIcon(image, "呵呵呵截圖", popupMenu); while (flag) { flag = false; menuItema.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 這裏調用截圖功能 RectD rd = new RectD(); GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice(); gd.setFullScreenWindow(rd); } }); try { tray.add(trayIcon); // 將 TrayIcon 添加到 SystemTray。 } catch (AWTException e) { System.err.println(e); } } } else { System.out.println("你的系統不支持系統托盤"); } try { } catch (Exception e) { } } }
可能會遇到托盤亂碼,能夠參考 《java軟件托盤MenuItem 顯示中文出現框框亂碼、讀取ini配置文件亂碼》 ,來解決問題。工具
public void saveToFile() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_"); String name = sdf.format(new Date()); File path = FileSystemView.getFileSystemView().getHomeDirectory(); // 圖片保存在桌面 String format = "png"; File f = new File(path + File.separator + name + "." + format); try { ImageIO.write(saveImage, format, f); String srcImgPath = path + "\\" + name + ".png"; String iconPath = System.getProperty("user.dir") + "\\1.png"; // URL iconPath = this.getClass().getResource("/image/1.png"); String targerPath = path + "\\" + name + "呵呵呵.png"; this.dispose(); //給圖片添加水印 HEHEHEScreenshot.markImageByIcon(iconPath, srcImgPath, targerPath, null); } catch (IOException e) { e.printStackTrace(); } }
效果圖以下所示:優化
|相關連接 | |:-------------:| | 蒼蠅畫刷工具下載 | | 項目主頁 | | java 截圖源碼 | | Java實現給圖片添加水印 | | java軟件托盤MenuItem 顯示中文出現框框亂碼、讀取ini配置文件亂碼 |this
下次再講.net
- jar的運行
- jar→exe
- jre精簡
- 跨平臺優化