#include "stdafx.h" #include "CVISON.h" #include "HuaGoCorrect.h" #include "afxdialogex.h" #include "HuaGoCorrectDlg.h" // CA3 对话框 using namespace cv; IMPLEMENT_DYNAMIC(CVISON, CDialog) CVISON::CVISON(CWnd* pParent /*=NULL*/) : CDialog(CVISON::IDD, pParent) { m_run = true; m_auireable = false; m_updateimgshow = std::thread(&CVISON::showfun, this); } CVISON::~CVISON() { if (m_updateimgshow.joinable()) { m_auireable = false; m_run = false; m_updateimgshow.join(); } } void CVISON::DrawPicture(CString path) { CImage img; img.Load(path); CRect rectf; int cx = img.GetWidth(); int cy = img.GetHeight(); if (cx == 0 || cy == 0) return; CWnd* pWnd = GetDlgItem(IDC_PIC); pWnd->GetClientRect(&rectf); CDC* pDC = pWnd->GetDC(); SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE); CPoint pos = rectf.TopLeft(); rectf = CRect(pos, CSize(630,535)); img.Draw(pDC->m_hDC, rectf); ReleaseDC(pDC); img.Destroy(); } void CVISON::SetCallBack(std::function onstop) { m_onStop = onstop; } void CVISON::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CVISON, CDialog) ON_BN_CLICKED(IDC_BTN_PREPAGE, &CVISON::OnBnClickedBtnPrepage) ON_BN_CLICKED(IDC_BTN_NEXT, &CVISON::OnBnClickedBtnNext) END_MESSAGE_MAP() // CA3 消息处理程序 BOOL CVISON::OnInitDialog() { CDialog::OnInitDialog(); // TODO: 在此添加额外的初始化 return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } void CVISON::MatToCImage(cv::Mat& mat, CImage& cimage) { if (0 == mat.total()) { return; } int nChannels = mat.channels(); if ((1 != nChannels) && (3 != nChannels)) { return; } int nWidth = mat.cols; int nHeight = mat.rows; //重建cimage cimage.Destroy(); cimage.Create(nWidth, nHeight, 8 * nChannels); //拷贝数据 uchar* pucRow;//指向数据区的行指针 uchar* pucImage = (uchar*)cimage.GetBits();//指向数据区的指针 int nStep = cimage.GetPitch();//每行的字节数,注意这个返回值有正有负 if (1 == nChannels)//对于单通道的图像需要初始化调色板 { RGBQUAD* rgbquadColorTable; int nMaxColors = 256; rgbquadColorTable = new RGBQUAD[nMaxColors]; cimage.GetColorTable(0, nMaxColors, rgbquadColorTable); for (int nColor = 0; nColor < nMaxColors; nColor++) { rgbquadColorTable[nColor].rgbBlue = (uchar)nColor; rgbquadColorTable[nColor].rgbGreen = (uchar)nColor; rgbquadColorTable[nColor].rgbRed = (uchar)nColor; } cimage.SetColorTable(0, nMaxColors, rgbquadColorTable); delete[]rgbquadColorTable; } for (int nRow = 0; nRow < nHeight; nRow++) { pucRow = (mat.ptr(nRow)); for (int nCol = 0; nCol < nWidth; nCol++) { if (1 == nChannels) { *(pucImage + nRow * nStep + nCol) = pucRow[nCol]; } else if (3 == nChannels) { for (int nCha = 0; nCha < 3; nCha++) { *(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha]; } } } } } void CVISON::OnBnClickedBtnPrepage() { // TODO: 在此添加控件通知处理程序代码 imageindex--; if (imageindex < 0) imageindex = 0; if (m_path.size() > 0) { DrawPicture(m_path[imageindex]); } } void CVISON::OnBnClickedBtnNext() { // TODO: 在此添加控件通知处理程序代码 imageindex++; if (imageindex >= m_path.size()) imageindex = m_path.size() - 1; if (m_path.size() > 0) { DrawPicture(m_path[imageindex]); } } void CVISON::SetEnableShow(bool show) { m_auireable = show; aquiredimgindx = 0; parent = (CHuaGoCorrectDlg*)GetParent(); } void CVISON::showfun() { while (m_run) { if (m_auireable) { if (parent) { std::string path; auto ret = ((CHuaGoCorrectDlg*)parent)->m_drv->aquire_image(path, 0); if (ret != -1) { if (path.length() > 0) { imageindex++; aquiredimgindx++; USES_CONVERSION; std::string t_path = StringToUtf(path); CString cPath(A2T(t_path.c_str())); m_path.push_back(cPath); m_onStop(false, aquiredimgindx); DrawPicture(cPath); } } else { m_auireable = false; m_onStop(true,0); } } } else std::this_thread::sleep_for(std::chrono::milliseconds(100)); } }