Qt Jambi是跨平台的应用程式框架Qt的Java绑定版本。
WebKit 是一个开源的浏览器引擎,Qt Webkit就是Webkit在Qt平台上的封装。
1、下载并安装Qt Jambi http://qt-jambi.org/downloads
2、执行安装目录下的qtjambi.bat
3、将安装目录下的jar加到lib列表中
4、实现类
public class Thumbnail extends QObject {
private QWebPage page;
/**
* @param args
*/
public static void main(String args[]) {
QApplication.initialize(args);
//
// QPushButton hello = new QPushButton("Hello World!");
// hello.resize(120, 40);
// hello.setWindowTitle("Hello World");
// hello.show();
//
Thumbnail h =new Thumbnail();
h.doSnap();
QApplication.exec();
QApplication.quit();
}
private void doSnap()
{
page = new QWebPage();
page.loadFinished.connect(this, "render()");
// String html = "<html><head>"
// + "<base href=\"http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/\" >"
// + "<title>HTML Test</title></head>"
// + "<body><a href=\"dev.html\">local link</a></body></html>";
//
// page.mainFrame().setHtml(html);
page.mainFrame().load(new QUrl("http://www.baidu.com"));
}
private void render( ) {
page.setViewportSize(page.mainFrame().contentsSize());
QImage image = new QImage(page.viewportSize(),
QImage.Format.Format_ARGB32);
QPainter painter = new QPainter(image);
page.mainFrame().render(painter);
// QImage thumbnail = image.scaled(400, 400);
image.save("d:\\thumbnail.png");
}
}
5、将QImage转换为AWT或SWT图片的方法
//TO AWT IMAGE
BufferedImage bufferedImage = new BufferedImage(
image.width(), image.height(), BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < image.width(); ++x) {
for (int y = 0; y < image.height(); ++y) {
bufferedImage.setRGB(x, y, image.pixel(x, y));
}
}
//TO SWT IMAGE
// Create a source ImageData
//PaletteData中mask参数可以根据类型在BufferedImage中找,如本例中为TYPE_INT_ARGB
PaletteData palette = new PaletteData(0x00ff0000,// Red
0x0000ff00,// Green
0x000000ff// Blue
);
ImageData data = new ImageData(image.width(),
image.height(),image.depth(),
palette);
for (int x = 0; x < image.width(); ++x) {
for (int y = 0; y < image.height(); ++y) {
data.setPixel(x, y, image.pixel(x, y));
}
}
ImageLoader l = new ImageLoader();
l.data=new ImageData[]{data};
l.save("d:\\thumbnail1.png", SWT.IMAGE_PNG);