he Flying Saucer Project An XML/XHTML/CSS 2.1 Renderer https://xhtmlrenderer.dev.java.net/Rendering to an ImageYou can render from a document directly to an image format of your choice using org.xhtmlrenderer.simple.ImageRenderer. To use ImageRenderer, just call ImageRenderer.renderToImage(url, path, width), or one of the overloaded versions of the method. You must specify either a width or a width and a height for the image if you like; if height is not specified, it's determined based on the content of the document. renderToImage() creates the document, writes it out to the given path, and returns a java.awt.image.BufferedImage which you can further manipulate—for example, scale and re-save or save in multiple image formats. Here's a simple sample rendering the contents of the http://www.w3c.org homepage:String address = "http://www.w3.org/";// render try { BufferedImage buff = null; buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024); } catch (IOException e) { e.printStackTrace(); }That's it. You can use Java's ImageIO class to write the image out in different formats; it supports writing most image formats you would want to use. Using BufferedImage, you can also manipulate (scale, rotate, transform) the images you create.
An XML/XHTML/CSS 2.1 Renderer
https://xhtmlrenderer.dev.java.net/Rendering to an ImageYou can render from a document directly to an image format of your choice using org.xhtmlrenderer.simple.ImageRenderer. To use ImageRenderer, just call ImageRenderer.renderToImage(url, path, width), or one of the overloaded versions of the method. You must specify either a width or a width and a height for the image if you like; if height is not specified, it's determined based on the content of the document. renderToImage() creates the document, writes it out to the given path, and returns a java.awt.image.BufferedImage which you can further manipulate—for example, scale and re-save or save in multiple image formats. Here's a simple sample rendering the contents of the http://www.w3c.org homepage:String address = "http://www.w3.org/";// render
try {
BufferedImage buff = null;
buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024);
} catch (IOException e) {
e.printStackTrace();
}That's it. You can use Java's ImageIO class to write the image out in different formats; it supports writing most image formats you would want to use. Using BufferedImage, you can also manipulate (scale, rotate, transform) the images you create.
我想利用java把IE浏览器或firefox打开的整体网页截图成图片保存!
import com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import chrriis.dj.nativeswing.swtimpl.*;
import chrriis.dj.nativeswing.swtimpl.components.*;
public class WebImage extends JPanel{
private static WebImage webImage=new WebImage();
private WebImage(){};
public static WebImage getWebImage(){
return webImage;
}
/**
*
*/
final static String fileName = System.currentTimeMillis() + ".jpg";
private static final long serialVersionUID = 1L;
// 行分隔符
final static public String LS = System.getProperty("line.separator", " ");
// 文件分割符
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;");
}
//DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载
public WebImage(final String url, final int maxWidth, final int maxHeight,final String contextpath) {
super(new BorderLayout());
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 + 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);
//String contextpath=ServletActionContext.getServletContext().getRealPath("/upload/webimage");
String webimgpath=contextpath+"/"+fileName;
// 当网页超出目标大小时
if (imageSize.width > maxWidth || imageSize.height > maxHeight) {
//截图部分图形
image = image.getSubimage(0, 0, maxWidth, maxHeight);
}
try {
// 输出图像
ImageIO.write(image, "jpg", new File(webimgpath));
reduceImg(webimgpath,webimgpath, 230,144);
addwater(webimgpath,contextpath+"\\shuiyin.jpg");
} catch (Exception ex) {
ex.printStackTrace();
}
// 退出操作
//System.exit(0);
}
}
}
);
add(panel, BorderLayout.SOUTH);
}
/**
* 截图对象
* @return
*/
public String getWebImg(final String website,final String imgcontextpath) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
// 加载指定页面,最大保存为640x480的截图
frame.getContentPane().add(new WebImage(website, 1010, 480,imgcontextpath),BorderLayout.CENTER);
frame.setSize(460, 288);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
NativeInterface
NativeInterface.close();
return fileName;
}
/**
* 缩小图片
* @param imgsrc
* @param imgdist
* @param widthdist
* @param heightdist
*/
public static void reduceImg(String imgsrc, String imgdist, int widthdist,int heightdist) {
try {
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = javax.imageio.ImageIO.read(srcfile); BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
} }
/**
* 添加水印
* @throws Exception
*/
public static void addwater(String imgpath,String wmpath) throws Exception{
//需要添加水印的图片的路径
InputStream is = new FileInputStream(imgpath);
//通过JPEG图象流创建JPEG数据流解码器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解码当前JPEG数据流,返回BufferedImage对象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
//得到画笔对象
Graphics g = buffImg.getGraphics();
//创建你要附加的图象。
//需要添加的图片水印
ImageIcon imgIcon = new ImageIcon(wmpath);
//得到Image对象。
Image img = imgIcon.getImage();
//将小图片绘到大图片上。
//5,300 .表示你的小图片在大图片上的位置。
g.drawImage(img,140,127,null);
//设置颜色。
g.setColor(Color.BLACK);
//最后一个参数用来设置字体的大小
// Font f = new Font("宋体",Font.BOLD,8);
// g.setFont(f);
//10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
/* g.drawString("添加的水印文字(测试)",10,30);*/
g.dispose();
OutputStream os = new FileOutputStream(imgpath);
//创键编码器,用于编码内存中的图象数据。
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(buffImg);
is.close();
os.close();
}
public static void main(String[] args) {
String str=WebImage.getWebImage().getWebImg("http://www.aihuaedu.cn","E:\\apache-tomcat-6.0.20\\webapps\\liuxin\\upload\\webimage\\");
System.out.println(str);
}}
你参考一下,这是基于web的截图,可以缩小图片,可以加水印,还有一些架包,可以qq我:971475760
好像只能用utf-8格式的网页