QT如何将RAW原始格式图像进行显示

本项目中使用QT进行开发采集CMOS芯片中的原始图像数据.RAW格式图像进行显示(16位无符号灰度图显示)

首先注意QT的5.13版本以后才支持16为无符号灰度图显示(老版本只有8位无符号灰度图显示,看个人需求了)。

.cpp

#include "QTShowPic.h"
#include "mainwindow.h"

#include <QGuiApplication>
#include <QTimer>
#include <QFile>
#include <QObject>




QTShowPic::QTShowPic(/*mainwindow *mainWindow,*/QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	ui.horizontalLayout->setContentsMargins(0, 0, 0, 0);
    //未加载时显示:暂无图片
	m_imgPicSrc = QImage(QString::fromLocal8Bit(":/icon/暂无图片.png")); 
	m_imgPicSrc2 = QImage(QString::fromLocal8Bit(":/icon/暂无图片.png"));
	//初始化自动采图时间间隔
    delaytime = 10000;
	m_index = 0;

	// 可以在需要的时候调用主界面的方法获取下拉框索引
	/*int index = getMainComboBoxIndex();*/

	socketRcv1 = new QUdpSocket(this);
	socketRcv1->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 20 * 1024 * 1024);//设置缓冲区大小(怀疑缓冲区泄露)

	connect(socketRcv1, &QUdpSocket::readyRead, this, &QTShowPic::recvdata1);

	socketRcv2 = new QUdpSocket(this);
	connect(socketRcv2, &QUdpSocket::readyRead, this, &QTShowPic::recvdata2);

	// 在需要的时候调用子界面的方法获取下拉框索引
	//int index = mainWindow->getMainComboBoxIndex();

	socketRcvAll = new QUdpSocket(this);
	connect(socketRcvAll, &QUdpSocket::readyRead, this, &QTShowPic::RecvTakeImages);

	socketRcvAll2 = new QUdpSocket(this);
	connect(socketRcvAll2, &QUdpSocket::readyRead, this, &QTShowPic::RecvTakeImages2);
	
	/*ui.label_Current->setFrameShape(QFrame::Box);
	ui.label_Current->setStyleSheet("border-width: 3px; border-style: solid; border-color: rgb(255, 170, 0); background-color: rgb(197, 211, 255);");
	ui.label_Voltage->setFrameShape(QFrame::Box);
	ui.label_Voltage->setStyleSheet("border-width: 3px; border-style: solid; border-color: rgb(255, 170, 0); background-color: rgb(197, 211, 255);");*/
	
	bool bRet2 = (bool)connect(ui.LoadImg_2, &QPushButton::clicked, this, &QTShowPic::onOpenPic2);
	bool bRet = (bool)connect(ui.LoadImg,&QPushButton::clicked,this,&QTShowPic::onOpenPic);

}


//调整拉伸时像素变化
void QTShowPic::resizeEvent(QResizeEvent *event)
{
	//拉伸变化时由于RAW格式图片大小的固定所以无法对其进行拉申自适应,其他格式正常
	m_imgPic2Show = m_imgPicSrc.scaled(ui.label_ImageShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
	m_imgPic2Show2 = m_imgPicSrc2.scaled(ui.label_ImageShow2->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

//初始化图片大小
void QTShowPic::showEvent(QShowEvent * event)
{
	m_imgPic2Show = m_imgPicSrc.scaled(ui.label_ImageShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
	m_imgPic2Show2 = m_imgPicSrc2.scaled(ui.label_ImageShow2->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

//选择图片显示1
void QTShowPic::onOpenPic()
{
	QString strImagePath = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/", tr("Image Files (*.bmp *.png *.jpg *.svg *.raw)"));
	if (strImagePath.isEmpty())
	{
		return;
	}

	// 检查文件是否为RAW格式
	if (strImagePath.endsWith(".raw", Qt::CaseInsensitive))
	{
		// 处理RAW图像
		// 这里需要你知道RAW图像的具体参数,例如宽度、高度和像素格式
		int width = 2048; // 示例宽度
		int height = 2048; // 示例高度
		QImage::Format format = QImage::Format_Grayscale16; // 假设是16位RGB图像

		QFile file(strImagePath);
		if (file.open(QIODevice::ReadOnly))
		{
			QByteArray rawData = file.readAll();
	
			QImage image(reinterpret_cast<uchar*>(rawData.data()), width, height, format);

			// 将QImage转换为QPixmap并显示
			QPixmap pixmap = QPixmap::fromImage(image);
		
			ui.label_ImageShow->setPixmap(pixmap.scaled(ui.label_ImageShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
		}
	}
	else
	{
		bool bRet = m_imgPicSrc.load(strImagePath);

		if (!bRet)
		{
			return;
		}
		// 对于非RAW格式的图像,正常显示
		m_imgPic2Show = m_imgPicSrc.scaled(ui.label_ImageShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
		m_pixpic2show = QPixmap::fromImage(m_imgPic2Show);
		ui.label_ImageShow->setPixmap(m_pixpic2show);
	}

}

//选择图片显示2(未平滑处理)
void QTShowPic::onOpenPic2()
{
	QString strImagePath2 = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/", tr("Image Files (*.bmp *.png *.jpg *.svg *.raw)"));
	if (strImagePath2.isEmpty())
	{
		return;
	}

	// 检查文件是否为RAW格式
	if (strImagePath2.endsWith(".raw", Qt::CaseInsensitive))
	{
		// 处理RAW图像
		// 这里需要你知道RAW图像的具体参数,例如宽度、高度和像素格式
		int width = 2048; // 示例宽度
		int height = 2048; // 示例高度
		QImage::Format format = QImage::Format_Grayscale16; // 假设是16位RGB图像

		QFile file(strImagePath2);
		if (file.open(QIODevice::ReadOnly))
		{
			QByteArray rawData = file.readAll();
			QImage image(reinterpret_cast<uchar*>(rawData.data()), width, height, format);

			// 将QImage转换为QPixmap并显示
			QPixmap pixmap = QPixmap::fromImage(image);
			ui.label_ImageShow2->setPixmap(pixmap.scaled(ui.label_ImageShow2->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

		}
	}
	else
	{
		bool bRet = m_imgPicSrc2.load(strImagePath2);

		if (!bRet)
		{
			return;
		}
		// 对于非RAW格式的图像,正常显示
		m_imgPic2Show2 = m_imgPicSrc2.scaled(ui.label_ImageShow2->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
		m_pixpic2show2 = QPixmap::fromImage(m_imgPic2Show2);
		ui.label_ImageShow2->setPixmap(m_pixpic2show2);
	}
}

.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QTShowPic.h"


class mainwindow;

class QTShowPic : public QMainWindow
{
	Q_OBJECT

public:
	QTShowPic(/*mainwindow *mainWindow,*/QWidget *parent = Q_NULLPTR);


public slots:

    void QTShowPic::onOpenPic();
	void QTShowPic::onOpenPic2();
	

protected:

	virtual void resizeEvent(QResizeEvent *event);

	virtual void showEvent(QShowEvent * event);

signals:

private:

	mainwindow *mainWindow;

	Ui::QTShowPicClass ui;

	QImage image;

	QImage m_imgPicSrc;//为增强显示效果保存原始图片
	QImage m_imgPic2Show;
	QPixmap m_pixpic2show;


	QImage m_imgPicSrc2;
	QImage m_imgPic2Show2;
	QPixmap m_pixpic2show2;



};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值