81,122
社区成员




public ImageUtils(final String url, final int maxWidth, final int maxHeight, final int pageNo,final String userID, final String topic) {
super(new BorderLayout());
System.out.println("url="+url);
JPanel webBrowserPanel = new JPanel(new BorderLayout());
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+20);
imageSize.height = Math.max(originalSize.height,imageSize.height);
nativeComponent.setSize(imageSize);
BufferedImage image = new BufferedImage(imageSize.width,imageSize.height, BufferedImage.TYPE_INT_RGB);
nativeComponent.paintComponent(image);
nativeComponent.setSize(originalSize);
// 当网页超出目标大小时
System.out.println("imageSize.width=" + imageSize.width);
System.out.println("maxWidth=" + maxWidth);
System.out.println("imageSize.height=" + imageSize.height);
System.out.println("maxHeight=" + maxHeight);
if (imageSize.width > maxWidth || imageSize.height > maxHeight) {
// 截图部分图形
image = image.getSubimage(0, 0, 780, 460);
}
try {
String fileDir = new PathUtils().getWebInfPath()+"record/"+userID+"/tp"+topic+"/";
File dstFile = new File(fileDir);
if (!dstFile.exists()){
dstFile.mkdirs();
}
System.out.println(fileDir);
// 输出图像
ImageIO.write(image, "jpg", new File(fileDir+pageNo+".jpg"));
System.out.println(fileDir);
System.out.println("图片输出结束!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
);
add(panel, BorderLayout.SOUTH);
}
for(int i=1; i<33; i++)
{
final String url = new PropertiesUtils().getPropertyValue("DomainName")+"tp"+topic+".html?page="+i;
final int page = i;
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
frame.getContentPane().add(new ImageUtils(url,780, 460,page,"1","02"), BorderLayout.CENTER);//获取指定url网页快照图片,然后存到相应的目录
frame.setSize(780, 460);
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
}
System.out.println("结束");