需要eclipse的swt.jar
public class HtmlToImageUtil {
private Display display;
private Shell shell;
private Browser browser;
private boolean completed = false;
static final ArrayList urls = new ArrayList();
static boolean canRemove = false;
static void initialize(final Display display, Browser browser) {
browser.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
System.out.println("event.time:" + event.time);
}
});
browser.addOpenWindowListener(new OpenWindowListener() {
public void open(WindowEvent event) {
// Embed the new window
final Shell shell = new Shell(display);
shell.setText("New Window");
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.NONE);
initialize(display, browser);
event.browser = browser;
event.display.asyncExec(new Runnable() {
public void run() {
String url = browser.getUrl();
System.out.println(url);
System.out.println(browser.getText());
if (urls.contains(url)){
//flag to chek if the window is closed automatic
canRemove = false;
shell.close();
}
else{
canRemove = true;
urls.add(url);
}
}
});
// System.out.println("open...............");
}
});
browser.addVisibilityWindowListener(new VisibilityWindowListener() {
public void hide(WindowEvent event) {
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
// System.out.println("hide................");
}
public void show(WindowEvent event) {
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
if (event.location != null)
shell.setLocation(event.location);
if (event.size != null) {
Point size = event.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
if (event.addressBar || event.menuBar || event.statusBar
|| event.toolBar) {
// Create widgets for the address bar, menu bar, status bar and/or tool bar
// leave enough space in the Shell to accomodate a Browser of the size
// given by event.size
}
shell.open();
//System.out.println("show.......................");
}
});
browser.addDisposeListener(new DisposeListener(){
public void widgetDisposed(DisposeEvent event){
Browser browser = (Browser) event.widget;
if (canRemove)
urls.remove(browser.getUrl());
Shell shell = browser.getShell();
// shell.close();
// System.out.println("dispose....................");
}
});
}
public HtmlToImageUtil(){
}
/**
* 将html地址的内容转换成图片
* @param htmlUrl html的地址
* @param imgUrl 生成图片的地址
* @param xDifference 生成图片在原来的基础上的x轴方向的减量
* @param yDifference 生成图片在原来的基础上的y轴方向的减量
* @param imageWidth 生成图片的宽度
* @param imageHeight 生成图片的高度
*/
public HtmlToImageUtil(String htmlUrl,String imgUrl,int xDifference,int yDifference,int imageWidth,int imageHeight)
{
display = new Display();
shell = new Shell(display);
shell.setVisible(false);
shell.setLayout(new FillLayout());
browser = new Browser(shell, SWT.EMBEDDED | SWT.NO_FOCUS | SWT.NO_REDRAW_RESIZE );
browser.refresh();
browser.clearSessions();
browser.setUrl(htmlUrl);
initialize(display, browser);
//shell.open();
browser.addProgressListener(new ProgressListener(){
public void changed(ProgressEvent progressevent) {
// TODO Auto-generated method stub
//System.out.println("change.........");
}
public void completed(ProgressEvent progressevent) {
// TODO Auto-generated method stub
completed=true;
//System.out.println("complete........");
}
});
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
//display.update();
if(completed){
capture(imgUrl, xDifference, yDifference, imageWidth, imageHeight);
completed = false;
try {
Thread.sleep(50);
display.dispose();//注销display
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else{
//display.dispose();
}
}
display.dispose();
}
/**
* 将html的内容转换成图片
* @param htmlText html的内容
* @param imgUrl 生成图片的地址
* @param xDifference 生成图片在原来的基础上的x轴方向的减量
* @param yDifference 生成图片在原来的基础上的y轴方向的减量
* @param imageWidth 生成图片的宽度
* @param imageHeight 生成图片的高度
*/
public HtmlToImageUtil(String htmlText,String imgUrl,int xDifference,int yDifference,int imageWidth,int imageHeight,Boolean isUser){
display = new Display();
shell = new Shell(display);
shell.setVisible(false);
shell.setLayout(new FillLayout());
browser = new Browser(shell, SWT.EMBEDDED | SWT.NO_FOCUS | SWT.NO_REDRAW_RESIZE );
browser.refresh();
browser.clearSessions();
browser.setText(htmlText);
initialize(display, browser);
//shell.open();
browser.addProgressListener(new ProgressListener(){
public void changed(ProgressEvent progressevent) {
// TODO Auto-generated method stub
//System.out.println("change.........");
}
public void completed(ProgressEvent progressevent) {
// TODO Auto-generated method stub
completed=true;
//System.out.println("complete........");
}
});
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
//display.update();
if(completed){
capture(imgUrl, xDifference, yDifference, imageWidth, imageHeight);
completed = false;
try {
Thread.sleep(50);
display.dispose();//注销display
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else{
//display.dispose();
}
}
display.dispose();
}
/**
*
* @param imgUrl 生成图片的地址
* @param xDifference 生成图片在原来的基础上的x轴方向的减量
* @param yDifference 生成图片在原来的基础上的y轴方向的减量
*/
private void capture(String imgUrl,int xDifference,int yDifference,int imageWidth,int imageHeight)
{
GC gc = new GC(browser);
//设置默认的宽度
if(BlankUtil.isBlank(imageWidth)){
imageWidth=1050;
}
//设置默认的高度
if(BlankUtil.isBlank(imageWidth)){
imageHeight=450;
}
//始终最前 (50:x轴,50:主轴,1050:图像的x轴的长度,450:图像的y轴的长度)
OS.SetWindowPos(shell.handle , OS.HWND_TOPMOST, 50 , 50 ,imageWidth, imageHeight , SWT.NULL);
shell.setVisible(true);
shell.open();
shell.layout();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point size = browser.getSize();
Image image = new Image(display, size.x-xDifference, size.y-yDifference);
gc.copyArea(image, 0, 0);
shell.setVisible(false);
// BufferedImage bufferedImage=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {image.getImageData()};
imageLoader.save(imgUrl, SWT.IMAGE_JPEG);
gc.dispose();
image.dispose();
//display.dispose();
//System.out.println("captured");
}
public class BrowserProgress implements ProgressListener
{
public void changed(ProgressEvent a_Event)
{
//completed = true;
//System.out.println("changed.......");
}
public void completed(ProgressEvent a_Event)
{
completed = true;
//System.out.println("complete........");
}
}
public static void main(String[] args) {
/*
new HtmlToImageUtil("E:/test.html","D:/test.jpg",18,0,1050,450);
System.out.println("test");*/
//new HtmlToImageUtil("aaa","F:/aa.jpg",18,0,1050,450,true);
//new HtmlToImageUtil("F:/reportWorkSpace/JxlReport/WebContent/test.html","D:/test.jpg",18,0,1050,802);
//new HtmlToImageUtil("D:/jakarta-tomcat-5.0.27/webapps/road/testImage/test.html","D:/test.jpg",18,0,1050,802);
//new HtmlToImageUtil("F:/d.html","D:/test.jpg",18,0,1050,802);
}