[QT]抄—影像显示实验

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

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

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

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

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

Main中的代码:
 #include "mainwindow.h"
#include <QApplication>
#include <QtGui/QLabel>
#include <QtCore> int main(int argc, char *argv[])
{
QApplication app(argc, argv); QTextCodec *codec=QTextCodec::codecForName("GBK");
if(codec==NULL)
codec=QTextCodec::codecForLocale(); QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForCStrings(codec); QTranslator translator( );
translator.load( "qt_zh_CN.qm", "." );
app.installTranslator( &translator ); QFont font;
font.setFamily(QString::fromUtf8("\345\256\213\344\275\223"));
font.setPointSize();
app.setFont(font); CImageDisplayDlg MainWindow;
app.setActiveWindow(&MainWindow);
MainWindow.show(); QString myStylesheet = "QDialog,QMainWindow,QSplitter\
{background-color: qlineargradient(x1:,y1:,x2:,y2:,stop: rgb(,,) stop: rgb(,,));}\
QMenuBar{background-color: rgb(,,);}";
app.setStyleSheet(myStylesheet); return app.exec();
}

main

窗体头文件
 /***************************************************************************
*
* Time: 2012-05-28
* Project: 遥感图像显示Demo
* Purpose: 遥感图像显示Demo
* Author: 李民录
* Copyright (c) 2011, liminlu0314@163.com
* Describe:遥感图像显示Demo
*
****************************************************************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include "ui_MainWindow.h"
#include <string>
using namespace std; class GDALDataset; QT_BEGIN_NAMESPACE
class QCheckBox;
class QComboBox;
class QLabel;
class QSpinBox;
QT_END_NAMESPACE class CImageDisplayDlg :
public QDialog,
private Ui::ImageDisplayDlg
{
Q_OBJECT public:
CImageDisplayDlg(QWidget* parent = );
~CImageDisplayDlg(void); private:
bool CheckInputData(const char* pszRaster, const char* pszVector, QString& strMsg); private slots:
void LoadInputRaster();
void soltApply();
void OnOK(); private:
bool ShowRasterFile();
void ShowRaster();
private:
string m_strInputRaster;
GDALDataset *m_pDataset;
double m_dScale;
int m_iMinx;
int m_iMiny;
int m_iMaxx;
int m_iMaxy;
}; #endif /*MAINWINDOW_H*/

CImageDisplayDlg.h

窗体实现

 /***************************************************************************
*
* Time: 2012-05-28
* Project: 遥感图像显示Demo
* Purpose: 遥感图像显示Demo
* Author: 李民录
* Copyright (c) 2011, liminlu0314@163.com
* Describe:遥感图像显示Demo
*
****************************************************************************/
#include "MainWindow.h" #include <QFileDialog>
#include <QMessageBox>
#include <QSettings> #include "gdal_priv.h" CImageDisplayDlg::CImageDisplayDlg(QWidget* parent)
: QDialog( parent )
{
setupUi(this); QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(OnOK()));
QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QObject::connect(pushButtonInput, SIGNAL(clicked()), this, SLOT(LoadInputRaster()));
QObject::connect(pushButtonApply, SIGNAL(clicked()), this, SLOT(soltApply())); labelView->clear();
m_pDataset = NULL;
} CImageDisplayDlg::~CImageDisplayDlg(void)
{
if(m_pDataset != NULL)
{
GDALClose((GDALDatasetH) m_pDataset);
m_pDataset = NULL;
}
} bool CImageDisplayDlg::ShowRasterFile()
{
GDALAllRegister();
m_pDataset = (GDALDataset*)GDALOpen(m_strInputRaster.c_str(), GA_ReadOnly);
if (m_pDataset == NULL)
{
QMessageBox::information(this,tr("提示"),tr("指定的文件不能打开!"));
return false;
}
//设置投影坐标范围
int dataWidth = m_pDataset->GetRasterXSize();
int dataHeight = m_pDataset->GetRasterYSize(); double padfTransform[] = {0.0};
m_pDataset->GetGeoTransform(padfTransform); //设置行列号范围
spinStartCol->setRange(, dataWidth-);
spinStartRow->setRange(, dataHeight-);
spinEndCol->setRange(, dataWidth-);
spinEndRow->setRange(, dataHeight-); spinStartCol->setValue();
spinStartRow->setValue();
spinEndCol->setValue(dataWidth-);
spinEndRow->setValue(dataHeight-); ShowRaster();
return true;
} void CImageDisplayDlg::LoadInputRaster()
{
QFileDialog::Options options;
QString selectedFilter;
QString fileName = QFileDialog::getOpenFileName(this,
tr("打开原始图像"),
".",
tr("所有文件(*.*);;Erdas Image (*.img);;PCIDSK (*.pix);;GTiff (*.tif *.tiff);;ENVI (*.*)"),
&selectedFilter,
options); if (!fileName.isEmpty())
{
if(m_pDataset != NULL)
{
GDALClose((GDALDatasetH) m_pDataset);
m_pDataset = NULL;
} lineInput->setText(fileName);
m_strInputRaster = QFile::encodeName( fileName ).constData(); if(!ShowRasterFile())
{
lineInput->setText("");
m_strInputRaster = "";
}
}
} void CImageDisplayDlg::ShowRaster()
{
int iStartCol = spinStartCol->value();
int iStartRow = spinStartRow->value(); int dataWidth = spinEndCol->value() - iStartCol;
int dataHeight = spinEndRow->value() - iStartRow;
int dataBands = m_pDataset->GetRasterCount(); int band_list[] = {,,}; m_dScale = dataHeight > dataWidth ? dataHeight : dataWidth;
int iViewHeight = ;
m_dScale = iViewHeight/m_dScale; int iSize = GDALGetDataTypeSize(GDT_Byte) / ;
int iScaleWidth = static_cast<int>(dataWidth*m_dScale+0.5);
int iScaleHeight = static_cast<int>(dataHeight*m_dScale+0.5); iScaleWidth = (iScaleWidth*+)/*; unsigned char* pBuffer = new unsigned char[iScaleWidth*iScaleHeight*dataBands];
CPLErr err = m_pDataset->RasterIO(GF_Read, , , dataWidth, dataHeight, pBuffer, iScaleWidth, iScaleHeight,
GDT_Byte, dataBands, band_list, iSize*dataBands, iSize*iScaleWidth*dataBands, iSize);
unsigned char* pDataBuffer = NULL;
if (dataBands >= )
{
pDataBuffer = pBuffer;
}
else
{
pDataBuffer = new unsigned char[iScaleWidth*iScaleHeight*];
for (int i=; i<iScaleWidth*iScaleHeight*; i++)
pDataBuffer[i] = pBuffer[i/]; delete []pBuffer;
} QImage QImg(pDataBuffer, iScaleWidth, iScaleHeight, QImage::Format_RGB888);
QPixmap pixmap = QPixmap::fromImage(QImg);
delete []pDataBuffer; labelView->setPixmap(pixmap);
} void CImageDisplayDlg::OnOK()
{
accept();
} void CImageDisplayDlg::soltApply()
{
ShowRaster();
}

实现文件

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

[QT]抄—影像显示实验

上一篇:Android 自定义View


下一篇:js----DOM对象(3