huago-corrcet_tools/HuaGoCorrect/CVISON.cpp

203 lines
4.1 KiB
C++
Raw Permalink Normal View History

2020-08-31 08:08:50 +00:00
#include "stdafx.h"
#include "CVISON.h"
#include "HuaGoCorrect.h"
#include "afxdialogex.h"
#include "HuaGoCorrectDlg.h"
2020-08-31 08:08:50 +00:00
// CA3 <20>Ի<EFBFBD><D4BB><EFBFBD>
using namespace cv;
2020-08-31 08:08:50 +00:00
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);
2020-08-31 08:08:50 +00:00
}
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<void(bool stoped, int num)> onstop)
{
m_onStop = onstop;
2020-08-31 08:08:50 +00:00
}
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)
2020-08-31 08:08:50 +00:00
END_MESSAGE_MAP()
// CA3 <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BOOL CVISON::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC>
return TRUE; // return TRUE unless you set the focus to a control
// <20>쳣: OCX <20><><EFBFBD><EFBFBD>ҳӦ<D2B3><D3A6><EFBFBD><EFBFBD> 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;
//<2F>ؽ<EFBFBD>cimage
cimage.Destroy();
cimage.Create(nWidth, nHeight, 8 * nChannels);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uchar* pucRow;//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
uchar* pucImage = (uchar*)cimage.GetBits();//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
int nStep = cimage.GetPitch();//ÿ<>е<EFBFBD><D0B5>ֽ<EFBFBD><D6BD><EFBFBD><><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD>
if (1 == nChannels)//<2F><><EFBFBD>ڵ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>
{
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<uchar>(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: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
imageindex--;
if (imageindex < 0)
imageindex = 0;
if (m_path.size() > 0)
{
DrawPicture(m_path[imageindex]);
}
}
void CVISON::OnBnClickedBtnNext()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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)
{
2021-08-17 01:07:55 +00:00
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));
}
}