其一是借助opencv,其二是利用流对象。
方法一:
CvMat *mat; //创建矩阵
mat = cvCreateMat(640,480,CV_8UC1); //指定分配内存大小
cvInitMatHeader(mat,640,480,CV_8UC1,JPEGBuf);
/*初始化矩阵信息头,这里的JPEGBuf就是JPEG图像数据的地址。现在很多摄像头是支持JPEG输出的,而且JPEG图像输出节 省宽带。640*480大小的图片大小仅在20K以内。网上提到的什么视频采集卡,提供的SDK也基本提供JPEG数据,它获得的数 据就是图像数据,而不是图像文件。*/
IplImage *pIplImage = cvDecodeImage(mat,CV_LOAD_IMAGE_COLOR); //这里将JPEG图像数组转化为IplImage类,这里自动包含了解压JPEG格式图片功能。 if(pIplImage != NULL) //如果解压失败得到的是NULL { CvvImage cimg; //CvvImage类在opencv 2.2以后没有CvvImage类了,网上搜索这个类,有低版本的源代码,直接添加到工程里就可以用了。 cimg.CopyOf( pIplImage); //复制图像 cimg.DrawToHDC(pMainDlg->m_DispHDC, &pMainDlg->m_DispRECT); //显示图像 cvReleaseImage(&pIplImage);//释放图像 }
cvReleaseMat(&mat); //释放矩阵
下面这个是个学习资料。
利用opencv读取图片并在MFC上显示,链接地址:http://licong1018.blog.163.com/blog/static/9026978420129239178934/
更新:因为自己需要,想要弄这个还要去翻看原来的工程文件。索性把CvvImage类贴在这里:
一、CvvImage.h
1 #pragma once 2 3 #ifndef CVVIMAGE_CLASS_DEF 4 #define CVVIMAGE_CLASS_DEF 5 6 #include "opencv.hpp" 7 8 /* CvvImage class definition */ 9 class CvvImage 10 { 11 public: 12 CvvImage(); 13 virtual ~CvvImage(); 14 15 /* Create image (BGR or grayscale) */ 16 virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 ); 17 18 /* Load image from specified file */ 19 virtual bool Load( const char* filename, int desired_color = 1 ); 20 21 /* Load rectangle from the file */ 22 virtual bool LoadRect( const char* filename, 23 int desired_color, CvRect r ); 24 25 #if defined WIN32 || defined _WIN32 26 virtual bool LoadRect( const char* filename, 27 int desired_color, RECT r ) 28 { 29 return LoadRect( filename, desired_color, 30 cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top )); 31 } 32 #endif 33 34 /* Save entire image to specified file. */ 35 virtual bool Save( const char* filename ); 36 37 /* Get copy of input image ROI */ 38 virtual void CopyOf( CvvImage& image, int desired_color = -1 ); 39 virtual void CopyOf( IplImage* img, int desired_color = -1 ); 40 41 IplImage* GetImage() { return m_img; }; 42 virtual void Destroy(void); 43 44 /* width and height of ROI */ 45 int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; }; 46 int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;}; 47 int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; }; 48 49 virtual void Fill( int color ); 50 51 /* draw to highgui window */ 52 virtual void Show( const char* window ); 53 54 #if defined WIN32 || defined _WIN32 55 /* draw part of image to the specified DC */ 56 virtual void Show( HDC dc, int x, int y, int width, int height, 57 int from_x = 0, int from_y = 0 ); 58 /* draw the current image ROI to the specified rectangle of the destination DC */ 59 virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect ); 60 #endif 61 62 protected: 63 64 IplImage* m_img; 65 }; 66 67 typedef CvvImage CImage; 68 69 #endif