HVSTATUS status = STATUS_OK; HHV hhv = NULL; int i,j; /* * 初始化所有成员变量,同时打开数字摄像机 */ status = BeginHVDevice(DeviceNum, &hhv);// 打开数字摄像机 1 HV_VERIFY(status);// 检验状态 /* * 初始化数字摄像机硬件状态,用户也可以在其他位置初始化数字摄像机, * 但应保证数字摄像机已经打开,建议用户在应用程序初始化时, * 同时初始化数字摄像机硬件。 */ HVSetResolution(hhv, Resolution);//设置分辨率 HVSetSnapMode(hhv, SnapMode);// 采集模式,包括 CONTINUATION(连续)、TRIGGER(外触发) for (i = 0; i < 4; i++) { HVAGCControl(hhv, RED_CHANNEL + i, Gain);// 设置各个分量的增益 } SetExposureTime(hhv,Width,ExposureTint_Upper,ExposureTint_Lower);// 设置曝光时间 HVADCControl(hhv, ADC_BITS, ADCLevel);// 设置ADC的级别 /* * 视频输出窗口,即视频输出范围,输出窗口取值范围必须在输入窗口范围以内, * 视频窗口左上角X坐标和窗口宽度应为4的倍数,左上角Y坐标和窗口高度应为2的倍数 * 输出窗口的起始位置一般设置为(0, 0)即可。 */ HVSetOutputWindow(hhv, XStart, YStart, Width, Height); BYTE *pRawBuffer = new BYTE[Width * Height];//分配原始图像缓冲区,一般用来存储采集图像原始数据 ASSERT(pRawBuffer); //一般图像缓冲区大小由输出窗口大小和视频格式确定。 BYTE *pImageBuffer = new BYTE[Width * Height * 3];//分配Bayer转换后图像数据缓冲 ASSERT(pImageBuffer); BYTE *pInfoBuffer = new BYTE[sizeof(BITMAPINFO)];// 分配BITMAPINFO缓冲 ASSERT(pInfoBuffer); BYTE* pTargetBuffer = NULL; DWORD dwTargetSize = 0; dwTargetSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256 + Height * Width; pTargetBuffer = (BYTE*)VirtualAlloc(NULL, dwTargetSize, MEM_COMMIT, PAGE_READWRITE); memset(pTargetBuffer, 0, dwTargetSize); /* * 初始化BITMAPINFO 结构,此结构在保存bmp文件、显示采集图像时使用 */ // m_pBmpInfo即指向m_chBmpBuf缓冲区,用户可以自己分配BTIMAPINFO缓冲区 BITMAPINFO *pBmpInfo = (BITMAPINFO *)pInfoBuffer; pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pBmpInfo->bmiHeader.biWidth = Width;// 图像宽度,一般为输出窗口宽度 pBmpInfo->bmiHeader.biHeight = Height;// 图像宽度,一般为输出窗口高度 pBmpInfo->bmiHeader.biBitCount = 24;// 图像位深度,数字摄像机采集的原始数据为8位,Bayer转换后为24位 // 以下设置一般相同,对于低于8位的位图,还应设置相应的位图调色板 pBmpInfo->bmiHeader.biPlanes = 1; pBmpInfo->bmiHeader.biCompression = BI_RGB; pBmpInfo->bmiHeader.biSizeImage = 0; pBmpInfo->bmiHeader.biXPelsPerMeter = 0; pBmpInfo->bmiHeader.biYPelsPerMeter = 0; pBmpInfo->bmiHeader.biClrUsed = 0; pBmpInfo->bmiHeader.biClrImportant = 0; BYTE *ppBuf[1]; ppBuf[0] = pRawBuffer; status = HVSnapShot(hhv, ppBuf,1);// 采集1帧图像到内存,采集完成后停止 HV_VERIFY(status); if (HV_SUCCESS(status)) { // 将原始图像数据进行Bayer转换,转换后为24位。 //同时将原始数据进行上下翻转 //颜色查找表 BYTE pLutR[256] ; BYTE pLutG[256] ; BYTE pLutB[256] ; for(int i=0;i<256;i++) { pLutR[i] = i; pLutG[i] = i; pLutB[i] = i; } ConvertBayer2Rgb(pImageBuffer,pRawBuffer,Width,Height,ConvertType,pLutR,pLutG,pLutB,true,Layout); ////////////////////////////////////////////////////////////////////////// BITMAPFILEHEADER *pTargetFileHeader = (BITMAPFILEHEADER *)pTargetBuffer; BITMAPINFOHEADER *pTargetInfoHeader = (BITMAPINFOHEADER *)(pTargetBuffer + sizeof(BITMAPFILEHEADER)); pTargetFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD)*256; pTargetFileHeader->bfReserved1 = 0; pTargetFileHeader->bfReserved2 = 0; pTargetFileHeader->bfSize = dwTargetSize; pTargetFileHeader->bfType = 0x4d42; pTargetInfoHeader->biBitCount = 8; pTargetInfoHeader->biClrImportant = 0; pTargetInfoHeader->biClrUsed = 256; pTargetInfoHeader->biCompression = BI_RGB; pTargetInfoHeader->biHeight = Height; pTargetInfoHeader->biPlanes = 1; pTargetInfoHeader->biSize =sizeof(BITMAPINFOHEADER); pTargetInfoHeader->biSizeImage = Height * nTargetWidth; pTargetInfoHeader->biWidth = Width; pTargetInfoHeader->biXPelsPerMeter = 0; pTargetInfoHeader->biYPelsPerMeter = 0; RGBQUAD *pRGBQUAD; for (i=0; i<256; i++) //初始化8位灰度图的调色板信息 { pRGBQUAD = (RGBQUAD*)(pTargetBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ i*sizeof(RGBQUAD)); pRGBQUAD->rgbBlue = i; pRGBQUAD->rgbGreen = i; pRGBQUAD->rgbRed = i; pRGBQUAD->rgbReserved = 0; } for (i=0;i<Height;i++) { for (j=0; j<Width; j++) { pTargetBuffer[54+sizeof(RGBQUAD)*256 + i*nTargetWidth + j] = (pImageBuffer[54+i*nSourceWidth +j*3] * 11 +pImageBuffer[54+i*nSourceWidth +j*3+1] * 59 +pImageBuffer[54+i*nSourceWidth +j*3+2] * 30)/100; } } for (i=0;i<Height;i++) { for (j=0; j<Width; j++) { if (pTargetBuffer[54+sizeof(RGBQUAD)*256 + i*nTargetWidth + j] > 220) { pTargetBuffer[54+sizeof(RGBQUAD)*256 + i*nTargetWidth + j] = 255; } else pTargetBuffer[54+sizeof(RGBQUAD)*256 + i*nTargetWidth + j] = 0; } } ////////////////////////////////////////////////////////////////////////// /* * 以下保存BMP文件设置基本相同 */ CFileDialog dlg(FALSE , "*.bmp", NULL ,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, "Bitmap Files(*.bmp)|*.bmp", this); if (dlg.DoModal() == IDOK) { DWORD dwImageSize = 0; DWORD dwBytesRead = 0; BOOL bRVal = TRUE; HANDLE hFile = ::CreateFile(dlg.GetPathName(), GENERIC_WRITE , 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if (hFile == INVALID_HANDLE_VALUE) { bRVal = FALSE; } else { ::WriteFile(hFile,pTargetBuffer, dwTargetSize, &dwBytesRead, NULL); CloseHandle(hFile); } } } delete []pRawBuffer; delete []pImageBuffer; delete []pInfoBuffer; VirtualFree(pTargetBuffer,NULL,MEM_RELEASE); // 关闭数字摄像机1 status = EndHVDevice(hhv); HV_VERIFY(status);