1 | /* compile with
|
---|
2 | moc webkit-image.cpp >webkit-image.h
|
---|
3 | g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include <QtGui/QApplication>
|
---|
7 | #include <QtCore/QFile>
|
---|
8 | #include <QtCore/QString>
|
---|
9 | #include <QtCore/QDebug>
|
---|
10 | #include <QtWebKit/QWebView>
|
---|
11 |
|
---|
12 | /* using mingw to set binary mode */
|
---|
13 | #ifdef WIN32
|
---|
14 | #include <io.h>
|
---|
15 | #include <fcntl.h>
|
---|
16 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
17 | #else
|
---|
18 | #define BINARYSTDOUT
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #define WIDTH 2000
|
---|
22 |
|
---|
23 | class Save : public QObject
|
---|
24 | {
|
---|
25 | Q_OBJECT
|
---|
26 | public:
|
---|
27 | Save(QWebView *v) : view(v) {};
|
---|
28 |
|
---|
29 | public slots:
|
---|
30 | void loaded(bool ok)
|
---|
31 | {
|
---|
32 | if(ok)
|
---|
33 | {
|
---|
34 | QImage im = QPixmap::grabWidget(view).toImage();
|
---|
35 | QRgb white = QColor(255,255,255).rgb();
|
---|
36 |
|
---|
37 | /* didn't find a way to reduce image directly, so we scan for white background */
|
---|
38 | bool iswhite = true;
|
---|
39 | int xsize = WIDTH;
|
---|
40 | int ysize = WIDTH;
|
---|
41 | for(int x = xsize-1; iswhite && x > 0; --x)
|
---|
42 | {
|
---|
43 | for(int y = 0; iswhite && y < ysize; ++y)
|
---|
44 | {
|
---|
45 | QRgb p = im.pixel(x, y);
|
---|
46 | if(p != white && p)
|
---|
47 | iswhite = false;
|
---|
48 | }
|
---|
49 | if(iswhite)
|
---|
50 | xsize = x;
|
---|
51 | }
|
---|
52 | iswhite = true;
|
---|
53 | for(int y = ysize-1; iswhite && y > 0; --y)
|
---|
54 | {
|
---|
55 | for(int x = 0; iswhite && x < xsize; ++x)
|
---|
56 | {
|
---|
57 | QRgb p = im.pixel(x, y);
|
---|
58 | if(p != white && p)
|
---|
59 | iswhite = false;
|
---|
60 | }
|
---|
61 | if(iswhite)
|
---|
62 | ysize = y;
|
---|
63 | }
|
---|
64 | /* didn't find a way to clip the QImage directly, so we reload it */
|
---|
65 | QPixmap p = QPixmap::grabWidget(view, 0,0,xsize,ysize);
|
---|
66 | QFile f;
|
---|
67 | BINARYSTDOUT
|
---|
68 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
69 | p.save(&f, "JPG");
|
---|
70 | }
|
---|
71 | emit finish();
|
---|
72 | }
|
---|
73 | signals:
|
---|
74 | void finish(void);
|
---|
75 |
|
---|
76 | private:
|
---|
77 | QWebView *view;
|
---|
78 | };
|
---|
79 |
|
---|
80 | #include "webkit-image.h"
|
---|
81 |
|
---|
82 | int main(int argc, char **argv)
|
---|
83 | {
|
---|
84 | if(argc != 2)
|
---|
85 | return 20;
|
---|
86 | QString url = QString(argv[1]);
|
---|
87 |
|
---|
88 | QApplication a( argc, argv );
|
---|
89 | QWebView *view = new QWebView();
|
---|
90 | Save *s = new Save(view);
|
---|
91 |
|
---|
92 | QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
93 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
94 | view->resize(WIDTH,WIDTH);
|
---|
95 | view->load(QUrl(url));
|
---|
96 | return a.exec();
|
---|
97 | }
|
---|