通過代碼來實現網頁截圖

來源:互聯網
上載者:User

有時候,我們需要用代碼來網頁,其實要實現這個功能,無非就是要麼實現一個模擬瀏覽器,要麼調用系統瀏覽器,唯有此而發而已。這裡還是用的最常見的第二種,第一種難度很大。在網上找了一個庫,很好用,記錄下來,僅供有需要的同學參考。
:http://code.google.com/p/greenvm/downloads/detail?name=Screenshot.7z&can=2&q=
    Screenshot就是這樣的一個程式,這是一個以DJNativeSwing於系統後台呼叫瀏覽器,產生指定網頁地址的樣本。
部分核心代碼如下:
public Main(final String url, final int maxWidth, final int maxHeight) {
        super(new BorderLayout());
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        final String fileName = System.currentTimeMillis() + ".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) {
                    String result = (String) webBrowser
                            .executeJavascriptWithResult(jsDimension.toString());
                    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 + 50);
                    imageSize.height = Math.max(originalSize.height,
                            imageSize.height + 50);
                    nativeComponent.setSize(imageSize);
                    BufferedImage image = new BufferedImage(imageSize.width,
                            imageSize.height, BufferedImage.TYPE_INT_RGB);
                    nativeComponent.paintComponent(image);
                    nativeComponent.setSize(originalSize);
                    // 當網頁超出目標大小時
                    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(fileName));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    // 退出操作
                    System.exit(0);
                }
            }
        }

        );
        add(panel, BorderLayout.SOUTH);

    }

轉載註明:http://www.zhurouyoudu.com/index.php/archives/374/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.