19,468
社区成员
发帖
与我相关
我的任务
分享
LRESULT CJVSDKDEMODlg::OnSDKNotify(WPARAM wParam, LPARAM lParam)
{
if (lParam == JVS_WM_COPYRIGHT)
{
//
}
else if (lParam == JVS_WM_BITMAP_OK)
{
int nChannel = wParam;
if (nChannel<0 || nChannel > JDVR_MAX_CHANNEL)
{
return FALSE;
}
if (g_config.Channel[nChannel].Bitmap.pRGB == NULL)
{
return FALSE;
}
int nWidth = g_config.Channel[0].Bitmap.dwWidth;
int nHeight = g_config.Channel[0].Bitmap.dwHeight;
int nBits = 4;
ULONG ulSize = nWidth * nHeight * nBits;
FILE * fout = fopen("bitmap.bmp", "wb");
if (fout)
{
BITMAPFILEHEADER bmHeader;
memset(&bmHeader, 0, sizeof(bmHeader));
bmHeader.bfType = 0x4D42;
bmHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nWidth * nHeight * nBits;
bmHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER bmInfo;
memset(&bmInfo, 0, sizeof(bmInfo));
bmInfo.biSize = sizeof(bmInfo);
bmInfo.biWidth = nWidth;
bmInfo.biHeight = nHeight;
bmInfo.biPlanes = 1;
bmInfo.biBitCount = nBits * 8; //每个像素位数32位,虽然这个符号nBits有点蛋疼,但是前面已经定义是4了,应该nBytes更合理些
bmInfo.biCompression = 0;
bmInfo.biSizeImage = nWidth * nHeight * nBits; // 实际图像占用的字节数
bmInfo.biXPelsPerMeter = 300;
bmInfo.biYPelsPerMeter = 300;
fwrite(&bmHeader, sizeof(bmHeader), 1, fout);
fwrite(&bmInfo, sizeof(bmInfo), 1, fout);
fwrite(g_config.Channel[nChannel].Bitmap.pRGB, nWidth * nHeight * nBits, 1, fout);
fclose(fout);
m_lstInfo.SetCurSel(m_lstInfo.AddString("Capture to \"bitmap.bmp\""));
// ********************************** 绘制位图***********************
DWORD count = nWidth * nHeight * nBits;
unsigned char* buff = new unsigned char[count];
unsigned char* buff_out = new unsigned char[count];
//int* buff_out = new int[count];
unsigned char ImgMax = 0;
unsigned char ImgMin = 0;
//ImgMax = ceil(0.9*255) - 20;
//ImgMin = ceil(0.1*100) + 100;
ImgMax = 255;
ImgMin = 100;
//输入图像每像素字节数,彩色图像为4字节/像素
int pixelByteIn=4;
//输入图像每行像素所占字节数,必须是4的倍数
int lineByteIn=(nWidth*pixelByteIn+ 3)/4*4; // + 3
//灰值化后,每像素位数为8比特
int m_nBitCountOut=8;
//输出图像每行像素所占字节数,必须是4的倍数
int lineByteOut=(nWidth*m_nBitCountOut/8+ 3)/4*4; // +3
int i = 0,j = 0;
for(i = 0; i < nHeight; ++i)
{
memcpy(&buff[i*nWidth*nBits],&(g_config.Channel[nChannel].Bitmap.pRGB[(nHeight - i) * nWidth * nBits]),nWidth * nBits);
}
for(i = 0; i < nHeight; ++i)
{
for(j = 0; j < nWidth; ++j)
{
//根据灰值化公式为输出图像赋值
*(buff_out+i*nWidth*nBits/*nWidth应该是lineByteIn */+j)
=(0.30**(buff+i*nWidth*nBits/*nWidth应该是lineByteIn 以下都应该要改*/+j*pixelByteIn+1)
+0.59**(buff+i*nWidth*nBits+j*pixelByteIn+2)
+0.11**(buff+i*nWidth*nBits+j*pixelByteIn+3)+0.5); // 转换为灰度图的像素数据 有点问题?求解
ImgTemp = int(*(buff_out+i*nWidth*nBits+j) - ImgMin);
// 会不会出错和你使用位图有关,如果位图宽度字节数刚好是4的倍数就不会有问题,否则图像会出现问题
if (ImgTemp < 0)
{
*(buff_out+i*nWidth*nBits+j) = 0;
}
else
{
ImgTemp = int((abs(*(buff_out+i*nWidth*nBits+j) - ImgMin))/(ImgMax - ImgMin)*255);
if (ImgTemp > 255)
{
*(buff_out+i*nWidth*nBits+j) = 255;
}
else
{
*(buff_out+i*nWidth*nBits+j) = (abs(*(buff_out+i*nWidth*nBits+j) - ImgMin))/(ImgMax - ImgMin)*255;
}
}
}
}
CBitmap bitmap;
bitmap.CreateBitmap(nWidth ,nHeight ,1,(nBits) * 8,buff_out);
CWnd* pWnd = GetDlgItem(IDC_SHOW_IMAGE);
CDC* dc = pWnd->GetDC();
CDC MemDC;
MemDC.CreateCompatibleDC(dc);
MemDC.SelectObject(&bitmap);
CRect rect;
pWnd->GetClientRect(&rect);
dc->SetStretchBltMode(HALFTONE);
int bmpPro = 1;
dc->StretchBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,bmpPro*rect.Width(),bmpPro*rect.Height(),SRCCOPY);
//dc->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);//显示位图
//释放RGB缓冲
delete[] g_config.Channel[nChannel].Bitmap.pRGB;
g_config.Channel[nChannel].Bitmap.pRGB = NULL;
}
}
return TRUE;
}