我想使用qt jambi來製作截圖。我使用集成的webkit瀏覽器,它像一個魅力。問題是:qtjambi - 主要方法之外的QApplication.initialize
我怎麼能初始化
QApplication.initialize(參數);
以外的主要方法。由於我想在不調用外部程序的情況下從我的Java Web應用程序中截取屏幕截圖,所以我需要在主外部初始化QApplication。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.trolltech.qt.core.QObject;
import com.trolltech.qt.core.QUrl;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.webkit.QWebPage;
import com.trolltech.qt.webkit.QWebView;
/**
*
* @author Marc Giombetti Created on: 16.04.2010
*/
public class ScreenshotWithoutWindow {
private QWebView browser;
/**
* @param args
*/
public static void main(String[] args) {
if (args.length > 2) {
String file = args[0];
String baseUrl = args[1];
String writeTo = args[2];
Thumbnailer thumbnail = new Thumbnailer(file, baseUrl, writeTo);
} else {
System.out.println("Please provide file, baseURL and savetoPath parameters");
System.out
.println("For example:java -jar ScreenshotWithoutWindow C:\\test\\test.htm file:///C:/Marc/test/ c:\\screenshot\\screenshot-new.png");
}
}
public static class Thumbnailer extends QObject {
private QWebPage page = new QWebPage();
public Signal0 finished = new Signal0();
private static String file;
private static String baseUrl;
private static String writeTo;
public Thumbnailer(String _file, String _baseUrl, String _writeTo) {
file = _file;
baseUrl = _baseUrl;
writeTo = _writeTo;
initialize();
}
private void initialize(){
QApplication.initialize(null);
page.loadFinished.connect(this, "render(Boolean)");
try {
page.mainFrame().setHtml(readFileAsString(file), new QUrl(baseUrl));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finished.connect(QApplication.instance(), "quit()");
QApplication.exec();
}
void render(Boolean b) {
page.setViewportSize(page.mainFrame().contentsSize());
QImage image = new QImage(page.viewportSize(), QImage.Format.Format_ARGB32);
QPainter painter = new QPainter(image);
page.mainFrame().render(painter);
painter.end();
image.save(writeTo);
System.out.println("Saved screenshot as "+writeTo);
finished.emit();
}
private static String readFileAsString(String filePath) throws java.io.IOException {
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
}
}
這不起作用,應用程序崩潰,一個致命的錯誤:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x670ddc30, pid=4588, tid=2776
#
# JRE version: 6.0_17-b04
# Java VM: Java HotSpot(TM) Client VM (14.3-b01 mixed mode windows-x86)
# Problematic frame:
# C [QtCore4.dll+0xddc30]
有沒有人有一個想法如何初始化的主要方法之外的QApplication。