根据URL生成网页缩略图 给定一个URL(网址),要求得到该网页的一个缩略图.请问如何实现啊??或者给一个思路也行,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.websnapr.com/ 先申请一个key 然后在你要用的缩略图时,将下面的size,key和url换成你的就好。 http://images.websnapr.com/?size=size&key=key&url=url size=T (92x70 pixels) size=S (202x152 pixels) size=M (400x300 pixels) size=L (640x480 pixels) 例如<img src="http://images.websnapr.com/?size=M&key=key&url=www.baidu.com"> 左面应用的话,通过winsock之类的远程取回图片即可。 ------ 其实直接使用http://images.websnapr.com/?size=t&url=www.163.com就可以了,图片会被打上一行字。 请问下你要js端截屏的还是java段啊 ?js的话就需要个ocx控件来调用截屏java的话倒是有个import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.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 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组件保存指定网页截图"); // 加载google,最大保存为640x480的截图 frame.getContentPane().add( new Main("http://www.hao123.com/", 640, 480), BorderLayout.CENTER); frame.setSize(800, 600); // 仅初始化,但不显示 frame.invalidate(); frame.pack(); frame.setVisible(false); } }); NativeInterface.runEventPump(); }} java的 我也是网上找的js的比较难实现截屏,做“场景再现”的时候经常需要截图。但是截屏ocx控件网上有公司可以买 还要加几个lib包DJNativeSwing.jarDJNativeSwing-SWT.jarswt-3.6M3-win32-win32-x86.jar这三个就可以了 奇怪的log4j 不能顺利登录处理,报空指针错误 谁能帮我解释下这句话啊 谢谢 真让人费解的一个疑问关于ftpClient的问题 求web service之间传输数据的实例! j2ee怎么入手 系统完成,如何如何打包配置到tomcat 请问各位,有谁知道 ERP 中的 现场绩效系统 请问sun有没有提供一个直接能把整个xml文件中的所有元素都读出的方法???? struts2 上传文件 大小错误 小弟脚本盲 请教一个问题 简单的js图片轮显出错
先申请一个key
然后在你要用的缩略图时,将下面的size,key和url换成你的就好。
http://images.websnapr.com/?size=size&key=key&url=url size=T (92x70 pixels)
size=S (202x152 pixels)
size=M (400x300 pixels)
size=L (640x480 pixels)
例如<img src="http://images.websnapr.com/?size=M&key=key&url=www.baidu.com"> 左面应用的话,通过winsock之类的远程取回图片即可。 ------
其实直接使用http://images.websnapr.com/?size=t&url=www.163.com就可以了,图片会被打上一行字。
js的话就需要个ocx控件来调用截屏
java的话倒是有个import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.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 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组件保存指定网页截图");
// 加载google,最大保存为640x480的截图
frame.getContentPane().add(
new Main("http://www.hao123.com/", 640, 480),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}}
js的比较难实现截屏,做“场景再现”的时候经常需要截图。但是截屏ocx控件网上有公司可以买
DJNativeSwing.jar
DJNativeSwing-SWT.jar
swt-3.6M3-win32-win32-x86.jar这三个就可以了