返回介绍

Display Images in Qt-supported Formats

发布于 2019-10-04 15:03:25 字数 3969 浏览 953 评论 0 收藏 0

This example displays images which are in any format supported by Qt. Combining this with the Qt ImageIO Extension adds PNG image support to your browser. It demonstrates the use of the QNPInstance::streamAsFile() function.

To build the example, you must first build the Qt Netscape Plugin Extension library. Then type make in extensions/nsplugin/examples/qtimage/ and copy the resulting qtimage.so or npqtimage.dll to the Plugins directory of your web browser.

This plugin displays a PNG format image.

New image formats can be supported by adding new image I/O handlers to the Qt library (see QImage::defineIOHandler), thus providing the functionality in both applications and WWW plugins from the same code.

This plugin supports transparency and scaling of the image, just like GIFs in most web browsers.

Implementation:

// Qt stuff
#include "qnp.h"
#include <qpainter.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include "qpngio.h"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

class ImageView : public QNPWidget {
public:
    ImageView()
    {
        popup = new QPopupMenu;
        popup->insertItem("Left as");
        popup->insertItem("An exercise");
        popup->insertItem("For the");
        popup->insertItem("Reader!");
    }

    void paintEvent(QPaintEvent* event)
    {
        QPainter p(this);
        p.setClipRect(event->rect());

        if ( pm.size() == size() ) {
            p.drawPixmap(0,0,pm);
        } else {
            if ( pmScaled.size() != size() ) {
                QWMatrix m;
                m.scale((double)width()/pm.width(),
                        (double)height()/pm.height());
                pmScaled = pm.xForm(m);
            }
            p.drawPixmap(0,0,pmScaled);
        }
    }

    void mousePressEvent(QMouseEvent* e)
    {
        popup->popup(mapToGlobal(e->pos()));
    }

    void showImage(const QImage& image)
    {
        pm.convertFromImage(image, QPixmap::Color);
        repaint( FALSE );
    }

private:
    QPixmap pm;
    QPixmap pmScaled;
    QPopupMenu* popup;
};

class ImageLoader : public QNPInstance {
    ImageView* iv;
    QImage image;

public:
    ImageLoader() : iv(0)
    {
    }

    QNPWidget* newWindow()
    {
        iv = new ImageView;
        imageToIV();
        return iv;
    }

    void imageToIV()
    {
        if (!iv || image.isNull()) return;

        iv->showImage(image);
        image.reset();
    }

    bool newStreamCreated(QNPStream*, StreamMode& smode)
    {
        smode = AsFileOnly;
        return TRUE;
    }

    void streamAsFile(QNPStream*, const char* fname)
    {
        //qInitPngIO();

        image = QImage(fname);
        if ( image.isNull() )
            fprintf(stderr, "Could not convert file: %s\n", fname);
        imageToIV();
    }
};

class ImagePlugin : public QNPlugin {

public:
    ImagePlugin()
    {
    }

    QNPInstance* newInstance()
    {
        return new ImageLoader;
    }

    const char* getMIMEDescription() const
    {
        return "image/x-png:png:PNG Image;"
               "image/png:png:PNG Image;"
               "image/x-portable-bitmap:pbm:PBM Image;"
               "image/x-portable-graymap:pgm:PGM Image;"
               "image/x-portable-pixmap:ppm:PPM Image;"
               "image/bmp:bmp:BMP Image;"
               "image/x-ms-bmp:bmp:BMP Image;"
               "image/x-xpixmap:xpm:XPM Image;"
               "image/xpm:xpm:XPM Image";
    }

    const char * getPluginNameString() const
    {
        return "Qt-based Image Plugin";
    }

    const char * getPluginDescriptionString() const
    {
        return "Supports all image formats supported by Qt";
    }
};

QNPlugin* QNPlugin::create()
{
    return new ImagePlugin;
}

See also LiveConnect Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文