|
发表于 2010-7-23 09:58:41
|
显示全部楼层
看是不是可以参考如下代码
/**********************************
* SAVE PICTURE
***********************************/
void CCameraTest2Dlg::OnBnClickedSave()
{
int pic_size=320*240*2; //图形缓冲区大小
int nBytesRead; //读取的数据长度
unsigned char *bufOut;
BOOL f;
bufOut=(unsigned char *)malloc(pic_size);
f=ReadFile(hCam,bufOut,pic_size,(LPDWORD)&nBytesRead,NULL);
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 320;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 240;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 16;
RGB16BitsBITMAPINFO.bmiHeader.biCompression = BI_BITFIELDS;
//在填充数据
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +16/*调色板结构大小*/;
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + (pic_size); ///2=(16 / 8)
#if 0//待调试
//停止采集
f = DeviceIoControl (hCam, IOCTL_CAM_STOP,
NULL, 0,
NULL, 0, NULL, NULL);
if(!f)
printf("IOCTL_CAM_STOP wrong!\n");
else
{
Bplay->SetWindowText(_T("play"));
CAM_PLAY=0;
}
CFileDialog Save(false /*如果为真则对话框为打开对话框,为否则为保存对话框*/,
TEXT("bmp") /*默认后缀名*/,
TEXT("cam") /*默认文件名*/,
0 /*对话框风格*/,
TEXT("bmp|*.bmp|"),
this /*父窗口指针*/);
CString strFilePath;
if (Save.DoModal() == IDOK)
{
strFilePath = Save.GetPathName();
}
//CString 转char * 用了Unicode字符集,CString是宽字符
//-------------------------------------
wchar_t* wCharString = strFilePath.GetBuffer(strFilePath.GetLength()+1);
size_t origsize = wcslen(wCharString) + 1;
char *name;
name=new char(origsize);
wcstombs(name, wCharString ,origsize);
printf("%s\n",name);
//---------------------------------------
#else
static int file_index=0;
char name[64];
memset(name, 0, sizeof(name));
sprintf(name, "%s%02d.jpg", "cam", file_index++);
#endif
FILE *mStream = NULL;
if((mStream = fopen(name,"wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write palette info
fwrite(&RGB565, 16, 1, mStream);
//write bitmap pixels data
fwrite(bufOut, pic_size, 1, mStream);
//close file
fclose(mStream);
printf("save pic %s done\n",name);
}
else
printf("open %s fail\n",name);
} |
|