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 |
|
---|
16 | /* using mingw to set binary mode */
|
---|
17 | #ifdef WIN32
|
---|
18 | #include <io.h>
|
---|
19 | #include <fcntl.h>
|
---|
20 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
21 | #else
|
---|
22 | #define BINARYSTDOUT
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | class Save : public QObject
|
---|
26 | {
|
---|
27 | Q_OBJECT
|
---|
28 | public:
|
---|
29 | Save(QWebPage *p) : page(p) {};
|
---|
30 |
|
---|
31 | public slots:
|
---|
32 | void loaded(bool ok)
|
---|
33 | {
|
---|
34 | if(ok)
|
---|
35 | {
|
---|
36 | page->setViewportSize(page->mainFrame()->contentsSize());
|
---|
37 | QImage im(page->viewportSize(), QImage::Format_ARGB32);
|
---|
38 | QPainter painter(&im);
|
---|
39 | page->mainFrame()->render(&painter);
|
---|
40 |
|
---|
41 | QFile f;
|
---|
42 | BINARYSTDOUT
|
---|
43 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
44 | {
|
---|
45 | if(!im.save(&f, "JPEG"))
|
---|
46 | {
|
---|
47 | im.save(&f, "PNG");
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | emit finish();
|
---|
52 | }
|
---|
53 | signals:
|
---|
54 | void finish(void);
|
---|
55 |
|
---|
56 | private:
|
---|
57 | QWebPage * page;
|
---|
58 | };
|
---|
59 |
|
---|
60 | #include "webkit-image.h"
|
---|
61 |
|
---|
62 | int main(int argc, char **argv)
|
---|
63 | {
|
---|
64 | if(argc != 2)
|
---|
65 | return 20;
|
---|
66 | QString url = QString(argv[1]);
|
---|
67 |
|
---|
68 | QApplication a(argc, argv);
|
---|
69 | QWebPage * page = new QWebPage();
|
---|
70 | Save * s = new Save(page);
|
---|
71 |
|
---|
72 | QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
73 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
74 | /* set some useful defaults for a webpage */
|
---|
75 | // page->setViewportSize(QSize(1280,1024));
|
---|
76 | // page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
|
---|
77 | // page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
|
---|
78 | page->mainFrame()->load (QUrl(url));
|
---|
79 | return a.exec();
|
---|
80 | }
|
---|