Files
CodeRepository/App_ThermalImageSystem/Guide/App_UVS12035A/GeneralThermalImageSystem/FileOperator.cpp
chenzhen 222dda1e43 1,新增“App_ThermalImageSystem”;
2,新增“Apps”;
3,新增“Common”;
4,新增“FileList”;
5,新增“MediaX”;
6,新增“OpenSource”;
7,新增“Samples”;
8,新增“SoftwareBusinessLines”.
2026-02-14 23:03:23 +08:00

289 lines
6.1 KiB
C++

#include "FileOperator.h"
#include "CommSerialDef.h"
#include <QFileDialog>
#include <QTextStream>
//#define MAX_UP_SIZE 20*1024*1024
FileOperator::FileOperator()
{
m_bufAll = new uchar[MAX_UP_SIZE];
}
FileOperator::~FileOperator()
{
if(m_bufAll)
{
delete[] m_bufAll;
m_bufAll = NULL;
}
}
bool FileOperator::Open(const QString &filePath)
{
bool ret = false;
QFile m_file(filePath);
if (m_file.open(QIODevice::ReadOnly))
{
memset(m_bufAll,0xFF,MAX_UP_SIZE);
m_size = m_file.size();
qint64 nFileSize = m_size;
m_nFrameCount = nFileSize/1024;
int nLast = nFileSize % 1024;
if(nLast > 0) m_nFrameCount += 1;
QByteArray by;
by = m_file.readAll();
char* p = by.data();
memcpy(m_bufAll,p,m_size);
// uchar pp[8];
// memcpy(pp, m_bufAll-24,8);
// m_iFileNo = m_bufAll[1];
long long checkAll = 0;
for(int i=0; i<m_size; ++i)
{
checkAll += m_bufAll[i];
}
m_FileCheckValue[0] = checkAll & 0xFF;
m_FileCheckValue[1] = (checkAll >> 8) & 0xFF;
m_FileCheckValue[2] = (checkAll >> 16) & 0xFF;
m_FileCheckValue[3] = (checkAll >> 24) & 0xFF;
// memcpy(m_FileCheckValue, m_bufAll+8, sizeof(m_FileCheckValue));
ret = true;
m_file.close();
}
return ret;
}
bool FileOperator::OpenUXDatFile(const QString &filePath)
{
bool ret = false;
QFile m_file(filePath);
if (m_file.open(QIODevice::ReadOnly|QIODevice::Text))
{
memset(m_bufAll,0xFF,MAX_UP_SIZE);
int nLineCnt = 0;
QTextStream varTextStream(&m_file);
while (!varTextStream.atEnd())
{
QString strLine = varTextStream.readLine();
bool ok;
int temp = strLine.toInt(&ok,16);
*(m_bufAll + nLineCnt) = temp;
nLineCnt++;
}
if(nLineCnt < 1)
{
ret = false;
return ret;
}
m_size = nLineCnt;
m_nFrameCount = nLineCnt/MAX_FRAME_LENGTH;
int nLast = nLineCnt % MAX_FRAME_LENGTH;
if(nLast > 0)
{
m_nFrameCount += 1;
}
ret = true;
m_file.close();
}
return ret;
}
bool FileOperator::OpenOXDatFile(const QString &bitFilePath)
{
bool ret = false;
QFile m_file(bitFilePath);
if (m_file.open(QIODevice::ReadOnly | QIODevice::Text))
{
memset(m_bufAll,0xFF,MAX_UP_SIZE);
int nLineCnt = 0;
QString strLine;
QTextStream varTextStream(&m_file);
while (!varTextStream.atEnd())
{
strLine = varTextStream.readLine();
strLine.remove(0,2); //去掉0x开头
nLineCnt++;
QByteArray arrBytes;
QByteArray byteIn;
byteIn = strLine.toLatin1();
HexStringToData(byteIn.data(),arrBytes);
*(m_bufAll+(nLineCnt-1)*4) = arrBytes[3];
*(m_bufAll+1+(nLineCnt-1)*4) = arrBytes[2];
*(m_bufAll+2+(nLineCnt-1)*4) = arrBytes[1];
*(m_bufAll+3+(nLineCnt-1)*4) = arrBytes[0];
}
if(nLineCnt<1)
{
return false;
}
m_size = nLineCnt*4;
qint64 nFileSize = m_size;
m_nFrameCount = nFileSize/MAX_FRAME_LENGTH;
int nLast = nFileSize % MAX_FRAME_LENGTH;
if(nLast > 0)
{
m_nFrameCount += 1;
}
ret = true;
m_file.close();
}
return ret;
}
bool FileOperator::OpenBitFile(const QString &filePath)
{
bool ret = false;
QFile m_file(filePath);
if (m_file.open(QIODevice::ReadOnly))
{
memset(m_bufAll,0xFF,MAX_UP_SIZE);
m_size = m_file.size();
qint64 nFileSize = m_size;
m_nFrameCount = nFileSize/MAX_FRAME_LENGTH;
int nLast = nFileSize % MAX_FRAME_LENGTH;
if(nLast > 0) m_nFrameCount += 1;
QByteArray by;
by = m_file.readAll();
char* p = by.data();
memcpy(m_bufAll,p,m_size);
ret = true;
m_file.close();
}
return ret;
}
void FileOperator::GetFlashData(int nFrameIndex, uchar *buf,int &len)
{
if(nFrameIndex>m_nFrameCount || nFrameIndex <= 0)
return;
memcpy(buf,m_bufAll+1024*(nFrameIndex-1),1024);
len = 1024;
if(nFrameIndex == m_nFrameCount)
{
qint64 nFileSize = m_size;
int nLast = nFileSize % 1024;
if(nLast != 0)
len = nLast;
}
}
void FileOperator::GetConvertData(int nFrameIndex, uchar *buf,int &len)
{
if(!m_bufAll)
{
return;
}
if(nFrameIndex>m_nFrameCount)
return;
len = MAX_FRAME_LENGTH;
if(nFrameIndex == m_nFrameCount)
{
qint64 nFileSize = m_size;
int nLast = nFileSize % MAX_FRAME_LENGTH;
if(nLast != 0)
len = nLast;
}
memcpy(buf,m_bufAll+MAX_FRAME_LENGTH*(nFrameIndex-1),len);
}
bool FileOperator::WriteDataToFile(const QString &savePath)
{
bool bRes = false;
if(!m_bufAll)
{
return false;
}
QFile file(savePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
file.write((char*)m_bufAll,m_size);
file.close();
bRes = true;
}
else
{
bRes = false;
}
return bRes;
}
int FileOperator::HexStringToData(const char* szStr,QByteArray& arr)
{
QString szHex = QString(szStr);
int nLen = szHex.length();
for (int i=0;i<nLen;)
{
char low,high;
high = szStr[i];
if (high == ' ')
{
i++;
continue;
}
i++;
if (i>=nLen)
break;
low = szStr[i];
int nHigh = HexChar(high);
int nLow = HexChar(low);
if ((nHigh==16)||(nLow==16))
break;
else
nHigh = nHigh*16+nLow;
i++;
arr.append((BYTE)nHigh);
}
return arr.size();
}
int FileOperator::HexChar(char c)
{
if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else
return 0x10;
}