JAVA實現網頁快照,存為圖片格式

來源:互聯網
上載者:User
截取的google的效果,將就吧,不是特別好。 但是作為普通的應用,我想這個效果我已經很滿意了。
注意,裡面的
this.setVisible(true);
這句話如果運行在一些不能顯示圖形介面的機器上,請屏蔽掉它,不過這樣的話,網頁裡的圖片就不能被截取了。


完整的原始碼如下:

  1. import java.awt.Graphics2D;
  2. import java.awt.RenderingHints;
  3. import java.awt.geom.AffineTransform;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.ColorModel;
  6. import java.awt.image.WritableRaster;
  7. import java.io.*;
  8. import javax.imageio.*;
  9. import javax.swing.*;
  10. /**
  11.  * HTML2JPG,HTML頁面轉圖片的實現方法。
  12.  * 
  13.  * @author 老紫竹(Java世紀網,java2000.net)
  14.  */
  15. public class Test extends JFrame {
  16.   public Test(String url, File file) throws Exception {
  17.     JEditorPane editorPane = new JEditorPane();
  18.     editorPane.setEditable(false);
  19.     editorPane.setPage(url);
  20.     JScrollPane jsp = new JScrollPane(editorPane);
  21.     getContentPane().add(jsp);
  22.     this.setLocation(0, 0);
  23.     this.setVisible(true); // 如果這裡不設定可見,則裡面的圖片等無法截取
  24.     
  25.     // 如果不延時,則圖片等可能沒有時間下載顯示
  26.     // 具體的秒數需要根據網速等調整
  27.     Thread.sleep(5 * 1000);
  28.     setSize(10000, 10000);
  29.     pack();
  30.     // BufferedImage image = new BufferedImage(editorPane.getWidth(),
  31.     // editorPane.getHeight(), BufferedImage.TYPE_INT_RGB);
  32.     BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(),
  33.         BufferedImage.TYPE_INT_RGB);
  34.     Graphics2D graphics2D = image.createGraphics();
  35.     editorPane.paint(graphics2D);
  36.     
  37.     BufferedImage image1 = resize(image, 600, 400);
  38.     ImageIO.write(image1, "jpg", file);
  39.     dispose();
  40.   }
  41.   public static void main(String[] args) throws Exception {
  42.     new Test("http://www.google.cn", new File("d:/file.jpg"));
  43.   }
  44.   public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
  45.     // targetW,targetH分別表示目標長和寬
  46.     int type = source.getType();
  47.     BufferedImage target = null;
  48.     double sx = (double) targetW / source.getWidth();
  49.     double sy = (double) targetH / source.getHeight();
  50.     // 這裡想實現在targetW,targetH範圍內實現等比縮放。如果不需要等比縮放
  51.     // 則將下面的if else語句注釋即可
  52.     if (sx > sy) {
  53.       sx = sy;
  54.       targetW = (int) (sx * source.getWidth());
  55.       // } else {
  56.       // sy = sx;
  57.       // targetH = (int) (sy * source.getHeight());
  58.     }
  59.     if (type == BufferedImage.TYPE_CUSTOM) { // handmade
  60.       ColorModel cm = source.getColorModel();
  61.       WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
  62.       boolean alphaPremultiplied = cm.isAlphaPremultiplied();
  63.       target = new BufferedImage(cm, raster, alphaPremultiplied, null);
  64.     } else
  65.       target = new BufferedImage(targetW, targetH, type);
  66.     Graphics2D g = target.createGraphics();
  67.     // smoother than exlax:
  68.     g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  69.     g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
  70.     g.dispose();
  71.     return target;
  72.   }
  73. }

轉自:http://blog.csdn.net/java2000_net/archive/2008/12/30/3643528.aspx#995286

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.