摄像头抓取图片


接上篇文章: http://blog.csdn.net/luckyboy101/article/details/7843661

发现笔记本自带的摄像头不带color space filter, 视频color space是rgb32的,因此加不加color space filter也就不重要了

那么如何抓取图片呢,其实这个功能在transform filter已经很简单了(如果只是单纯的抓取图片,用inplace transform filter就够了),这里贴和上篇文章不同的部分,相同部分就不贴了。

HRESULT CAppTransform::Transform(IMediaSample *pIn, IMediaSample *pOut)
{
	HRESULT hr = Copy(pIn, pOut);
	if (FAILED(hr)) {
		return hr;
	}

	return Transform(pOut);


} // Transform
HRESULT CAppTransform::Transform(IMediaSample *pSample)
{
	// Override to do something inside the application
	// Such as grabbing a poster frame...
	// ...
	BYTE *pData;                // Pointer to the actual image buffer
	long lDataLen;              // Holds length of any given sample
	int iPixel;                 // Used to loop through the image pixels
	tagRGBQUAD *prgb;            // Holds a pointer to the current pixel

	AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
	VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
	ASSERT(pvi);

	CheckPointer(pSample,E_POINTER);
	pSample->GetPointer(&pData);
	lDataLen = pSample->GetSize();

	// Get the image properties from the BITMAPINFOHEADER

	cxImage    = pvi->bmiHeader.biWidth;
	cyImage    = pvi->bmiHeader.biHeight;
	int numPixels  = cxImage * cyImage;
	if(ISSHOT)
	{
		long lSourceSize = pSample->GetActualDataLength();
		BYTE *pSourceBuffer;
		pSample->GetPointer(&pSourceBuffer);
		if(pBuffer!=NULL)
			free(pBuffer);
		pBuffer=(BYTE *)malloc(lSourceSize*sizeof(BYTE));
		CopyMemory((PVOID) pBuffer,(PVOID) pSourceBuffer,lSourceSize);
		SnapshotToFile("hello");
		ISSHOT=FALSE;
	}

    return S_OK;
}
应用程序通过控制ISSHOT来赚钱图片,ISSHOT=TRUE,在Transform里就把当前帧给pBuffer.
BOOL CAppTransform::SnapshotToFile(const char * inFile)
{


	IplImage* pSrc;
	pSrc=cvCreateImage(cvSize(cxImage,cyImage),IPL_DEPTH_8U,3);
	tagRGBQUAD *prgb;
	prgb = (tagRGBQUAD*) pBuffer;
	for(int y=0;y<cyImage;y++)
	{
		for(int x=0;x<cxImage;x++)
		{
			((uchar*)(pSrc->imageData+y*pSrc->widthStep))[x*pSrc->nChannels]=prgb->rgbBlue;
			((uchar*)(pSrc->imageData+y*pSrc->widthStep))[x*pSrc->nChannels+1]=prgb->rgbGreen;
			((uchar*)(pSrc->imageData+y*pSrc->widthStep))[x*pSrc->nChannels+2]=prgb->rgbRed;
			prgb++;
		}
	}
	cvSaveImage("D:\\test.bmp",pSrc);

	return TRUE;
}
把pBuffer里面的数据存成图片,我们采用opencv的iplimage来保存,因为支持保存成各种图片格式。


你可能感兴趣的:(摄像头抓取图片)