MFC实现位图序列播放并计算位图亮度
/********************************************************** 功能:选择图像序列文件夹后调用该线程进行播放序列 参数:对话框句柄 作者:airduce 时间:2018-9-26 10:02:46 修改时间:无 修改内容:新建 ********************************************************************/ UINT ThreadOne(LPVOID lpParameter){ //showPic(0,TRUE); CMFCDialogDlg *mydlg =(CMFCDialogDlg *) lpParameter; mydlg->m_lbl_total = 1000; CString selectPath; mydlg->GetDlgItemTextW(IDC_MFCEDITBROWSE1,selectPath); string StrSelectPath(CW2A(selectPath.GetString())); StrSelectPath = StrSelectPath + "\\"; const char * myPath = StrSelectPath.data(); CString EntName("bmp"); vector<string> myStrings = listFiles(myPath); mydlg->m_slider.SetRange(0,myStrings.size()-1); //总帧数 mydlg->m_lbl_total=myStrings.size(); while (true){ if(index==myStrings.size()){ Sleep(100); continue; } if(!flag){ Sleep(100); continue; } //当前帧显示值 mydlg->m_lbl = index+1; //设置拉条位置 mydlg->m_slider.SetPos(index); CString BmpName( myStrings.at(index).c_str()); mydlg->m_lbl_name = BmpName; //mydlg->UpdateData(FALSE); CString _path("\\"); BmpName = selectPath + _path +BmpName; EntName.MakeLower(); if(EntName.Compare(_T("bmp")) == 0) { //定义变量存储图片信息 BITMAPINFO *pBmpInfo; //记录图像细节 BYTE *pBmpData; //图像数据 BITMAPFILEHEADER bmpHeader; //文件头 BITMAPINFOHEADER bmpInfo; //信息头 CFile bmpFile; //记录打开文件 //以只读的方式打开文件 读取bmp图片各部分 bmp文件头 信息 数据 if(!bmpFile.Open(BmpName, CFile::modeRead|CFile::typeBinary)) return 0; if (bmpFile.Read(&bmpHeader,sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER)) return 0; if (bmpFile.Read(&bmpInfo,sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER)) return 0; pBmpInfo = (BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER)]; //为图像数据申请空间 memcpy(pBmpInfo,&bmpInfo,sizeof(BITMAPINFOHEADER)); DWORD dataBytes = bmpHeader.bfSize - bmpHeader.bfOffBits; pBmpData = (BYTE*)new char[dataBytes]; bmpFile.Read(pBmpData,dataBytes); //获取位图亮度值 /****************************************************************/ if(bmpInfo.biBitCount==24){ double radL[256*256]; double maxradl=0; //最高亮度 double minradl=65535;//最小亮度 double meanradl;//最大亮度 double sum=0; for(int row = 0; row < 256; row++) { for(int col = 0; col < 256; col++) { BYTE R = pBmpData[256 * row * 3 + col * 3 + 0]; BYTE G = pBmpData[256 * row * 3 + col * 3 + 1]; BYTE B = pBmpData[256 * row * 3 + col * 3 + 2]; radL[row*256+col] = R*100+G+B/255.0; //辐射亮度 sum+=radL[row*256+col]; if(radL[row*256+col]>maxradl) maxradl=radL[row*256+col]; if(radL[row*256+col]<minradl) minradl=radL[row*256+col]; //frameimage[row*256+col]=radL[row*256+col]; } } meanradl=sum/65536; maxradl = ((double)((int)((maxradl+0.005)*100)))/100; meanradl = ((double)((int)((meanradl+0.005)*100)))/100; mydlg->max_lum = maxradl; mydlg->ave_lum = meanradl; } /****************************************************************/ //bmpFile.Read(pBmpData,dataBytes); bmpFile.Close(); //显示图像 CWnd *pWnd=mydlg->GetDlgItem(IDC_STATIC_PIC); //获得pictrue控件窗口的句柄 CRect rect; pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域 CDC *pDC=pWnd->GetDC(); //获得pictrue控件的DC pDC->SetStretchBltMode(COLORONCOLOR); StretchDIBits(pDC->GetSafeHdc(),0,0,rect.Width(),rect.Height(),0,0, bmpInfo.biWidth,bmpInfo.biHeight,pBmpData,pBmpInfo,DIB_RGB_COLORS,SRCCOPY); delete pBmpInfo; delete pBmpData; Sleep(15); index++; } } ExitThread(0); return 0; }
运行效果:
.