zoukankan      html  css  js  c++  java
  • [QT]抄—影像显示实验

    QtCreator新建一个Qt Application,命名为ImageView

    在项目文件夹下添加gdal库,统一放在ImageViewgdal目录下。

    右键单击项目,选择添加库命令,添加gdal_i.lib和头文件路径,完成后可以再ImageView.pro文件中查看:

    win32: LIBS += -L$$PWD/gdal/lib/ -lgdal_i

    INCLUDEPATH += $$PWD/gdal/include
    DEPENDPATH += $$PWD/gdal/include

    Main中的代码:
     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 #include <QtGui/QLabel>
     4 #include <QtCore>
     5 
     6 int main(int argc, char *argv[])
     7 {
     8     QApplication app(argc, argv);
     9 
    10         QTextCodec *codec=QTextCodec::codecForName("GBK");
    11         if(codec==NULL)
    12             codec=QTextCodec::codecForLocale();
    13 
    14         QTextCodec::setCodecForLocale(codec);
    15         QTextCodec::setCodecForTr(codec);
    16         QTextCodec::setCodecForCStrings(codec);
    17 
    18         QTranslator translator( 0 );
    19         translator.load( "qt_zh_CN.qm", "." );
    20         app.installTranslator( &translator );
    21 
    22         QFont font;
    23         font.setFamily(QString::fromUtf8("345256213344275223"));
    24         font.setPointSize(9);
    25         app.setFont(font);
    26 
    27         CImageDisplayDlg MainWindow;
    28         app.setActiveWindow(&MainWindow);
    29         MainWindow.show();
    30 
    31         QString myStylesheet = "QDialog,QMainWindow,QSplitter
    32                                {background-color: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 rgb(193,219,255) stop:1 rgb(227,239,255));}
    33                                QMenuBar{background-color: rgb(193,219,255);}";
    34         app.setStyleSheet(myStylesheet);
    35 
    36         return app.exec();
    37 }
    main
    窗体头文件
     1 /***************************************************************************
     2 *
     3 * Time: 2012-05-28
     4 * Project: 遥感图像显示Demo
     5 * Purpose: 遥感图像显示Demo
     6 * Author:  李民录
     7 * Copyright (c) 2011, liminlu0314@163.com
     8 * Describe:遥感图像显示Demo
     9 *
    10 ****************************************************************************/
    11 #ifndef MAINWINDOW_H
    12 #define MAINWINDOW_H
    13 
    14 #include "ui_MainWindow.h"
    15 #include <string>
    16 using namespace std;
    17 
    18 class GDALDataset;
    19 
    20 QT_BEGIN_NAMESPACE
    21 class QCheckBox;
    22 class QComboBox;
    23 class QLabel;
    24 class QSpinBox;
    25 QT_END_NAMESPACE
    26 
    27 class CImageDisplayDlg :
    28     public QDialog,
    29     private Ui::ImageDisplayDlg
    30 {
    31     Q_OBJECT
    32 
    33 public:
    34     CImageDisplayDlg(QWidget* parent = 0);
    35     ~CImageDisplayDlg(void);
    36 
    37 private:
    38     bool CheckInputData(const char* pszRaster, const char* pszVector, QString& strMsg);
    39 
    40     private slots:
    41         void LoadInputRaster();
    42         void soltApply();
    43         void OnOK();
    44 
    45 private:
    46     bool ShowRasterFile();
    47     void ShowRaster();
    48 private:
    49     string m_strInputRaster;
    50     GDALDataset *m_pDataset;
    51     double m_dScale;
    52     int m_iMinx;
    53     int m_iMiny;
    54     int m_iMaxx;
    55     int m_iMaxy;
    56 };
    57 
    58 #endif /*MAINWINDOW_H*/
    CImageDisplayDlg.h

      窗体实现

      1 /***************************************************************************
      2 *
      3 * Time: 2012-05-28
      4 * Project: 遥感图像显示Demo
      5 * Purpose: 遥感图像显示Demo
      6 * Author:  李民录
      7 * Copyright (c) 2011, liminlu0314@163.com
      8 * Describe:遥感图像显示Demo
      9 *
     10 ****************************************************************************/
     11 #include "MainWindow.h"
     12 
     13 #include <QFileDialog>
     14 #include <QMessageBox>
     15 #include <QSettings>
     16 
     17 #include "gdal_priv.h"
     18 
     19 CImageDisplayDlg::CImageDisplayDlg(QWidget* parent)
     20 : QDialog( parent )
     21 {
     22     setupUi(this);
     23 
     24     QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(OnOK()));
     25     QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
     26     QObject::connect(pushButtonInput, SIGNAL(clicked()), this, SLOT(LoadInputRaster()));
     27     QObject::connect(pushButtonApply, SIGNAL(clicked()), this, SLOT(soltApply()));
     28 
     29     labelView->clear();
     30     m_pDataset = NULL;
     31 }
     32 
     33 CImageDisplayDlg::~CImageDisplayDlg(void)
     34 {
     35     if(m_pDataset != NULL)
     36     {
     37         GDALClose((GDALDatasetH) m_pDataset);
     38         m_pDataset = NULL;
     39     }
     40 }
     41 
     42 bool CImageDisplayDlg::ShowRasterFile()
     43 {
     44     GDALAllRegister();
     45     m_pDataset = (GDALDataset*)GDALOpen(m_strInputRaster.c_str(), GA_ReadOnly);
     46     if (m_pDataset == NULL)
     47     {
     48         QMessageBox::information(this,tr("提示"),tr("指定的文件不能打开!"));
     49         return false;
     50     }
     51     //设置投影坐标范围
     52     int    dataWidth = m_pDataset->GetRasterXSize();
     53     int dataHeight = m_pDataset->GetRasterYSize();
     54 
     55     double padfTransform[6] = {0.0};
     56     m_pDataset->GetGeoTransform(padfTransform);
     57 
     58     //设置行列号范围
     59     spinStartCol->setRange(0, dataWidth-1);
     60     spinStartRow->setRange(0, dataHeight-1);
     61     spinEndCol->setRange(0, dataWidth-1);
     62     spinEndRow->setRange(0, dataHeight-1);
     63 
     64     spinStartCol->setValue(0);
     65     spinStartRow->setValue(0);
     66     spinEndCol->setValue(dataWidth-1);
     67     spinEndRow->setValue(dataHeight-1);
     68 
     69     ShowRaster();
     70     return true;
     71 }
     72 
     73 void CImageDisplayDlg::LoadInputRaster()
     74 {
     75     QFileDialog::Options options;
     76     QString selectedFilter;
     77     QString fileName = QFileDialog::getOpenFileName(this,
     78         tr("打开原始图像"),
     79         ".",
     80         tr("所有文件(*.*);;Erdas Image (*.img);;PCIDSK (*.pix);;GTiff (*.tif *.tiff);;ENVI (*.*)"),
     81         &selectedFilter,
     82         options);
     83 
     84     if (!fileName.isEmpty())
     85     {
     86         if(m_pDataset != NULL)
     87         {
     88             GDALClose((GDALDatasetH) m_pDataset);
     89             m_pDataset = NULL;
     90         }
     91 
     92         lineInput->setText(fileName);
     93         m_strInputRaster = QFile::encodeName( fileName ).constData();
     94 
     95         if(!ShowRasterFile())
     96         {
     97             lineInput->setText("");
     98             m_strInputRaster = "";
     99         }
    100     }
    101 }
    102 
    103 void CImageDisplayDlg::ShowRaster()
    104 {
    105     int iStartCol = spinStartCol->value();
    106     int iStartRow = spinStartRow->value();
    107 
    108     int dataWidth = spinEndCol->value() - iStartCol;
    109     int dataHeight = spinEndRow->value() - iStartRow;
    110     int    dataBands =  m_pDataset->GetRasterCount();
    111 
    112     int    band_list[3] = {1,2,3};
    113 
    114     m_dScale = dataHeight > dataWidth ? dataHeight : dataWidth;
    115     int iViewHeight = 541;
    116     m_dScale = iViewHeight/m_dScale;
    117 
    118     int iSize = GDALGetDataTypeSize(GDT_Byte) / 8;
    119     int iScaleWidth = static_cast<int>(dataWidth*m_dScale+0.5);
    120     int iScaleHeight = static_cast<int>(dataHeight*m_dScale+0.5);
    121 
    122     iScaleWidth = (iScaleWidth*8+31)/32*4;
    123 
    124     unsigned char* pBuffer = new unsigned char[iScaleWidth*iScaleHeight*dataBands];
    125     CPLErr err = m_pDataset->RasterIO(GF_Read, 0, 0, dataWidth, dataHeight, pBuffer, iScaleWidth, iScaleHeight,
    126         GDT_Byte, dataBands, band_list, iSize*dataBands, iSize*iScaleWidth*dataBands, iSize);
    127     unsigned char* pDataBuffer = NULL;
    128     if (dataBands >=3 )
    129     {
    130         pDataBuffer = pBuffer;
    131     }
    132     else
    133     {
    134         pDataBuffer = new unsigned char[iScaleWidth*iScaleHeight*3];
    135         for (int i=0; i<iScaleWidth*iScaleHeight*3; i++)
    136             pDataBuffer[i] = pBuffer[i/3];
    137 
    138         delete []pBuffer;
    139     }
    140 
    141     QImage QImg(pDataBuffer, iScaleWidth, iScaleHeight, QImage::Format_RGB888);
    142     QPixmap pixmap = QPixmap::fromImage(QImg);
    143     delete []pDataBuffer;
    144 
    145     labelView->setPixmap(pixmap);
    146 }
    147 
    148 void CImageDisplayDlg::OnOK()
    149 {
    150     accept();
    151 }
    152 
    153 void CImageDisplayDlg::soltApply()
    154 {
    155     ShowRaster();
    156 }
    实现文件

    编译运行需要将GDAl所有的DLL拷贝到输出目录下。
    结果如图:

     

     

    
    
  • 相关阅读:
    元素的高度自适应
    关于IE6的一些常见的CSS BUG处理
    Vue项目在IE浏览器报错polyfilleventsource added missing EventSource to window
    Springboot使用JdbcTemplate RowMapper查询,直接返回实体列表
    Springboot启动工程后,浏览器出现输入用户名和密码
    mysql5.6 zip版本如何安装
    python基础基础知识介绍
    python基础数据类型,集合及深浅copy
    格式化输出
    python基础windows环境下 安装python2和python3
  • 原文地址:https://www.cnblogs.com/yhlx125/p/3806782.html
Copyright © 2011-2022 走看看