⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 screenchattingclientdlg.cpp

📁 Screen Chatting source
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	if (!pWinBmpEx) return ;

	BYTE *pJpegData;
	CJPEGEncoderEx Encoder;

	int nBufferSize=Encoder.GetJpegData(pWinBmpEx,&pJpegData);
	SendFrame(pJpegData,nBufferSize);

	delete pWinBmpEx;
	delete pJpegData;

	if (i==0)
	{
		FARPROC fpErrorCallback=MakeProcInstance((FARPROC)ErrorCallbackProc,AfxGetInstanceHandle());
		FARPROC fpYieldCallback=MakeProcInstance((FARPROC)YieldCallbackProc,AfxGetInstanceHandle());    
		FARPROC fpStatusCallback=MakeProcInstance((FARPROC)StatusCallbackProc,AfxGetInstanceHandle());

		m_pVFWWrapper->SetHandler(fpErrorCallback,fpYieldCallback,fpStatusCallback);
	}

	i++;
*/
	CDialog::OnTimer(nIDEvent);
}

void CScreenChattingClientDlg::OnProgress(double Part)
{

}

LRESULT CScreenChattingClientDlg::ErrorCallbackProc(HWND hWnd,int nErrID,LPSTR lpErrorText)
{
/*
	if (!ghWndMain) return FALSE;
    if (nErrID==0) return TRUE;

    lstrcpy(gachLastError, lpErrorText);
*/
    AfxMessageBox(lpErrorText);

    return (LRESULT) TRUE ;
}

LRESULT CScreenChattingClientDlg::StatusCallbackProc(HWND hWnd,int nID,LPSTR lpStatusText)
{
	static int CurrentID;
/*
    if (!ghWndMain) 
	{
        return FALSE;
    }
*/
    if (nID == IDS_CAP_END) 
	{
        if ((CurrentID==IDS_CAP_STAT_VIDEOAUDIO)||(CurrentID==IDS_CAP_STAT_VIDEOONLY)) 
		{
            return(TRUE);
        }
    }
    CurrentID=nID;

    return (LRESULT)TRUE;
}

LRESULT CScreenChattingClientDlg::YieldCallbackProc(HWND hWnd)
{
    MSG msg;

    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) 
	{
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (LRESULT) TRUE;
}

BOOL CScreenChattingClientDlg::SendFrame(BYTE *pJpegData, int nBufferSize)
{ 
	m_pSendFrameSocket->Send(pJpegData,nBufferSize);

	return TRUE;
}

void CScreenChattingClientDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	delete m_pSendFrameSocket;
}

BOOL CScreenChattingClientDlg::ReceiveJpegData(BYTE *pJpegData, int nBufferSize)
{
	CString strMsg;
	int iWidth,iHeight;

	HANDLE hDIB=JpegBufferToDib(pJpegData,nBufferSize,&strMsg,iWidth,iHeight);

	BITMAPFILEHEADER	hdr;
	LPBITMAPINFOHEADER	lpbi;

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	int nColors;

	if (lpbi->biBitCount>=16) nColors=0;
	else nColors= 1 << lpbi->biBitCount;

	// Fill in the fields of the file header 
	hdr.bfType		= ((WORD) ('M' << 8) | 'B');	// is always "BM"
	hdr.bfSize		= GlobalSize (hDIB) + sizeof( hdr );//57640+sizeof(hdr);//GlobalSize (hDIB) + sizeof( hdr );
	hdr.bfReserved1 	= 0;
	hdr.bfReserved2 	= 0;
	hdr.bfOffBits		= (DWORD) (sizeof( hdr ) + lpbi->biSize +
						nColors * sizeof(RGBQUAD));

	CMyDib *pMyDib=new CMyDib;
	pMyDib->bLoadBmpBuffer(&hdr,hDIB);

	CWnd *pWndChatting=GetDlgItem(IDS_CHATTINGWINDOW);
	
	CRect rect;

	pWndChatting->GetWindowRect(&rect);

	CWindowDC *pWindowDC=new CWindowDC(pWndChatting);
	
	int iWndWidth=rect.Width();
	int iWndHeight=rect.Height();

	pMyDib->PaintImage(iWndWidth/2-iWidth/2,iWndHeight/2-iHeight/2,iWidth,iHeight,pWindowDC->m_hDC);
	
	delete pWindowDC;
	delete pMyDib;

	GlobalFree(hDIB);

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////
//This function takes the contents of a DIB
//and turns it into a JPEG file.
//
//The DIB may be monochrome, 16-color, 256-color, or 24-bit color.
//
//Any functions or data items beginning with "jpeg_" belong to jpeg.lib,
//and are not included here.
//
//The function assumes 3 color components per pixel.
/////////////////////////////////////////////////////////////////////////////
HANDLE JpegBufferToDib(BYTE *pJpegData,
				int nBufferSize,
                CString*   pcsMsg,int &iWidth, int &iHeight)   //Error msg to return
{

	// Jo Hagelberg 15.4.99: added progress notification callback
    JMETHOD( void, notify, (j_common_ptr));
    notify = JNotification;

	 int nDestBPP=24;

    jpeg_decompress_struct cinfo;  // IJPEG decoder state.
	jpeg_error_mgr         jerr;   // Custom error manager.

	cinfo.err = jpeg_std_error (&jerr);
    jerr.error_exit = error_exit;  // Register custom error manager.
    jpeg_create_decompress (&cinfo);

    jpeg_mem_src (&cinfo, pJpegData,
                  nBufferSize,notify);

    jpeg_read_header (&cinfo, TRUE);

    cinfo.do_fancy_upsampling = TRUE;

    // Choose floating point DCT method.
    cinfo.dct_method = JDCT_FLOAT;

    int w = cinfo.image_width;
    int h = cinfo.image_height;

    jpeg_start_decompress (&cinfo);
	
	BYTE *pByte;

    if (cinfo.out_color_space == JCS_GRAYSCALE)
      pByte=decodeGray ( &cinfo,w, h, &nDestBPP);
    else
      pByte=decodeRGB ( &cinfo,w, h,  &nDestBPP);

    jpeg_finish_decompress (&cinfo);
  
	long lSize=GetBitsMemNeeded (w, h, nDestBPP);

	CBitmap *pMemBitmap=new CBitmap;
	pMemBitmap->CreateBitmap(w,h,1,nDestBPP,pByte);

	pByte=BGRToRGB(pByte,lSize);
	HANDLE hDIB=DDBToDIB(*pMemBitmap,BI_RGB,NULL,pByte);

	iWidth=w;
	iHeight=h;

	delete pMemBitmap;
	delete pByte;

	return hDIB;
}

BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
	BITMAPFILEHEADER	hdr;
	LPBITMAPINFOHEADER	lpbi;

	if (!hDIB)
		return FALSE;

	CFile file;
	if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
		return FALSE;

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	int nColors;

	if (lpbi->biBitCount>=16) nColors=0;
	else nColors= 1 << lpbi->biBitCount;

	// Fill in the fields of the file header 
	hdr.bfType		= ((WORD) ('M' << 8) | 'B');	// is always "BM"
	hdr.bfSize		= GlobalSize (hDIB) + sizeof( hdr );//57640+sizeof(hdr);//GlobalSize (hDIB) + sizeof( hdr );
	hdr.bfReserved1 	= 0;
	hdr.bfReserved2 	= 0;
	hdr.bfOffBits		= (DWORD) (sizeof( hdr ) + lpbi->biSize +
						nColors * sizeof(RGBQUAD));

	// Write the file header 
	file.Write( &hdr, sizeof(hdr) );

	// Write the DIB header and the bits 
	file.Write( lpbi,  GlobalSize(hDIB));
	file.Close();

	return TRUE;
}

HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal ,BYTE *pData)
{
	BITMAP bm;
	BITMAPINFOHEADER bi;
	LPBITMAPINFOHEADER lpbi;
	DWORD dwLen;
	HANDLE hDIB;
	HANDLE handle;
	HDC hDC;
	HPALETTE hPal;

	ASSERT( bitmap.GetSafeHandle() );

	// The function has no arg for bitfields
	if ( dwCompression == BI_BITFIELDS )
		return NULL;

	// If a palette has not been supplied use defaul palette
	hPal = (HPALETTE) pPal->GetSafeHandle();
	if (hPal==NULL)
		hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

	// Get bitmap information
	bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

	// Initialize the bitmapinfoheader
	bi.biSize               = sizeof(BITMAPINFOHEADER);
	bi.biWidth              = bm.bmWidth;
	bi.biHeight             = bm.bmHeight;
	bi.biPlanes             = 1;
	bi.biBitCount           = bm.bmPlanes * bm.bmBitsPixel;
	bi.biCompression        = dwCompression;
	bi.biSizeImage          = 0;
	bi.biXPelsPerMeter      = 0;
	bi.biYPelsPerMeter      = 0;
	bi.biClrUsed            = 0;
	bi.biClrImportant       = 0;

	// Compute the size of the  infoheader and the color table
	int nColors = (1 << bi.biBitCount);
	if ( nColors > 256 ) 
		nColors = 0;
	dwLen = bi.biSize + nColors * sizeof(RGBQUAD);

	// We need a device context to get the DIB from
	hDC = ::GetDC(NULL);
	hPal = SelectPalette(hDC,hPal,FALSE);
	RealizePalette(hDC);

	// Allocate enough memory to hold bitmapinfoheader and color table
	hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

	if (!hDIB)
	{
		SelectPalette(hDC,hPal,FALSE);
		::ReleaseDC(NULL,hDC);
		return NULL;
	}

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	*lpbi = bi;

	// Call GetDIBits with a NULL lpBits param, so the device driver 
	// will calculate the biSizeImage field 
	GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
		(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

	bi = *lpbi;

	// If the driver did not fill in the biSizeImage field, then compute it
	// Each scan line of the image is aligned on a DWORD (32bit) boundary
	if (bi.biSizeImage == 0)
	{
		bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
			* bi.biHeight;

		// If a compression scheme is used the result may infact be larger
		// Increase the size to account for this.
		if (dwCompression != BI_RGB)
			bi.biSizeImage = (bi.biSizeImage * 3) / 2;
	}

	// Realloc the buffer so that it can hold all the bits
	dwLen += bi.biSizeImage;
	if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
	{
		hDIB = handle;
		GlobalLock(hDIB);
	}
	else
	{
		GlobalFree(hDIB);

		// Reselect the original palette
		SelectPalette(hDC,hPal,FALSE);
		::ReleaseDC(NULL,hDC);
		return NULL;
	}

	// Get the bitmap bits
	lpbi = (LPBITMAPINFOHEADER)hDIB;

	LPBYTE lpByte1=(LPBYTE)lpbi;
	// FINALLY get the DIB
	BOOL bGotBits=TRUE;
	LPBYTE lpByte2=(LPBYTE)(lpByte1+(bi.biSize+nColors*sizeof(RGBQUAD)));
	
	memcpy(lpByte2,pData,dwLen-bi.biSize);

	if( !bGotBits )
	{
		GlobalFree(hDIB);

		SelectPalette(hDC,hPal,FALSE);
		::ReleaseDC(NULL,hDC);
		return NULL;
	}

	SelectPalette(hDC,hPal,FALSE);
	::ReleaseDC(NULL,hDC);
	return hDIB;
}
/*
 * Prepare for input. This routine tells the jpeg library where to find 
 * the data & sets up the function pointers the library needs.
 *
 * Jo Hagelberg 15.4.99
 * added pDataSrc and JMETHOD( notifyCppworld ) for progress notification 
 */

void jpeg_mem_src (
              j_decompress_ptr cinfo,
              JOCTET * pData,
              int FileSize,
			  JMETHOD(void, notifyCppWorld, (j_common_ptr))
             )
{
  struct jpeg_source_mgr * src;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -