1 | /* compile with
|
---|
2 | moc webkit-image.cpp >webkit-image.h
|
---|
3 | g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui -s -O2
|
---|
4 | or under Windows:
|
---|
5 | g++ webkit-image.cpp -o webkit-image -lQtCore4 -lQtWebKit4 -lQtGui4 -s O2
|
---|
6 | adding the correct directories with -L or -I:
|
---|
7 | -I C:\Progra~1\Qt\include -L C:\Progra~1\Qt\lib
|
---|
8 | */
|
---|
9 | #include <QtGui/QApplication>
|
---|
10 | #include <QtGui/QPainter>
|
---|
11 | #include <QtCore/QFile>
|
---|
12 | #include <QtCore/QString>
|
---|
13 | #include <QtWebKit/QWebPage>
|
---|
14 | #include <QtWebKit/QWebFrame>
|
---|
15 | #include <QtNetwork/QNetworkProxy>
|
---|
16 | #include <QtCore/QProcess>
|
---|
17 |
|
---|
18 | /* using mingw to set binary mode */
|
---|
19 | #ifdef WIN32
|
---|
20 | #include <io.h>
|
---|
21 | #include <fcntl.h>
|
---|
22 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
23 | #else
|
---|
24 | #define BINARYSTDOUT
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | class Save : public QObject
|
---|
28 | {
|
---|
29 | Q_OBJECT
|
---|
30 | public:
|
---|
31 | Save(QWebPage *p) : page(p) {};
|
---|
32 |
|
---|
33 | public slots:
|
---|
34 | void loaded(bool ok)
|
---|
35 | {
|
---|
36 | if(ok)
|
---|
37 | {
|
---|
38 | page->setViewportSize(page->mainFrame()->contentsSize());
|
---|
39 | QImage im(page->viewportSize(), QImage::Format_ARGB32);
|
---|
40 | QPainter painter(&im);
|
---|
41 | page->mainFrame()->render(&painter);
|
---|
42 |
|
---|
43 | QFile f;
|
---|
44 | BINARYSTDOUT
|
---|
45 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
46 | {
|
---|
47 | if(!im.save(&f, "JPEG"))
|
---|
48 | {
|
---|
49 | im.save(&f, "PNG");
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | emit finish();
|
---|
54 | }
|
---|
55 | signals:
|
---|
56 | void finish(void);
|
---|
57 |
|
---|
58 | private:
|
---|
59 | QWebPage * page;
|
---|
60 | };
|
---|
61 |
|
---|
62 | #include "webkit-image.h"
|
---|
63 |
|
---|
64 | int main(int argc, char **argv)
|
---|
65 | {
|
---|
66 | if(argc != 2)
|
---|
67 | return 20;
|
---|
68 | QString url = QString(argv[1]);
|
---|
69 |
|
---|
70 | QApplication a(argc, argv);
|
---|
71 | QWebPage * page = new QWebPage();
|
---|
72 | Save * s = new Save(page);
|
---|
73 |
|
---|
74 | QStringList environment = QProcess::systemEnvironment();
|
---|
75 | int idx = environment.indexOf(QRegExp("http_proxy=.*"));
|
---|
76 | if(idx != -1)
|
---|
77 | {
|
---|
78 | QString proxyHost;
|
---|
79 | int proxyPort = 8080;
|
---|
80 | QStringList tmpList = environment.at(idx).split("=");
|
---|
81 | QStringList host_port = tmpList.at(1).split(":");
|
---|
82 | proxyHost = host_port.at(0);
|
---|
83 | if(host_port.size() == 2)
|
---|
84 | {
|
---|
85 | bool ok;
|
---|
86 | int port = host_port.at(1).toInt(&ok);
|
---|
87 | if(ok)
|
---|
88 | proxyPort = port;
|
---|
89 | }
|
---|
90 |
|
---|
91 | QNetworkProxy proxy;
|
---|
92 | proxy.setType(QNetworkProxy::HttpCachingProxy);
|
---|
93 | proxy.setHostName(proxyHost);
|
---|
94 | proxy.setPort(proxyPort);
|
---|
95 | page->networkAccessManager()->setProxy(proxy);
|
---|
96 | }
|
---|
97 |
|
---|
98 | QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
99 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
100 | /* set some useful defaults for a webpage */
|
---|
101 | // page->setViewportSize(QSize(1280,1024));
|
---|
102 | // page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
|
---|
103 | // page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
|
---|
104 | page->mainFrame()->load (QUrl(url));
|
---|
105 | return a.exec();
|
---|
106 | }
|
---|