此處圖片大小爲:width*height
1.將圖像數據加上bmp頭
unsigned char* ProcessFile::GetBmp32(PBYTE pOCTData, int nColorBits, int iWidth, int iHeight)
{
unsigned char* pszBmp32 = NULL;
BITMAPFILEHEADER fileHeader;
fileHeader.bfType = 'MB';
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileHeader.bfSize = fileHeader.bfOffBits + iWidth * iHeight * (nColorBits / 8);
pszBmp32 = new unsigned char[fileHeader.bfSize];
unsigned char * pHead = pszBmp32;
memcpy(pHead, &fileHeader, sizeof(BITMAPFILEHEADER));
pHead += sizeof(BITMAPFILEHEADER);
BITMAPINFOHEADER bih;
memset(&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = iWidth;
bih.biHeight = iHeight;
bih.biPlanes = 1;
bih.biBitCount = nColorBits;
memcpy(pHead, &bih, sizeof(BITMAPINFOHEADER));
pHead += sizeof(BITMAPINFOHEADER);
memcpy(pHead, pOCTData, iWidth * iHeight * (nColorBits / 8));
return pszBmp32;
}
2.轉換爲opencv格式並保存:
int ProcessFile::SaveBitmapToFileEx(BYTE *pBuffer, int nColorBits, CString m_szFileName, int nWidth, int nHeight)
{
unsigned char * pszBmp32 = GetBmp32(pBuffer, nColorBits, nWidth, nHeight);
cv::Mat mt;
ProcessFile::DecodeIM(pszBmp32, mt);
//cv::imread(szBmpPath);
USES_CONVERSION;
char* szFilePath = W2A(m_szFileName);
cv::imwrite(szFilePath, mt);
delete pszBmp32;
return 0;
}