import java.awt.Graphics2D; import java.awt.Image; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.util.Collection; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; public class Test { private static Robot robot = null; public static void main(String[] args) throws Exception { String folderPath = args.length > 0 ? args[0] : "C:\Users\Administrator\Documents\Tencent Files"; Collection<File> files = FileUtils.listFiles(new File(folderPath), new String[] { "flv", "mp4" }, true); robot = new Robot(); // switchTask(); openQQPlayer(); Thread.sleep(500); System.out.println("切换任务成功>...................."); Thread.sleep(500); for (File file : files) { openFile(file.getAbsolutePath()); Thread.sleep(1500); fastPlay(); Thread.sleep(1500); pauseOrPlay(); Thread.sleep(1500); capture(); Thread.sleep(500); saveImg(FilenameUtils.getBaseName(file.getName())); } } /** * 打开QQ影音 */ private static void openQQPlayer() { press(KeyEvent.VK_WINDOWS, KeyEvent.VK_DOWN); press(KeyEvent.VK_WINDOWS, KeyEvent.VK_DOWN); press(KeyEvent.VK_WINDOWS, KeyEvent.VK_R); robot.delay(100); setIntoClipboard("C:/Program Files (x86)/Tencent/QQPlayer/QQPlayer.exe"); press(KeyEvent.VK_CONTROL, KeyEvent.VK_V); robot.delay(100); press(KeyEvent.VK_ENTER); } /** * 从剪贴板获取图片 * * @return * @throws Exception */ public static Image getImageFromClipboard() throws Exception { Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable cc = sysc.getContents(null); if (cc == null) { return null; } else if (cc.isDataFlavorSupported(DataFlavor.imageFlavor)) { return (Image) cc.getTransferData(DataFlavor.imageFlavor); } return null; } /** * 保存图片 * * @param fileName */ public static void saveImg(String fileName) { new Thread(new Runnable() { @Override public void run() { try { Image image = getImageFromClipboard(); while (image == null) { image = getImageFromClipboard(); } BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, null, null); // ImageIO.write((RenderedImage)bufferedImage, "jpg", file); File f = new File("D:/capture/"); if (!f.exists()) { f.mkdirs(); } ImageIO.write((RenderedImage) bufferedImage, "png", new File("D:/capture/" + fileName + ".png")); } catch (Exception e) { e.printStackTrace(); } } }).start(); } /** * 调用QQ影音快捷键打开视频文件 * * @param path */ public static void openFile(String path) { press(KeyEvent.VK_CONTROL, KeyEvent.VK_O); setIntoClipboard(path); clearTexBox(); press(KeyEvent.VK_CONTROL, KeyEvent.VK_V); press(KeyEvent.VK_ENTER); } /** * 清空文本框 * * @param path */ public static void clearTexBox() { robot.keyPress(KeyEvent.VK_BACK_SPACE); robot.delay(500); robot.keyRelease(KeyEvent.VK_BACK_SPACE); } /** * 快进播放 */ public static void fastPlay() { press(KeyEvent.VK_CONTROL, KeyEvent.VK_RIGHT); } /** * alt+tab切换任务 */ public static void switchTask() { press(KeyEvent.VK_ALT, KeyEvent.VK_TAB); } /** * 播放/暂停 */ public static void pauseOrPlay() { press(KeyEvent.VK_SPACE); } /** * 切换下一个视频 */ public static void switchNextVideo() { press(KeyEvent.VK_PAGE_DOWN); } /** * 设置剪贴板内容 * * @param data */ public static void setIntoClipboard(String data) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection ss = new StringSelection(data); clipboard.setContents(ss, null); } /** * 调用QQ影音截图 * * @throws Exception */ public static void capture() throws Exception { press(KeyEvent.VK_ALT, KeyEvent.VK_A); robot.delay(50); press(KeyEvent.VK_ENTER); robot.delay(50); } /** * 控制键盘按键输入指定不含中文的字符串 * * @param s */ public static void pressCode(String s) { char[] ch = s.toCharArray(); press(ch); } /** * 控制键盘按键输入指定不含中文的字符串 * * @param s */ public static void press(String s) { press(s.toCharArray()); } /** * 控制键盘按键输入指定键码 * * @param s */ private static void press(int... keycode) { for (int key : keycode) { robot.keyPress(key); } robot.delay(150); for (int key : keycode) { robot.keyRelease(key); } } /** * 控制键盘按键输入指定字符 * * @param s */ private static void press(char... keycode) { char errorKey = ' '; try { for (int key : keycode) { errorKey = (char) key; if (key == ':') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SEMICOLON); robot.delay(150); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_SEMICOLON); } else if (key == '.') { robot.keyPress(KeyEvent.VK_PERIOD); robot.delay(150); robot.keyRelease(KeyEvent.VK_PERIOD); } else if ('a' <= key && key <= 'z') { robot.delay(20); robot.keyPress(key - ' '); robot.delay(20); robot.delay(20); robot.keyRelease(key - ' '); } else if ('A' <= key && key <= 'Z') { robot.keyPress(KeyEvent.VK_SHIFT); robot.delay(20); robot.keyPress(key); robot.delay(20); robot.keyRelease(KeyEvent.VK_SHIFT); robot.delay(20); robot.keyRelease(key); } else { robot.keyPress(key); } } robot.delay(150); for (int key : keycode) { if (key == ':' || key == '.') { continue; } else if ('a' < key && key <= 'z') { continue; } else if ('A' <= key && key <= 'Z') { continue; } else { robot.keyRelease(key); } } } catch (Exception e) { System.out.println(errorKey); e.printStackTrace(); } } }