有一个一维数组image1保存的是RGB565的图片数据,可以确保图片可以显示成功(已经实现),现在想将其保存为16位或者24位的BMP文件,但是我总是保存不对,图片颜色不对,并且变形了,代码如下,先谢了:void CTakePhoto::OnBnClickedKeep()
{
CString fileName=TEXT("tupian");
CString filePath=TEXT("\\Storage Card\\");
int iWidth = WIDTH;//WIDTH,HEIGHT是image1图片的高和宽,我用了宏定义分别为205 169
int iHeight= HEIGHT;
//invert(image1);
const unsigned char * pBuffer=image1; SaveDIB2Bmp( fileNum,fileName ,filePath ,iWidth,iHeight, pBuffer); fileNum++;}//构建BMP文件信息头
void CTakePhoto::ConstructBih(int nWidth, int nHeight, BITMAPINFO& bih)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //为了使得每行像素为4的倍数
bih.bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
bih.bmiHeader.biWidth = nWidth;
bih.bmiHeader.biHeight = -nHeight;
bih.bmiHeader.biPlanes = 1;
bih.bmiHeader.biBitCount = 16;
bih.bmiHeader.biCompression = BI_BITFIELDS;//BI_RGB仅有555格式
bih.bmiHeader.biClrUsed = 0;
bih.bmiHeader.biClrImportant = 0;
bih.bmiHeader.biSizeImage= widthStep*nHeight*2; //这里应该是数组长度,这个与这个算法的结果相等
bih.bmiHeader.biXPelsPerMeter=0;
bih.bmiHeader.biYPelsPerMeter=0;
bih.bmiColors[1].rgbBlue = 0xf800;
bih.bmiColors[1].rgbGreen = 0x07e0;
bih.bmiColors[1].rgbRed = 0x001F;
bih.bmiColors[1].rgbReserved = 0; }void CTakePhoto::ContructBhh(int nWidth, int nHeight, BITMAPFILEHEADER& bhh)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界) bhh.bfType = ((WORD) ('M' << 8) | 'B'); //'BM' BITMAPINFOHEADER
bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO) + widthStep*nHeight*2;
bhh.bfReserved1 = 0;
bhh.bfReserved2 = 0;
bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO);
}//保存buffer到bmp文件 //iWidth:图像宽; iHeight:图像高;pBuffer:图像RGB数据;filePath:存储路径;fileName:保存文件名;
//fileNum:保存文件编号
bool CTakePhoto::SaveDIB2Bmp(int fileNum, CString fileName , CString filePath ,
int iWidth, int iHeight, const unsigned char * pBuffer)
{
BITMAPINFO bih;
ConstructBih(iWidth,iHeight,bih);
BITMAPFILEHEADER bhh;
ContructBhh(iWidth,iHeight,bhh); int widthStep = (((iWidth * 16) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
int DIBSize = widthStep * iHeight*2 ; //buffer的大小 (字节为单位) CString path_temp=TEXT("");
CString temp; temp.Format(_T("%d"),fileNum);
path_temp= filePath+fileName+temp+_T(".BMP");
// LPCTSTR BMPFileName=L"\\Storage Card\\fram101.bmp";//测试语句 CFile file;
if(file.Open(path_temp,CFile::modeWrite | CFile::modeCreate))
{//写入文件
file.Write((LPSTR)&bhh,sizeof(BITMAPFILEHEADER));
file.Write((LPSTR)&bih,sizeof(BITMAPINFO));
file.Write(pBuffer,DIBSize);
file.Close();
return true;
}
else
{
MessageBox(TEXT("创建文件失败!"));
return false;
}
}bmprgb图片
{
CString fileName=TEXT("tupian");
CString filePath=TEXT("\\Storage Card\\");
int iWidth = WIDTH;//WIDTH,HEIGHT是image1图片的高和宽,我用了宏定义分别为205 169
int iHeight= HEIGHT;
//invert(image1);
const unsigned char * pBuffer=image1; SaveDIB2Bmp( fileNum,fileName ,filePath ,iWidth,iHeight, pBuffer); fileNum++;}//构建BMP文件信息头
void CTakePhoto::ConstructBih(int nWidth, int nHeight, BITMAPINFO& bih)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //为了使得每行像素为4的倍数
bih.bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
bih.bmiHeader.biWidth = nWidth;
bih.bmiHeader.biHeight = -nHeight;
bih.bmiHeader.biPlanes = 1;
bih.bmiHeader.biBitCount = 16;
bih.bmiHeader.biCompression = BI_BITFIELDS;//BI_RGB仅有555格式
bih.bmiHeader.biClrUsed = 0;
bih.bmiHeader.biClrImportant = 0;
bih.bmiHeader.biSizeImage= widthStep*nHeight*2; //这里应该是数组长度,这个与这个算法的结果相等
bih.bmiHeader.biXPelsPerMeter=0;
bih.bmiHeader.biYPelsPerMeter=0;
bih.bmiColors[1].rgbBlue = 0xf800;
bih.bmiColors[1].rgbGreen = 0x07e0;
bih.bmiColors[1].rgbRed = 0x001F;
bih.bmiColors[1].rgbReserved = 0; }void CTakePhoto::ContructBhh(int nWidth, int nHeight, BITMAPFILEHEADER& bhh)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界) bhh.bfType = ((WORD) ('M' << 8) | 'B'); //'BM' BITMAPINFOHEADER
bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO) + widthStep*nHeight*2;
bhh.bfReserved1 = 0;
bhh.bfReserved2 = 0;
bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO);
}//保存buffer到bmp文件 //iWidth:图像宽; iHeight:图像高;pBuffer:图像RGB数据;filePath:存储路径;fileName:保存文件名;
//fileNum:保存文件编号
bool CTakePhoto::SaveDIB2Bmp(int fileNum, CString fileName , CString filePath ,
int iWidth, int iHeight, const unsigned char * pBuffer)
{
BITMAPINFO bih;
ConstructBih(iWidth,iHeight,bih);
BITMAPFILEHEADER bhh;
ContructBhh(iWidth,iHeight,bhh); int widthStep = (((iWidth * 16) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
int DIBSize = widthStep * iHeight*2 ; //buffer的大小 (字节为单位) CString path_temp=TEXT("");
CString temp; temp.Format(_T("%d"),fileNum);
path_temp= filePath+fileName+temp+_T(".BMP");
// LPCTSTR BMPFileName=L"\\Storage Card\\fram101.bmp";//测试语句 CFile file;
if(file.Open(path_temp,CFile::modeWrite | CFile::modeCreate))
{//写入文件
file.Write((LPSTR)&bhh,sizeof(BITMAPFILEHEADER));
file.Write((LPSTR)&bih,sizeof(BITMAPINFO));
file.Write(pBuffer,DIBSize);
file.Close();
return true;
}
else
{
MessageBox(TEXT("创建文件失败!"));
return false;
}
}bmprgb图片
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货