视频监控终端

main.cpp
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    if(!w.init())
    {   qDebug() << "init error!";
        return -1;
    }
    w.show();
    w.ShowImage();
    
    return a.exec();
}

ui_widget.h
/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created: Fri May 4 10:03:55 2012
**      by: Qt User Interface Compiler version 4.8.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(400, 300);

        retranslateUi(Widget);

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QImage>
#include <QTimer>
#include <QDebug>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    QImage IplImageToQImage(const IplImage * iplImage,double mini, double maxi);
    bool ShowImage();

    bool init();

protected:
    void paintEvent(QPaintEvent *);
    
private:
    Ui::Widget *ui;
    QImage image;
    QTimer *timer;
    IplImage *pImage;
    CvCapture *pCapture;

public slots:
    void slot_timer_positionImage();

};

#endif // WIDGET_H


moc_widget.cpp

/****************************************************************************
** Meta object code from reading C++ file 'widget.h'
**
** Created: Fri May 4 11:25:04 2012
**      by: The Qt Meta Object Compiler version 63 (Qt 4.8.0)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include "widget.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'widget.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 63
#error "This file was generated using the moc from 4.8.0. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_Widget[] = {

 // content:
       6,       // revision
       0,       // classname
       0,    0, // classinfo
       1,   14, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

 // slots: signature, parameters, type, tag, flags
       8,    7,    7,    7, 0x0a,

       0        // eod
};

static const char qt_meta_stringdata_Widget[] = {
    "Widget\0\0slot_timer_positionImage()\0"
};

void Widget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
    if (_c == QMetaObject::InvokeMetaMethod) {
        Q_ASSERT(staticMetaObject.cast(_o));
        Widget *_t = static_cast<Widget *>(_o);
        switch (_id) {
        case 0: _t->slot_timer_positionImage(); break;
        default: ;
        }
    }
    Q_UNUSED(_a);
}

const QMetaObjectExtraData Widget::staticMetaObjectExtraData = {
    0,  qt_static_metacall 
};

const QMetaObject Widget::staticMetaObject = {
    { &QWidget::staticMetaObject, qt_meta_stringdata_Widget,
      qt_meta_data_Widget, &staticMetaObjectExtraData }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &Widget::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *Widget::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *Widget::qt_metacast(const char *_clname)
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_Widget))
        return static_cast<void*>(const_cast< Widget*>(this));
    return QWidget::qt_metacast(_clname);
}

int Widget::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QWidget::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        if (_id < 1)
            qt_static_metacall(this, _c, _id, _a);
        _id -= 1;
    }
    return _id;
}
QT_END_MOC_NAMESPACE

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <QVector>
#include <cstring>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(slot_timer_positionImage()));
    //this->init();

}

Widget::~Widget()
{
    delete ui;
    timer->stop();

    cvReleaseCapture(&pCapture);
    cvReleaseImage(&pImage);
}

QImage Widget::IplImageToQImage(const IplImage * iplImage,double mini, double maxi)
{
    uchar *qImageBuffer = NULL;

    int width = iplImage->width;

/**//* Note here that OpenCV image is stored so that each lined is
32-bits aligned thus
* explaining the necessity to "skip" the few last bytes of each
line of OpenCV image buffer.
*/
    int widthStep = iplImage->widthStep;
    int height = iplImage->height;

    switch (iplImage->depth)
    {
        case IPL_DEPTH_8U:
        if(iplImage->nChannels == 1)
        {
        /**//* OpenCV image is stored with one byte grey pixel. We convert it
        to an 8 bit depth QImage.
        */

            qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
            uchar *QImagePtr = qImageBuffer;
            const uchar *iplImagePtr = (const uchar *) iplImage->imageData;

            for(int y = 0; y < height; y++)
            {
                // Copy line by line
                    memcpy(QImagePtr, iplImagePtr, width);
                    QImagePtr += width;
                    iplImagePtr += widthStep;
            }

        }
        else if(iplImage->nChannels == 3)
        {
                /**//* OpenCV image is stored with 3 byte color pixels (3 channels).
                We convert it to a 32 bit depth QImage.
                */
                qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
                uchar *QImagePtr = qImageBuffer;
                const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
                for(int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                            // We cannot help but copy manually.
                            QImagePtr[0] = iplImagePtr[0];
                            QImagePtr[1] = iplImagePtr[1];
                            QImagePtr[2] = iplImagePtr[2];
                            QImagePtr[3] = 0;

                            QImagePtr += 4;
                            iplImagePtr += 3;
                    }
                    iplImagePtr += widthStep-3*width;
                }

        }
        else
        {
                qDebug("IplImageToQImage: image format is not supported : depth=8U and %d channels ", iplImage->nChannels);
        }
        break;
        case IPL_DEPTH_16U:
        if(iplImage->nChannels == 1)
        {
        /**//* OpenCV image is stored with 2 bytes grey pixel. We convert it
        to an 8 bit depth QImage.
        */
                qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
                uchar *QImagePtr = qImageBuffer;
                //const uint16_t *iplImagePtr = (const uint16_t *);
                const unsigned int *iplImagePtr = (const unsigned int *)iplImage->imageData;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                    // We take only the highest part of the 16 bit value. It is
                    //similar to dividing by 256.
                        *QImagePtr++ = ((*iplImagePtr++) >> 8);
                    }
                    iplImagePtr += widthStep/sizeof(unsigned int)-width;
                }
        }
        else
        {
                qDebug("IplImageToQImage: image format is not supported : depth=16U and %d channels ", iplImage->nChannels);

        }
        break;
        case IPL_DEPTH_32F:
         if(iplImage->nChannels == 1)
         {
        /**//* OpenCV image is stored with float (4 bytes) grey pixel. We
        convert it to an 8 bit depth QImage.
        */
                 qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
                 uchar *QImagePtr = qImageBuffer;
                 const float *iplImagePtr = (const float *) iplImage->imageData;
                 for(int y = 0; y < height; y++)
                 {
                     for(int x = 0; x < width; x++)
                     {
                             uchar p;
                             float pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);
                             if(pf < 0) p = 0;
                             else if(pf > 255) p = 255;
                             else p = (uchar) pf;

                             *QImagePtr++ = p;
                      }
                     iplImagePtr += widthStep/sizeof(float)-width;
                 }
         }
         else
         {
                 qDebug("IplImageToQImage: image format is not supported : depth=32F and %d channels ", iplImage->nChannels);
         }
           break;
           case IPL_DEPTH_64F:
         if(iplImage->nChannels == 1)
         {
            /**//* OpenCV image is stored with double (8 bytes) grey pixel. We
            convert it to an 8 bit depth QImage.
            */
                qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
                uchar *QImagePtr = qImageBuffer;
                const double *iplImagePtr = (const double *) iplImage->imageData;
                for(int y = 0; y < height; y++)
                {
                    for(int x = 0; x < width; x++)
                    {
                            uchar p;
                            double pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);

                            if(pf < 0) p = 0;
                            else if(pf > 255) p = 255;
                            else p = (uchar) pf;

                            *QImagePtr++ = p;
                    }
                    iplImagePtr += widthStep/sizeof(double)-width;
                }
        }
        else
        {
                qDebug("IplImageToQImage: image format is not supported : depth=64F and %d channels ", iplImage->nChannels);
        }
        break;
        default:
            qDebug("IplImageToQImage: image format is not supported : depth=%d and %d channels ", iplImage->depth, iplImage->nChannels);
    }

    QImage qImage;
    QVector<QRgb> vcolorTable;
    if(iplImage->nChannels == 1)
    {
       // We should check who is going to destroy this allocation.
        QRgb *colorTable = new QRgb[256];
        for(int i = 0; i < 256; i++)
        {
               colorTable[i] = qRgb(i, i, i);
               vcolorTable[i] = colorTable[i];
        }
        qImage = QImage(qImageBuffer, width, height, QImage::Format_Indexed8).copy();
        qImage.setColorTable(vcolorTable);
    }
    else
    {
        qImage = QImage(qImageBuffer, width, height, QImage::Format_RGB32).copy();
    }
    free(qImageBuffer);
    return qImage;
}

bool Widget::init()
{
    pImage = NULL;
    pCapture = NULL;
    qDebug()<<"before cvCaptureFromCAM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    if((pCapture = cvCaptureFromCAM(0)) == NULL)
    {
        return false;
    }
    qDebug()<<"after cvCaptureFromCAM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    return true;
}

void Widget::slot_timer_positionImage()
{
    qDebug()<<"read camera data!!!!!!!!!!!!!!!!!!!!!!!!!";
    if((pImage = cvQueryFrame(pCapture)) != NULL)
    {
        qDebug()<<"read pImage data!!!!!!!!!!!!!!!!!!!!!!!!!!";
        cvCvtColor(pImage, pImage, CV_BGR2RGB);
         image = QImage((uchar *)pImage->imageData, pImage->width, pImage->height, pImage->widthStep, QImage::Format_RGB888);
        //image = IplImageToQImage(pImage, 100, 1000);
        update();
    }
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawImage(QRect(0,0, 1920, 1080), image);
    qDebug()<<"paintEvent!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}

bool Widget::ShowImage()
{
    timer->start(1000 / 24); // / 24
    return true;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值