用C ++拍摄视频(vga)相机的照片 [英] Take a picture of the video(vga) camera in C++

查看:73
本文介绍了用C ++拍摄视频(vga)相机的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多关于如何用C#拍照的回复,但是我需要用C ++拍摄这张照片,我用它作为随SDK提供的Skeletal Viewer的基础。目前我正在这样做:


 



 

 
void CSkeletalViewerApp :: Nui_GotVideoAlert()

{

const NUI_IMAGE_FRAME * pImageFrame = NULL;

float timestamp = NULL;

char * filename = NULL;



HRESULT hr = NuiImageStreamGetNextFrame(

m_pVideoStreamHandle,

0,

& ; pImageFrame);

if (FAILED(hr))

{

返回;

}



NuiImageBuffer * pTexture = pImageFrame-> pFrameTexture;

KINECT_LOCKED_RECT LockedRect;

pTexture-> LockRect(0,& LockedRect,NULL,0);



filename = " 5.bmp" ;



FILE * pFile = fopen(文件名," wb" );



BITMAPINFOHEADER BMIH;



BMIH.biSize = sizeof (BITMAPINFOHEADER);

BMIH.biBitCount = 24;

BMIH.biPlanes = 0;

BMIH.biCompression = 0;

BMIH.biWidth = 640;

BMIH.biHeight = 480;

BMIH.biSizeImage =((((BMIH.biWidth * BMIH.biBitCount)

+ 31)& ~31)>> 3)* BMIH.biHeight ;



BITMAPFILEHEADER bmfh;



int nBitsOffset = sizeof (BITMAPFILEHEADER)+ BMIH.biSize;

LONG lImageSize = BMIH.biSizeImage;

LONG lFileSize = nBitsOffset + lImageSize;

bmfh.bfType ='B'+('M'<< 8);

bmfh.bfOffBits = nBitsOffset;

bmfh.bfSize = lFileSize;

bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

//写位图文件头



UINT nWrittenFileHeaderSize = fwrite (& bmfh,1,

sizeof (BITMAPFILEHEADER),pFile);

//然后是位图信息标题



UINT nWrittenInfoHeaderSize = fwrite(& BMIH,

1, sizeof (BITMAPINFOHEADER),pFile);

//最后,自己编写图像数据



// - 数据代表我们的绘图









if (LockedRect.Pitch!= 0)

{

BYTE * pBuffer =(BYTE *)LockedRect.pBits;



m_DrawVideo.DrawFrame((BYTE *)pBuffer);





}

else

{

OutputDebugString(L "接收纹理的缓冲区长度为bogus \\\\ nn" );

}

UINT nWrittenDIBDataSize =

fwrite(pImageFrame-> pFrameTexture,1,BMIH.biSizeImage,pFile);



NuiImageStreamReleaseFrame(m_pVideoStreamHandle,pImageFrame);

fclose(pFile);

}

解决方案

Lino,


返回给你的数据实际上是每像素32位,即使其中只有24位有信息。此外,您应该使用LockedRect.pBits来编写图像数据,而不是pImageFrame-pFrameTexture:


 char * filename = filename = " snapshot.bmp英寸; 
FILE * pFile = NULL;
fopen_s(& pFile,filename," wb");

BITMAPINFOHEADER BMIH = {0};
BMIH.biSize = sizeof(BITMAPINFOHEADER);
BMIH.biBitCount = 32;
BMIH.biPlanes = 1;
BMIH.biCompression = BI_RGB;
BMIH.biWidth = 640;
BMIH.biHeight = 480;
BMIH.biSizeImage =((((BMIH.biWidth * BMIH.biBitCount)+ 31)& ~31)>> 3)* BMIH.biHeight;

BITMAPFILEHEADER bmfh = {0};
int nBitsOffset = sizeof(BITMAPFILEHEADER)+ BMIH.biSize;
LONG lImageSize = BMIH.biSizeImage;
LONG lFileSize = nBitsOffset + lImageSize;
bmfh.bfType ='B'+('M'<< 8);
bmfh.bfOffBits = nBitsOffset;
bmfh.bfSize = lFileSize;

//写位图文件头
fwrite(& bmfh,1,sizeof(BITMAPFILEHEADER),pFile);

//然后是位图信息标题
fwrite(& BMIH,1,sizeof(BITMAPINFOHEADER),pFile);

//最后,写出图像数据本身
fwrite(LockedRect.pBits,1,BMIH.biSizeImage,pFile);

fclose(pFile);




希望这会有所帮助!

Eddy


I've seen a lot of replies of how to take a picture in C#, but I would need to take this picture in C++, I'm using as a base the Skeletal Viewer provided with the SDK. At the moment I'm doing this:

 

 

void CSkeletalViewerApp::Nui_GotVideoAlert( )

{

    const NUI_IMAGE_FRAME * pImageFrame = NULL;

	float timestamp=NULL;

	char *filename=NULL;



    HRESULT hr = NuiImageStreamGetNextFrame(

        m_pVideoStreamHandle,

        0,

        &pImageFrame );

    if( FAILED( hr ) )

    {

        return;

    }



    NuiImageBuffer * pTexture = pImageFrame->pFrameTexture;

    KINECT_LOCKED_RECT LockedRect;

    pTexture->LockRect( 0, &LockedRect, NULL, 0 );



		filename="5.bmp";



	    FILE *pFile = fopen(filename, "wb");



		BITMAPINFOHEADER BMIH;



		BMIH.biSize = sizeof(BITMAPINFOHEADER);

        BMIH.biBitCount = 24;

        BMIH.biPlanes = 0;

        BMIH.biCompression = 0;

        BMIH.biWidth = 640;

        BMIH.biHeight = 480;

        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount) 

                           + 31) & ~31) >> 3) * BMIH.biHeight;



        BITMAPFILEHEADER bmfh;



        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize; 

        LONG lImageSize = BMIH.biSizeImage;

        LONG lFileSize = nBitsOffset + lImageSize;

        bmfh.bfType = 'B'+('M'<<8);

        bmfh.bfOffBits = nBitsOffset;

        bmfh.bfSize = lFileSize;

        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

        //Write the bitmap file header



        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1, 

                     sizeof(BITMAPFILEHEADER), pFile);

        //And then the bitmap info header



        UINT nWrittenInfoHeaderSize = fwrite(&BMIH, 

               1, sizeof(BITMAPINFOHEADER), pFile);

        //Finally, write the image data itself 



        //-- the data represents our drawing









    if( LockedRect.Pitch != 0 )

    {

        BYTE * pBuffer = (BYTE*) LockedRect.pBits;



        m_DrawVideo.DrawFrame( (BYTE*) pBuffer );





    }

    else

    {

        OutputDebugString( L"Buffer length of received texture is bogus\r\n" );

    }

			UINT nWrittenDIBDataSize = 

				fwrite(pImageFrame->pFrameTexture, 1,BMIH.biSizeImage, pFile);



    NuiImageStreamReleaseFrame( m_pVideoStreamHandle, pImageFrame );

	fclose(pFile);

}

解决方案

Lino,

The data returned to you is actually 32-bits per pixel, even if only 24 of those have information. Also, you should use LockedRect.pBits to write image data, rather than pImageFrame-pFrameTexture:

char *filename=filename="snapshot.bmp";
FILE *pFile = NULL;
fopen_s(&pFile, filename, "wb");
	
BITMAPINFOHEADER BMIH = {0};
BMIH.biSize = sizeof(BITMAPINFOHEADER);
BMIH.biBitCount = 32;
BMIH.biPlanes = 1;
BMIH.biCompression = BI_RGB;
BMIH.biWidth = 640;
BMIH.biHeight = 480;
BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * BMIH.biHeight;

BITMAPFILEHEADER bmfh = {0};
int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize; 
LONG lImageSize = BMIH.biSizeImage;
LONG lFileSize = nBitsOffset + lImageSize;
bmfh.bfType = 'B'+('M'<<8);
bmfh.bfOffBits = nBitsOffset;
bmfh.bfSize = lFileSize;

//Write the bitmap file header
fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), pFile);

//And then the bitmap info header
fwrite(&BMIH, 1, sizeof(BITMAPINFOHEADER), pFile);

//Finally, write the image data itself
fwrite(LockedRect.pBits, 1,BMIH.biSizeImage, pFile);

fclose(pFile);


Hope this helps!
Eddy


这篇关于用C ++拍摄视频(vga)相机的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆