java根據url生成網頁截圖,縮圖
阿新 • • 發佈:2019-02-17
public class ShotsPic extends JPanel { private static final long serialVersionUID = 1L; // 行分隔符 final static public String LS = System.getProperty("line.separator", "\n"); // 檔案分割符 final static public String FS = System.getProperty("file.separator", "\\"); //以javascript指令碼獲得網頁全屏後大小 final static StringBuffer jsDimension; static { jsDimension = new StringBuffer(); jsDimension.append("var width = 0;").append(LS); jsDimension.append("var height = 0;").append(LS); jsDimension.append("if(document.documentElement) {").append(LS); jsDimension.append(" width = Math.max(width, document.documentElement.scrollWidth);").append(LS); jsDimension.append(" height = Math.max(height, document.documentElement.scrollHeight);").append(LS); jsDimension.append("}").append(LS); jsDimension.append("if(self.innerWidth) {").append(LS); jsDimension.append(" width = Math.max(width, self.innerWidth);").append(LS); jsDimension.append(" height = Math.max(height, self.innerHeight);").append(LS); jsDimension.append("}").append(LS); jsDimension.append("if(document.body.scrollWidth) {").append(LS); jsDimension.append(" width = Math.max(width, document.body.scrollWidth);").append(LS); jsDimension.append(" height = Math.max(height, document.body.scrollHeight);").append(LS); jsDimension.append("}").append(LS); jsDimension.append("return width + ':' + height;"); } public ShotsPic( String url) { super(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); final String fileName = "screenShots.jpg"; final JWebBrowser webBrowser = new JWebBrowser(null); webBrowser.setBarsVisible(false); webBrowser.navigate(url); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); add(webBrowserPanel, BorderLayout.CENTER); JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4)); webBrowser.addWebBrowserListener(new WebBrowserAdapter() { // 監聽載入進度 public void loadingProgressChanged(WebBrowserEvent e) { // 當載入完畢時 if (e.getWebBrowser().getLoadingProgress() == 100) { //動態執行JS 最終返回網頁的實際的寬度和高度 String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString()); System.out.println(result); int index = result == null ? -1 : result.indexOf(":"); NativeComponent nativeComponent = webBrowser.getNativeComponent(); Dimension originalSize = nativeComponent.getSize(); Dimension imageSize = new Dimension(Integer.parseInt(result.substring(0, index)), Integer.parseInt(result.substring(index + 1))); imageSize.width = Math.max(originalSize.width,imageSize.width); imageSize.height = Math.max(originalSize.height,imageSize.height); System.out.println(imageSize.width); System.out.println(imageSize.height); nativeComponent.setSize(imageSize); BufferedImage image = new BufferedImage(imageSize.width,imageSize.height, BufferedImage.TYPE_INT_RGB); nativeComponent.paintComponent(image); //nativeComponent.setSize(imageSize); // 當網頁超出目標大小時 /*if (imageSize.width > maxWidth|| imageSize.height > maxHeight) { //截圖部分圖形 image = image.getSubimage(0, 0, maxWidth, maxHeight); }*/ /*//此部分為使用縮圖 int width = image.getWidth(), height = image .getHeight(); AffineTransform tx = new AffineTransform(); tx.scale((double) maxWidth / width, (double) maxHeight / height); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); //縮小 image = op.filter(image, null); */ try { // 輸出影象 ImageIO.write(image, "jpg", new File("E:\\"+fileName)); } catch (IOException ex) { ex.printStackTrace(); } // 退出操作 System.exit(0); } } } ); add(panel, BorderLayout.SOUTH); } public static void main(String[] args) { NativeInterface.open(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("以DJ元件儲存指定網頁截圖"); frame.getContentPane().add( new ShotsPic("www.qq.com"), BorderLayout.CENTER); frame.setSize(1280, 768); // 僅初始化,但不顯示 frame.invalidate(); frame.pack(); frame.setVisible(false); } }); NativeInterface.runEventPump(); } }
程式需要三個包:,下載地址請參考原文地址!
注:如果網速不是很好,會對截圖有一定的影響!