需要在C**Doc和C**View中进行相应修改


图像打开:

Doc.cpp中:


[cpp]​view plain​​​ ​​copy​


  1. BOOL CCVMFCDoc::Load(IplImage** pp, LPCTSTR csFilename)
  2. {
  3. IplImage* pImg=NULL;

  4. pImg = cvLoadImage(csFilename,-1); // 读图像文件(DSCV)
  5. if (!pImg) return(false);
  6. cvFlip(pImg); // 与 DIB 像素结构一致
  7. if (*pp) {
  8. cvReleaseImage(pp);
  9. }
  10. (*pp)=pImg;
  11. m_Display=0;
  12. return(true);
  13. }



[cpp]​view plain​​​ ​​copy​


  1. BOOL CCVMFCDoc::OnOpenDocument(LPCTSTR lpszPathName)
  2. {
  3. if(!CDocument::OnOpenDocument(lpszPathName)) return false;
  4. Load(&pImg,lpszPathName);
  5. if(pImg) return true;
  6. return false;
  7. }


图像保存:



1.Doc.cpp中


[cpp]​view plain​​​ ​​copy​


  1. BOOL CCVMFCDoc::Save(LPCTSTR csFilename, IplImage* pImg)
  2. {
  3. int bl;
  4. cvFlip(pImg); // 恢复原 OpenCV 位图结构
  5. bl=cvSaveImage(csFilename,pImg); // 图像存盘
  6. return(bl);
  7. }



2.View.cpp中



[cpp]​view plain​​​ ​​copy​


  1. void CCVMFCView::OnFileSaveAs()//图像保存
  2. {
  3. CFileDialog dlg(false,"*.bmp","Test.bmp",OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT,"保存为(*.bmp)|*.bmp|所有文件(*.*)|*.*||");
  4. CString strPath("");
  5. if(dlg.DoModal()==IDOK)
  6. {
  7. cvFlip(workImg);
  8. strPath=dlg.GetPathName();
  9. cvSaveImage(strPath,workImg);
  10. cvFlip(workImg);
  11. }
  12. }






整体代码如下:



[cpp]​view plain​​​ ​​copy​


  1. // CVMFCDoc.h : CCVMFCDoc 类的接口
  2. //@Author:ZrqSophia
  3. //@2012-3-2


  4. #pragma once


  5. class CCVMFCDoc : public CDocument
  6. {
  7. protected: // 仅从序列化创建
  8. CCVMFCDoc();
  9. DECLARE_DYNCREATE(CCVMFCDoc)

  10. // 属性
  11. public:

  12. // 操作
  13. public:

  14. // 重写
  15. public:
  16. virtual BOOL OnNewDocument();
  17. virtual void Serialize(CArchive& ar);

  18. // 实现
  19. public:
  20. virtual ~CCVMFCDoc();
  21. #ifdef _DEBUG
  22. virtual void AssertValid() const;
  23. virtual void Dump(CDumpContext& dc) const;
  24. #endif

  25. protected:

  26. // 生成的消息映射函数
  27. protected:
  28. DECLARE_MESSAGE_MAP()
  29. public:
  30. IplImage* pImg;
  31. int m_Display;
  32. BOOL Load(IplImage** pp, LPCTSTR csFilename);
  33. BOOL Save(LPCTSTR csFilename, IplImage* pImg);
  34. BOOL OnOpenDocument(LPCTSTR lpszPathName);
  35. };



[cpp]​view plain​​​ ​​copy​


  1. // CVMFCDoc.cpp : CCVMFCDoc 类的实现
  2. //Author:ZrqSophia
  3. //2012-3-2

  4. #include "stdafx.h"
  5. #include "CVMFC.h"

  6. #include "CVMFCDoc.h"

  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif


  10. // CCVMFCDoc

  11. IMPLEMENT_DYNCREATE(CCVMFCDoc, CDocument)

  12. BEGIN_MESSAGE_MAP(CCVMFCDoc, CDocument)
  13. END_MESSAGE_MAP()


  14. // CCVMFCDoc 构造/析构

  15. CCVMFCDoc::CCVMFCDoc()
  16. : pImg(NULL)
  17. , m_Display(0)
  18. {
  19. // TODO: 在此添加一次性构造代码

  20. }

  21. CCVMFCDoc::~CCVMFCDoc()
  22. {
  23. }

  24. BOOL CCVMFCDoc::OnNewDocument()
  25. {
  26. if (!CDocument::OnNewDocument())
  27. return FALSE;

  28. // TODO: 在此添加重新初始化代码
  29. // (SDI 文档将重用该文档)

  30. return TRUE;
  31. }




  32. // CCVMFCDoc 序列化

  33. void CCVMFCDoc::Serialize(CArchive& ar)
  34. {
  35. if (ar.IsStoring())
  36. {
  37. // TODO: 在此添加存储代码
  38. }
  39. else
  40. {
  41. // TODO: 在此添加加载代码
  42. }
  43. }


  44. // CCVMFCDoc 诊断

  45. #ifdef _DEBUG
  46. void CCVMFCDoc::AssertValid() const
  47. {
  48. CDocument::AssertValid();
  49. }

  50. void CCVMFCDoc::Dump(CDumpContext& dc) const
  51. {
  52. CDocument::Dump(dc);
  53. }
  54. #endif //_DEBUG


  55. // CCVMFCDoc 命令

  56. BOOL CCVMFCDoc::Load(IplImage** pp, LPCTSTR csFilename)
  57. {
  58. IplImage* pImg=NULL;

  59. pImg = cvLoadImage(csFilename,-1); // 读图像文件(DSCV)
  60. if (!pImg) return(false);
  61. cvFlip(pImg); // 与 DIB 像素结构一致
  62. if (*pp) {
  63. cvReleaseImage(pp);
  64. }
  65. (*pp)=pImg;
  66. m_Display=0;
  67. return(true);
  68. }

  69. BOOL CCVMFCDoc::Save(LPCTSTR csFilename, IplImage* pImg)
  70. {
  71. int bl;
  72. cvFlip(pImg); // 恢复原 OpenCV 位图结构
  73. bl=cvSaveImage(csFilename,pImg); // 图像存盘
  74. return(bl);
  75. }

  76. BOOL CCVMFCDoc::OnOpenDocument(LPCTSTR lpszPathName)
  77. {
  78. if(!CDocument::OnOpenDocument(lpszPathName)) return false;
  79. Load(&pImg,lpszPathName);
  80. if(pImg) return true;
  81. return false;
  82. }



[cpp]​view plain​​​ ​​copy​


  1. // CVMFCView.h : CCVMFCView 类的接口
  2. //@Author:Zrqsophia
  3. //@2012-3-2

  4. #pragma once


  5. class CCVMFCView : public CScrollView
  6. {
  7. protected: // 仅从序列化创建
  8. CCVMFCView();
  9. DECLARE_DYNCREATE(CCVMFCView)

  10. // 属性
  11. public:
  12. CCVMFCDoc* GetDocument() const;

  13. // 操作
  14. public:

  15. // 重写
  16. public:
  17. virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
  18. virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
  19. protected:
  20. virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
  21. virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
  22. virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

  23. // 实现
  24. public:
  25. virtual ~CCVMFCView();
  26. #ifdef _DEBUG
  27. virtual void AssertValid() const;
  28. virtual void Dump(CDumpContext& dc) const;
  29. #endif

  30. protected:
  31. IplImage* saveImg;
  32. IplImage* workImg;

  33. LPBITMAPINFO m_lpBmi;

  34. int m_CaptFlag;
  35. int m_dibFlag;
  36. int m_SaveFlag;
  37. int m_ImageType;
  38. // 生成的消息映射函数
  39. protected:
  40. afx_msg void OnFilePrintPreview();
  41. afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
  42. afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
  43. DECLARE_MESSAGE_MAP()
  44. public:
  45. virtual void OnInitialUpdate();
  46. afx_msg void OnSize(UINT nType, int cx, int cy);
  47. afx_msg void OnFileSaveAs();
  48. afx_msg void OnColorImageRefresh();
  49. };

  50. #ifndef _DEBUG // CVMFCView.cpp 中的调试版本
  51. inline CCVMFCDoc* CCVMFCView::GetDocument() const
  52. { return reinterpret_cast<CCVMFCDoc*>(m_pDocument); }
  53. #endif



[cpp]​view plain​​​ ​​copy​


  1. // CVMFCView.cpp : CCVMFCView 类的实现
  2. //@Author:ZrqSophia
  3. //@2012-3-2

  4. #include "stdafx.h"
  5. #include "CVMFC.h"

  6. #include "CVMFCDoc.h"
  7. #include "CVMFCView.h"
  8. #include "resource.h"

  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #endif


  12. // CCVMFCView

  13. IMPLEMENT_DYNCREATE(CCVMFCView, CScrollView)

  14. BEGIN_MESSAGE_MAP(CCVMFCView, CScrollView)
  15. // 标准打印命令
  16. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  17. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  18. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  19. ON_WM_SIZE()
  20. ON_COMMAND(ID_FILE_SAVE_AS, &CCVMFCView::OnFileSaveAs)
  21. ON_COMMAND(ID_COLOR_IMAGE_REFRESH, &CCVMFCView::OnColorImageRefresh)
  22. END_MESSAGE_MAP()

  23. // CCVMFCView 构造/析构

  24. CFile fCapture;
  25. CFileException eCapture;
  26. char pbuf[20];
  27. int captSetFlag=0;

  28. CCVMFCView::CCVMFCView()
  29. {
  30. // TODO: 在此处添加构造代码
  31. saveImg = NULL;
  32. workImg = NULL;

  33. m_lpBmi = 0;

  34. m_CaptFlag = 0;
  35. m_dibFlag = 0;
  36. m_ImageType= 0;

  37. CSize sizeTotal;
  38. sizeTotal.cx = sizeTotal.cy = 100;
  39. SetScrollSizes(MM_TEXT, sizeTotal);
  40. }

  41. CCVMFCView::~CCVMFCView()
  42. {
  43. if (saveImg)
  44. cvReleaseImage(&saveImg); // 释放位图
  45. if (workImg)
  46. cvReleaseImage(&workImg);

  47. if (m_lpBmi)
  48. free(m_lpBmi); // 释放位图信息
  49. }

  50. BOOL CCVMFCView::PreCreateWindow(CREATESTRUCT& cs)
  51. {
  52. // TODO: 在此处通过修改
  53. // CREATESTRUCT cs 来修改窗口类或样式

  54. return CScrollView::PreCreateWindow(cs);
  55. }

  56. // CCVMFCView 绘制

  57. void CCVMFCView::OnDraw(CDC* pDC)
  58. {
  59. CCVMFCDoc* pDoc = GetDocument();
  60. ASSERT_VALID(pDoc);
  61. if (!pDoc)
  62. return;

  63. // TODO: 在此处为本机数据添加绘制代码
  64. if(pDoc->pImg)
  65. {
  66. if(pDoc->m_Display==0)
  67. {
  68. imageClone(pDoc->pImg,&saveImg);
  69. m_dibFlag=imageClone(saveImg,&workImg);

  70. m_ImageType=imageType(workImg);
  71. m_SaveFlag=m_ImageType;
  72. pDoc->m_Display=1;
  73. }
  74. }

  75. if (m_dibFlag) { // DIB 结构改变
  76. if (m_lpBmi)
  77. free(m_lpBmi);
  78. m_lpBmi=CtreateMapInfo(workImg,m_dibFlag);
  79. m_dibFlag=0;

  80. CSize sizeTotal;
  81. sizeTotal=CSize(workImg->width,workImg->height);
  82. SetScrollSizes(MM_TEXT,sizeTotal);
  83. }

  84. char* pBits;
  85. if(workImg)
  86. pBits=workImg->imageData;

  87. if(workImg)
  88. StretchDIBits(pDC->m_hDC,
  89. 0,0,workImg->width,workImg->height,
  90. 0,0,workImg->width,workImg->height,
  91. pBits,m_lpBmi,DIB_RGB_COLORS,SRCCOPY);
  92. }


  93. // CCVMFCView 打印


  94. void CCVMFCView::OnFilePrintPreview()
  95. {
  96. AFXPrintPreview(this);
  97. }

  98. BOOL CCVMFCView::OnPreparePrinting(CPrintInfo* pInfo)
  99. {
  100. // 默认准备
  101. return DoPreparePrinting(pInfo);
  102. }

  103. void CCVMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  104. {
  105. // TODO: 添加额外的打印前进行的初始化过程
  106. }

  107. void CCVMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  108. {
  109. // TODO: 添加打印后进行的清理过程
  110. }

  111. void CCVMFCView::OnRButtonUp(UINT nFlags, CPoint point)
  112. {
  113. ClientToScreen(&point);
  114. OnContextMenu(this, point);
  115. }

  116. void CCVMFCView::OnContextMenu(CWnd* pWnd, CPoint point)
  117. {
  118. theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
  119. }


  120. // CCVMFCView 诊断

  121. #ifdef _DEBUG
  122. void CCVMFCView::AssertValid() const
  123. {
  124. CScrollView::AssertValid();
  125. }

  126. void CCVMFCView::Dump(CDumpContext& dc) const
  127. {
  128. CScrollView::Dump(dc);
  129. }

  130. CCVMFCDoc* CCVMFCView::GetDocument() const // 非调试版本是内联的
  131. {
  132. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCVMFCDoc)));
  133. return (CCVMFCDoc*)m_pDocument;
  134. }
  135. #endif //_DEBUG


  136. // CCVMFCView 消息处理程序

  137. void CCVMFCView::OnInitialUpdate()
  138. {
  139. CScrollView::OnInitialUpdate();

  140. CSize sizeTotal;
  141. // TODO: calculate the total size of this view
  142. sizeTotal.cx = sizeTotal.cy = 100;
  143. SetScrollSizes(MM_TEXT, sizeTotal);
  144. }

  145. void CCVMFCView::OnSize(UINT nType, int cx, int cy)
  146. {
  147. CScrollView::OnSize(nType, cx, cy);

  148. if (workImg) { // 刷新窗口画面
  149. CSize sizeTotal;
  150. sizeTotal = CSize(workImg->width,workImg->height);
  151. SetScrollSizes(MM_TEXT, sizeTotal); // 设置滚动条
  152. }
  153. }

  154. void CCVMFCView::OnFileSaveAs()
  155. {
  156. CFileDialog dlg(false,"*.bmp","Test.bmp",OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT,"保存为(*.bmp)|*.bmp|所有文件(*.*)|*.*||");
  157. CString strPath("");
  158. if(dlg.DoModal()==IDOK)
  159. {
  160. cvFlip(workImg);
  161. strPath=dlg.GetPathName();
  162. cvSaveImage(strPath,workImg);
  163. cvFlip(workImg);
  164. }
  165. }
  166. void CCVMFCView::OnColorImageRefresh()
  167. {
  168. CCVMFCDoc* pDoc = GetDocument();
  169. ASSERT_VALID(pDoc);
  170. pDoc->m_Display=0;
  171. Invalidate();
  172. }