java后台生成截图-(DJNativeSwing版本)
2016-08-10 14:25
411 查看
根据url生成网页截图。最近在网上看到一个使用DJnatvieSwing版本测试了可以使用。
需要使用的pom依赖有以下3个(第三个是64位操作系统需要添加。)
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing-SWT</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>。
代码如下
/**
* 功能:根据URL能截取整个网页的缩略图(保存为一个图片)
* @author
*
*/
public class ScreenshotUtils 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) {")
7fe0
.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;");
}
//DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载
public ScreenshotUtils(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);
}
public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
// 加载指定页面,最大保存为640x480的截图
frame.getContentPane().add(
new ScreenshotUtils("http://127.0.0.1:8080/public/article/detail/view?id=8a290217559ba2650155b988ac8725ed", 200, 550),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}
}
需要使用的pom依赖有以下3个(第三个是64位操作系统需要添加。)
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing-SWT</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>。
代码如下
/**
* 功能:根据URL能截取整个网页的缩略图(保存为一个图片)
* @author
*
*/
public class ScreenshotUtils 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) {")
7fe0
.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;");
}
//DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载
public ScreenshotUtils(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);
}
public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
// 加载指定页面,最大保存为640x480的截图
frame.getContentPane().add(
new ScreenshotUtils("http://127.0.0.1:8080/public/article/detail/view?id=8a290217559ba2650155b988ac8725ed", 200, 550),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}
}
相关文章推荐
- java swing根据url生成截图
- 通过文件结构直接生成xls文件的VB.Net和Java两个版本
- 按概率生成随机数Java版本
- Java根据word模板生成word文档之后台解析和实现及部分代码(三)A
- Java根据word模板生成word文档之后台解析和实现及部分代码(三)F
- Java根据word模板生成word文档之后台解析和实现及部分代码(三)E
- UML工具推荐 Poseidon版本 可以免费下载,自动生成代码 C++ Java C# VB.NET等 (来自德国汉堡的国际性建模软件开发商Gentleware AG的)
- Java根据word模板生成word文档之后台解析和实现及部分代码(一)
- theboboy原创 随机生成器 生成伪数据 用来做测试 Java版本
- JAVA-SWING:生成透明JTable
- 报表源代码 winform c# -基于DexExpressXtraReport的打印报表,后台生成报表,winform版本,采用C#语言封装,比金质通更强大,现在开放源代码
- 报表源代码 winform c# -基于DexExpressXtraReport的打印报表,后台生成报表,winform版本,采用C#语言封装,比金质通更强大,现在开放源代码 .
- 报表源代码 winform c# -基于DexExpressXtraReport的打印报表,后台生成报表,winform版本,采用C#语言封装,比金质通更强大,现在开放源代码 .
- Java根据word模板生成word文档之后台解析和实现及部分代码(二)
- 视频上传过程中自动转换为flv格式并截图生成缩略图(Java调用命令实现)
- AXIS1版本 WSDL生成JAVA客户端
- 编译环境与生成环境的JDK版本不一样,报:java.util.zip.ZipException: error in opening zip file
- Java根据word模板生成word文档之后台解析和实现及部分代码(三)C
- JAVA-SWING:生成透明JTable的改进1
- 微博短链接的生成算法(Java 版本)