1.检测文件路径,如果文件不存在则创建路径
a.
void ProcessFile::CheckDirectory(CString path)
{
//if (!PathFileExists(path))
{
ProcessFile::createMultiDir(path);
}
}
b.
bool ProcessFile::createMultiDir(const wchar_t* path)
{
if (path == NULL) return false;
const wchar_t* pwcStrrchr = wcstrrchr(path,L'\\');
if (!pwcStrrchr) return false;
//检查指定的目录是否存在
if (PathIsDirectory(path)) return true;
wchar_t wsSubPath[MAX_PATH] = {};
memset(wsSubPath,0,sizeof(wsSubPath));
for (int i=0; i<pwcStrrchr-path; i++)
wsSubPath[i] = *(path+i);
createMultiDir(wsSubPath);
if(CreateDirectory(path,NULL)) return true;
return false;
}
c.
void ProcessFile::CreateMultiDirectory(string path)
{
string::size_type nPos = 2;
CFileFind finder;
while ((nPos = path.find("\\", nPos)) != string::npos)
{
string sSubPath = path.substr(0, nPos);
if (!finder.FindFile((CString)sSubPath.c_str()))
{
BOOL bCreate = CreateDirectory((CString)sSubPath.c_str(), NULL);
int nn;
nn = 0;
}
nPos++;
}
}
2.按日期创建路径
//按照日期创建路径
CString ProcessFile::CreateTimeDirectory()
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString fname;
fname.Format(_T("D:\\ISO\\ISO%04d%02d%02d%02d\\"),sytm.wYear,sytm.wMonth,sytm.wDay,sytm.wHour/*,sytm.wMinute*/);
WIN32_FIND_DATA fd;
HANDLE hfind=::FindFirstFile(fname,&fd);
while(hfind!=INVALID_HANDLE_VALUE)
{
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
return NULL;
}
CreateDirectory(fname,NULL);
return fname;
}
3.解析txt文件
源代码:
ProcessFile.h
#pragma once
#include <iostream>
#include <bitset>
using namespace std;
//图
typedef struct tagOCTDATA
{
BYTE _pbyOCTDataBuffer[2000 * 1024];
}OCTDATA;
//线
typedef struct tagOCTSEGEMENT
{
double _fOCTSegement[4 * 2000];
}OCTSEGEMENT;
class ProcessFile
{
public:
ProcessFile(void);
~ProcessFile(void);
public:
//1.
static DWORD __stdcall ReadBinaryFile(BYTE * pBuf, DWORD size, LPCTSTR lpszFileName);
static DWORD __stdcall WriteBinaryFile(BYTE * pBuf, DWORD size, LPCTSTR lpszFileName);
static void LoadImageFromFile(int** m_ppImgData, CString imgPath, int m_imgWidth, int m_imgHeight);
static void LoadImageFromFile_Long(long** m_ppImgData, CString imgPath, int m_imgWidth, int m_imgHeight);
//按照日期创建路径
static CString CreateOneDirectory(CString fname);
static CString CreateTimeDirectory();
static CString CreateXYZTmDirectory();
static CString ZAixsCreateTmDirectory();
static CString GetOtherOnePath(CString curPath, int mNum);
//保存虹膜图象**.BMP
static BOOL SaveBitmapToFile(BYTE *pBuffer, int nColorBits, CString m_szFileName, int nWidth, int nHeight);
//保存OCT图为*.bin文件
static void SaveCurOCT(BYTE * octData, int octnum, int nWidth, int nHeight, int nRealWidth);
static void SaveCurOCTemp(BYTE ** octData, int octnum, int nWidth, int nHeight);
static void SaveCurIris(PUSHORT pBuffer, int irisNum, int num, int width, int height);
static void SaveCurOCT(int octnum, int num);
static void WriteToFile(CString filename, BYTE *pBuff, int nsize);
//检测路径是否存在,如果不存在则创建路径
static void CheckDirectory(CString path);
static bool createMultiDir(const wchar_t* path);
static void CreateMultiDirectory(string path);
static const wchar_t* wcstrrchr(const wchar_t* str, const wchar_t wc);
static int ParseFileGetEye(string path);
};
ProcessFile.cpp
#include "StdAfx.h"
#include "ProcessFile.h"
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
using namespace std;
int g_Wd = 860;
int g_Ht = 500;
ProcessFile::ProcessFile(void)
{
}
ProcessFile::~ProcessFile(void)
{
}
DWORD __stdcall ProcessFile::ReadBinaryFile(BYTE * pBuf, DWORD size, LPCTSTR lpszFileName)
{
DWORD realRead = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
__try {
hFile = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
ReadFile(hFile, pBuf, size, &realRead, NULL);
}
}
__finally {
CloseHandle(hFile); hFile = NULL;
}
return realRead;
}
DWORD __stdcall ProcessFile::WriteBinaryFile(BYTE * pBuf, DWORD size, LPCTSTR lpszFileName)
{
DWORD realWrite = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
__try {
hFile = CreateFile(lpszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
WriteFile(hFile, pBuf, size, &realWrite, NULL);
}
}
__finally {
CloseHandle(hFile); hFile = NULL;
}
return realWrite;
}
//按照日期创建路径
CString ProcessFile::CreateTimeDirectory()
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString fname;
fname.Format(_T("D:\\ISO\\ISO%04d%02d%02d%02d\\"), sytm.wYear, sytm.wMonth, sytm.wDay, sytm.wHour/*,sytm.wMinute*/);
WIN32_FIND_DATA fd;
HANDLE hfind = ::FindFirstFile(fname, &fd);
while (hfind != INVALID_HANDLE_VALUE)
{
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
return NULL;
}
CreateDirectory(fname, NULL);
return fname;
}
//按照日期创建路径
CString ProcessFile::CreateXYZTmDirectory()
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString fname;
fname.Format(_T("D:\\XYZISO\\XYZISO%04d%02d%02d%02d\\"), sytm.wYear, sytm.wMonth, sytm.wDay, sytm.wHour/*,sytm.wMinute*/);
WIN32_FIND_DATA fd;
HANDLE hfind = ::FindFirstFile(fname, &fd);
while (hfind != INVALID_HANDLE_VALUE)
{
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
return NULL;
}
CreateDirectory(fname, NULL);
return fname;
}
//按照日期创建路径
CString ProcessFile::ZAixsCreateTmDirectory()
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString fname;
fname.Format(_T("D:\\ZISO\\ZISO%04d%02d%02d%02d\\"), sytm.wYear, sytm.wMonth, sytm.wDay, sytm.wHour/*,sytm.wMinute*/);
WIN32_FIND_DATA fd;
HANDLE hfind = ::FindFirstFile(fname, &fd);
while (hfind != INVALID_HANDLE_VALUE)
{
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
return NULL;
}
CreateDirectory(fname, NULL);
return fname;
}
void ProcessFile::SaveCurIris(PUSHORT pBuffer, int irisNum, int num, int width, int height)
{
CString paths;
paths.Format(L"D:\\REF\\iris");
if (!PathFileExists(paths))
{
createMultiDir(paths);
}
//灰度图转换为彩色图
BYTE *pdata = new BYTE[width * height * 3];
int k = 0;
if (pBuffer != NULL)
{
for (int i = 0; i<width * height; i++)
{
pdata[k] = (BYTE)pBuffer[i];
pdata[k + 1] = (BYTE)pBuffer[i];
pdata[k + 2] = (BYTE)pBuffer[i];
k += 3;
}
}
CString m_szFileName;
//octnum++;
m_szFileName.Format(_T("\\iris%d_%d.BMP"), irisNum, num);
m_szFileName = paths + m_szFileName;
SaveBitmapToFile(pdata, 24, m_szFileName, width, height);
delete pdata;
pdata = NULL;
}
//保存虹膜图像,**.BMP
BOOL ProcessFile::SaveBitmapToFile(BYTE *pBuffer, int nColorBits, CString m_szFileName, int nWidth, int nHeight)
{
HANDLE hf = CreateFile(
m_szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, NULL, NULL);
if (hf == INVALID_HANDLE_VALUE) return 0;
int lBufferSize = nWidth * nHeight * (nColorBits / 8);
// 写??文?件t头a?¤
BITMAPFILEHEADER bfh;
memset(&bfh, 0, sizeof(bfh));
bfh.bfType = 'MB';
bfh.bfSize = sizeof(bfh) + lBufferSize + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
DWORD dwWritten = 0;
WriteFile(hf, &bfh, sizeof(bfh), &dwWritten, NULL);
// 写??位?图a?格?式o?
BITMAPINFOHEADER bih;
memset(&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = nWidth;
bih.biHeight = nHeight;
bih.biPlanes = 1;
bih.biBitCount = nColorBits;
WriteFile(hf, &bih, sizeof(bih), &dwWritten, NULL);
// 写??位?图a?数oy据Y
WriteFile(hf, pBuffer, lBufferSize, &dwWritten, NULL);
CloseHandle(hf);
return 1;
}
void ProcessFile::SaveCurOCT(int octnum, int num)
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString paths;
paths.Format(L"D:\\REF\\oct");
//paths.Format(L"D:\\REF\\oct\\oct%02d%02d%02d%02d\\",sytm.wMonth,sytm.wDay,sytm.wHour,sytm.wMinute);
if (!PathFileExists(paths))
{
createMultiDir(paths);
}
//保存图片
int nRealWidth = g_Wd;
int nWidth = nRealWidth /*- UPCUTPOINT - DOWNCUTPOINT*/;//860
int nHeight = g_Ht/* - DOWNCUTLINE*/;//线数
PBYTE buff = (PBYTE)malloc(nHeight * nWidth * 3);
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
//buff[j*nWidth + i] = g_OCTData[j * nRealWidth + i];
buff[j*nWidth + i] = i*j;
}
}
CString m_szFileName;
m_szFileName.Format(_T("\\oct%d_%d.dat"), octnum, num);
m_szFileName = paths + m_szFileName;
WriteToFile(m_szFileName, buff, nWidth * nHeight);
free(buff);
}
void ProcessFile::SaveCurOCTemp(BYTE ** octData, int octnum, int nWidth, int nHeight)
{
SYSTEMTIME sytm;
GetLocalTime(&sytm);
CString paths;
//paths = _T("D:\\OCT\\oct");
paths.Format(L"D:\\OCT\\temp\\temp%02d%02d%02d%02d", sytm.wMonth, sytm.wDay, sytm.wHour, sytm.wMinute);
//CString paths;
//paths = _T("D:\\OCT\\temp");
if (!PathFileExists(paths))
{
CreateDirectory(paths, NULL);
//CLog::GetInstance()->WriteLog(_T("CreateDirectory(paths, NULL)"));
}
//保存图片
//int nRealWidth = g_Wd;
//int nWidth = nRealWidth - UPCUTPOINT - DOWNCUTPOINT;//860
//int nHeight = g_Ht - DOWNCUTLINE;//线数
PBYTE buff = (PBYTE)malloc(nHeight * nWidth * 3);
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
//buff[j*nWidth + i] = g_OCTData[j * nRealWidth + i];
//buff[j*nWidth + i+1] = g_OCTData[j * nRealWidth + i];
//buff[j*nWidth + i+2] = g_OCTData[j * nRealWidth + i];
//buff[j*nWidth + i] = octData[j * nWidth + i];
buff[j*nWidth + i] = octData[j][i];
}
}
CString m_szFileName;
//octnum++;
m_szFileName.Format(_T("\\octTemp%d.dat"), octnum);
m_szFileName = paths + m_szFileName;
//CLog::GetInstance()->WriteLog(_T("SaveOCTToFilen,Width:%d,nHeight:%d"),nWidth,nHeight);
//CUtilityObject::GetInstance()->tmFile.WriteToFile(m_szFileName, buff, nWidth * nHeight);
WriteToFile(m_szFileName, buff, nWidth * nHeight);
free(buff);
}
/************************************************************************
* 功能描述: 将缓存数据写入文件
* 输入参数:filename: 文件名(含绝对路径),pBuff:数据缓存,
nCount:数据块大小
* 输出参数:
* 返回参数:
* 其它说明:
************************************************************************/
void ProcessFile::WriteToFile(CString filename, BYTE *pBuff, int nsize)
{
FILE *fp;
if (0 == _wfopen_s(&fp, filename, _T("wb+")))
{
fwrite(pBuff, nsize, 1, fp);
fclose(fp);
}
}
const wchar_t* ProcessFile::wcstrrchr(const wchar_t* str, const wchar_t wc)
{
const wchar_t* pwc = NULL;
for (int i = wcslen(str) - 1; i >= 0; i--)
{
if (str[i] == wc)
{
pwc = str + i;
break;
}
}
return pwc;
}
void ProcessFile::CreateMultiDirectory(string path)
{
string::size_type nPos = 2;
CFileFind finder;
while ((nPos = path.find("\\", nPos)) != string::npos)
{
string sSubPath = path.substr(0, nPos);
if (!finder.FindFile((CString)sSubPath.c_str()))
{
BOOL bCreate = CreateDirectory((CString)sSubPath.c_str(), NULL);
int nn;
nn = 0;
}
nPos++;
}
}
bool ProcessFile::createMultiDir(const wchar_t* path)
{
if (path == NULL) return false;
const wchar_t* pwcStrrchr = wcstrrchr(path, L'\\');
if (!pwcStrrchr) return false;
//检查指定的目录是否存在
if (PathIsDirectory(path)) return true;
wchar_t wsSubPath[MAX_PATH] = {};
memset(wsSubPath, 0, sizeof(wsSubPath));
for (int i = 0; i<pwcStrrchr - path; i++)
wsSubPath[i] = *(path + i);
createMultiDir(wsSubPath);
if (CreateDirectory(path, NULL)) return true;
return false;
}
CString ProcessFile::GetOtherOnePath(CString curPath, int mNum)
{
int pos = curPath.Find(_T("."));
CString tm;
tm.Format(L"%03d", mNum);
CString befStr, afterStr;
befStr = curPath.Mid(0, pos - 3);
afterStr = curPath.Mid(pos, curPath.GetLength() - pos);
CString myPath;
myPath = befStr + tm + afterStr;
return myPath;
}
void ProcessFile::LoadImageFromFile(int** m_ppImgData, CString imgPath, int m_imgWidth, int m_imgHeight)
{
CFileFind findFile;
if (!findFile.FindFile(imgPath))
{
//CLog::GetInstance()->WriteLog(_T("can not find the file of the Image!"));
//AfxMessageBox(_T(""));
return;
}
OCTDATA * pOctdata = new OCTDATA;
memset(pOctdata, 0, sizeof(OCTDATA));
DWORD dwReadBytes = ProcessFile::ReadBinaryFile(&(pOctdata->_pbyOCTDataBuffer[0]),
m_imgWidth * m_imgHeight, imgPath);
if (m_imgWidth * m_imgHeight != dwReadBytes)
{
//AfxMessageBox(_T("fail to read the Data of the Image!"));
//CLog::GetInstance()->WriteLog(_T("fail to read the Data of the Image!"));
return; //读取图形数据失败
}
for (int i = 0; i< m_imgWidth; ++i)
{
for (int j = 0; j< m_imgHeight; ++j)
{
m_ppImgData[i][j] = (int)(pOctdata->_pbyOCTDataBuffer[i * m_imgHeight + j]);
}
}
//CLog::GetInstance()->WriteLog(_T("m_ppImgData[i][j]:%d,i:%d,j:%d"),m_ppImgData[m_imgWidth-1][m_imgHeight-1],m_imgWidth,m_imgHeight);
delete pOctdata;
pOctdata = NULL;
}
void ProcessFile::LoadImageFromFile_Long(long** m_ppImgData, CString imgPath, int m_imgWidth, int m_imgHeight)
{
CFileFind findFile;
if (!findFile.FindFile(imgPath))
{
//CLog::GetInstance()->WriteLog(_T("can not find the file of the Image!"));
//AfxMessageBox(_T(""));
return;
}
OCTDATA * pOctdata = new OCTDATA;
memset(pOctdata, 0, sizeof(OCTDATA));
DWORD dwReadBytes = ProcessFile::ReadBinaryFile(&(pOctdata->_pbyOCTDataBuffer[0]),
m_imgWidth * m_imgHeight, imgPath);
if (m_imgWidth * m_imgHeight != dwReadBytes)
{
//AfxMessageBox(_T("fail to read the Data of the Image!"));
//CLog::GetInstance()->WriteLog(_T("fail to read the Data of the Image!"));
return; //读取图形数据失败
}
for (int i = 0; i< m_imgWidth; ++i)
{
for (int j = 0; j< m_imgHeight; ++j)
{
m_ppImgData[i][j] = (long)(pOctdata->_pbyOCTDataBuffer[i * m_imgHeight + j]);
}
}
//CLog::GetInstance()->WriteLog(_T("m_ppImgData[i][j]:%d,i:%d,j:%d"),m_ppImgData[m_imgWidth-1][m_imgHeight-1],m_imgWidth,m_imgHeight);
delete pOctdata;
pOctdata = NULL;
}
int ProcessFile::ParseFileGetEye(string path)
{
//读取txt文件
int eye = -1;
ifstream file;
file.open(path, ios::in);
if (!file)
{
cout << "open fail!" << endl;
return eye;
}
int pos;
string strTemp;
list<string> strBuffer;
while (!file.eof())
{
char buffer[4096];
//一次读4096个字节
file.read(buffer, 4096);
strTemp = buffer;
pos = strTemp.find("烫");
strTemp = strTemp.substr(0, pos);
strBuffer.push_back(strTemp);
}
file.close();
string meyeStr;
//解析字符串
while (!strBuffer.empty())
{
string strTm;
strTm = strBuffer.front();
strBuffer.pop_front();
int pos = strTm.find("\n");
while (pos >= 0)
{
string sline = strTm.substr(0, pos);
strTm = strTm.substr(pos + 1, sline.length() - pos - 1);
int lpo;
lpo = sline.find("=");
string picres = sline.substr(0, lpo);
if (picres == "ScanEye")
{
meyeStr = sline.substr(lpo + 1, sline.length() - lpo - 1);
break;
}
pos = strTm.find("\n");
}
}
if (meyeStr == "R")
{
eye = 0;
}
else if (meyeStr == "L")
{
eye = 1;
}
return eye;
}
void ProcessFile::CheckDirectory(CString path)
{
//if (!PathFileExists(path))
{
ProcessFile::createMultiDir(path);
}
}
CString ProcessFile::CreateOneDirectory(CString fname)
{
WIN32_FIND_DATA fd;
HANDLE hfind = ::FindFirstFile(fname, &fd);
while (hfind != INVALID_HANDLE_VALUE)
{
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
return NULL;
}
CreateDirectory(fname, NULL);
return fname;
}