java网页快照_java网页快照-网页转存为图片
java網頁快照-網頁轉存為圖片
個人覺得免費的java實現方式,最方便的網頁快照生成方式。可以生成大型網頁的快照。非常棒!
使用代碼前需要導入 jar包。需要三個jar包 :swt-3.6M3-win32-win32-x86.jar,DJNativeSwing-SWT.jar,DJNativeSwing.jar
運行環境:jdk1.6以上
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.p_w_picpath.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.p_w_picpathio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
public class Main 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 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 p_w_picpathSize = new Dimension(Integer.parseInt(result
.substring(0, index)), Integer.parseInt(result
.substring(index + 1)));
p_w_picpathSize.width = Math.max(originalSize.width,
p_w_picpathSize.width + 50);
p_w_picpathSize.height = Math.max(originalSize.height,
p_w_picpathSize.height + 50);
nativeComponent.setSize(p_w_picpathSize);
BufferedImage p_w_picpath = new BufferedImage(p_w_picpathSize.width,
p_w_picpathSize.height, BufferedImage.TYPE_INT_RGB);
nativeComponent.paintComponent(p_w_picpath);
nativeComponent.setSize(originalSize);
// 當網頁超出目標大小時
if (p_w_picpathSize.width > maxWidth
|| p_w_picpathSize.height > maxHeight) {
//截圖部分圖形
p_w_picpath = p_w_picpath.getSubp_w_picpath(0, 0, maxWidth, maxHeight);
/*此部分為使用縮略圖
int width = p_w_picpath.getWidth(), height = p_w_picpath
.getHeight();
AffineTransform tx = new AffineTransform();
tx.scale((double) maxWidth / width, (double) maxHeight
/ height);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//縮小
p_w_picpath = op.filter(p_w_picpath, null);*/
}
try {
// 輸出圖像
ImageIO.write(p_w_picpath, "jpg", new File(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() {
// SWT組件轉Swing組件,不初始化父窗體將無法啟動webBrowser
JFrame frame = new JFrame("以DJ組件保存指定網頁截圖");
// 加載google,最大保存為640x480的截圖
frame.getContentPane().add(
new Main(http://yijianfengvip.blog.163.com/, 640, 480),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 僅初始化,但不顯示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}
}
總結
以上是生活随笔為你收集整理的java网页快照_java网页快照-网页转存为图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果7和6sp哪个更实用
- 下一篇: 星露谷物语升级房子的方法