opencv-hbitmap在内存中直接转jgp格式-bmp转jpg

opencv-hbitmap在内存中直接转jgp格式-bmp转jpg

IplImage* CMainFrame::Bitmap2IplImage(HANDLE hBitmap)
{
	BITMAP bmp; // 得到位图对象
	int nPicSize;
	nPicSize = sizeof(BITMAP);
	GetObject(hBitmap, sizeof(BITMAP), &bmp);

	int depth,nChannels;
	if(bmp.bmBitsPixel == 1)//得到图像深度和通道数
	{
		depth=IPL_DEPTH_1U;
		nChannels=1;
	}
	else
	{
		depth=IPL_DEPTH_8U;
		nChannels=bmp.bmBitsPixel/8;
	}
	long	nBuffer = bmp.bmHeight*bmp.bmWidth*nChannels;
	IplImage* pImg = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight), depth, nChannels); //创建图像
	BYTE *pBuffer = new BYTE[nBuffer]; //创建缓冲区
	//GetBitmapBits(hBmp, bmp.bmHeight*bmp.bmWidth*nChannels, pBuffer); //将位图信息复制到缓冲区
	GetBitmapBits((HBITMAP)hBitmap, nBuffer, pBuffer); //将位图信息复制到缓冲区
	memcpy(pImg->imageData, pBuffer, nBuffer);//将缓冲区信息复制给IplImage
	delete [] pBuffer;//防止内存泄露
	//cvSaveImage("kkk.jpg",pImg,0);
return pImg;
}

Leave a Reply