📄 jpeg.cpp
字号:
#include "stdafx.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>
#include <windows.h> // std windows defs, etc
#include <wininet.h> // Windows Internet SDK api
#include <winsock.h>
#include <Objbase.h>
#include "imgdecmp.h" //Bridge
#include "AllCam.h"
HINTERNET hInternetSession;
HINTERNET hInternetConnect;
HINTERNET hResourceHandle;
DWORD dwGLE;
#define URLBUFFERSIZE 80000
char *pBuffer;
DWORD dwBytesRead;
HINTERNET hConnect;
HINTERNET hFile;
#define LocalJpegFile _T("\\windows\\theJpeg.jpg")
BOOL StartJpegConnection();
void EndJpegConnection();
BOOL ReadJpeg();
void DisplayJpeg(HWND hWnd);
static DWORD CALLBACK GetImageData( LPSTR , DWORD , LPARAM ); //Callback for reading data
static void CALLBACK ImageProgress( IImageRender *, BOOL , LPARAM ); //Callback for completion
extern CCamDesc *pCurCam;
BOOL StartJpegConnection()
{
EndJpegConnection();
hInternetSession= ::InternetOpen (_T("Camera Monitor"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
// if no session then return error code
if (hInternetSession == NULL) { dwGLE = GetLastError(); return FALSE; }
//////////////////////
TCHAR ip[255];
int port;
pCurCam ->getIPandPort (ip, port);
hInternetConnect = ::InternetConnect (hInternetSession, ip ,
port ,
NULL , NULL,
INTERNET_SERVICE_HTTP, 0, 0);
if(hInternetConnect ==NULL)
{
EndJpegConnection();
return FALSE;
}
//////////////////////////////
pBuffer = (char *)malloc(URLBUFFERSIZE); // we need a buffer to read into
return TRUE;
}
BOOL ReadJpeg()
{
TCHAR objectName[255];
pCurCam->getObjectName (objectName);
hResourceHandle = HttpOpenRequest(hInternetConnect, L"GET", objectName,
NULL, NULL, NULL,
// INTERNET_FLAG_RESYNCHRONIZE |
INTERNET_FLAG_NO_CACHE_WRITE,
// INTERNET_FLAG_KEEP_CONNECTION,
0);
if(hResourceHandle ==NULL) return FALSE;
///////////////////////////
DWORD dwStatus, dwStatusSize;
dwStatusSize = sizeof(dwStatus);
do
{
HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
::HttpQueryInfo (hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE , &dwStatus, &dwStatusSize, NULL);
switch(dwStatus)
{
case HTTP_STATUS_PROXY_AUTH_REQ:
::InternetSetOption (hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, pCurCam->Username , _tcslen(pCurCam->Username)+1);
::InternetSetOption (hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, pCurCam->Password , _tcslen(pCurCam->Password)+1);
break;
case HTTP_STATUS_DENIED:
::InternetSetOption (hResourceHandle, INTERNET_OPTION_USERNAME, pCurCam->Username , _tcslen(pCurCam->Username)+1);
::InternetSetOption (hResourceHandle, INTERNET_OPTION_PASSWORD, pCurCam->Password , _tcslen(pCurCam->Password)+1);
break;
default:
break;
}
} while(dwStatus == HTTP_STATUS_PROXY_AUTH_REQ || dwStatus == HTTP_STATUS_DENIED);
////////////////////////////////////
HANDLE handle = INVALID_HANDLE_VALUE;
while(handle == INVALID_HANDLE_VALUE)
handle= ::CreateFile (LocalJpegFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while( 1 )
{
InternetQueryDataAvailable(hResourceHandle,&dwBytesRead,0,0);
if(dwBytesRead==0) break;
InternetReadFile( hResourceHandle , pBuffer, (DWORD)dwBytesRead, &dwBytesRead);
::WriteFile (handle, pBuffer, dwBytesRead, &dwBytesRead, NULL);
}
while(::CloseHandle (handle)==0) ;
while(InternetCloseHandle(hResourceHandle)==FALSE) ; hResourceHandle = NULL;
return TRUE;
}
void EndJpegConnection()
{
if(hResourceHandle) {
while(InternetCloseHandle(hResourceHandle)==FALSE) ; hResourceHandle = NULL;
}
if(hInternetConnect) {
while(InternetCloseHandle(hInternetConnect)==FALSE) ; hInternetConnect = NULL;
}
if(hInternetSession) {
while(InternetCloseHandle(hInternetSession)==FALSE) ; hInternetSession = NULL;
}
////////////////////////////////
if(pBuffer!=NULL) { free(pBuffer); pBuffer = NULL; }
}
void DisplayJpeg(HWND hWnd)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
hFile = CreateFile(LocalJpegFile , GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile == INVALID_HANDLE_VALUE) return;
DecompressImageInfo dii;
BYTE szBuffer[4096] = {0};
HBITMAP hImgBitmap = NULL;
HDC hdc = GetDC(hWnd);
//Fill in the 'DecompressImageInfo' structure
dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
dii.dwBufferMax = 4096; // Size of the buffer
dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
dii.phBM = &hImgBitmap; // Pointer to the bitmap returned (can be NULL)
dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image
dii.lParam = ( LPARAM ) hFile; // User parameter for callback functions
dii.hdc = hdc; // HDC to use for retrieving palettes
dii.iScale = 100; // Scale factor (1 - 100)
dii.iMaxWidth = 10000; // Maximum width of the output image
dii.iMaxHeight = 10000; // Maxumum height of the output image
dii.pfnGetData = GetImageData; // Callback function to get image data
dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the
// transparent color in the image with this color. (GIF ONLY)
HRESULT hr = DecompressImageIndirect( &dii ); //Call DecompressImageIndirect
while(CloseHandle( hFile )==0) ; // Clean up
////////////////////////////////////
//Select the bitmap into the memory device context
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem,hImgBitmap);
BITMAP bmp;
GetObject(hImgBitmap, sizeof(BITMAP), &bmp);
RECT r;
r.left =34; r.top =7; r.right = bmp.bmWidth ; r.bottom =bmp.bmHeight ;
::StretchBlt(hdc,0,0,240,200,hdcMem,0,0,r.right ,r.bottom,SRCCOPY);
SelectObject(hdcMem,hOldBitmap);
DeleteDC(hdcMem);
DeleteObject(hImgBitmap); hImgBitmap=NULL;
ReleaseDC(hWnd, hdc);
InvalidateRect(hWnd, &r, FALSE); //Invalidate the window to update it
}
//Step 5: Create the Callback functions
//---------------------------------------------------------------------
// GetImageData
//---------------------------------------------------------------------
static DWORD CALLBACK GetImageData( LPSTR szBuffer, DWORD dwBufferMax, LPARAM lParam )
{
DWORD dwNumberOfBytesRead;
if ( (HANDLE)lParam != INVALID_HANDLE_VALUE )
{
//graphics file
ReadFile( (HANDLE)lParam, szBuffer, dwBufferMax, &dwNumberOfBytesRead, NULL );
}
else
{
return 0;
}
return dwNumberOfBytesRead;
// return amount read
}
//---------------------------------------------------------------------
// ImageProgress
//---------------------------------------------------------------------
static void CALLBACK ImageProgress( IImageRender *pRender, BOOL bComplete, LPARAM lParam )
{
if( bComplete )
{
;//Do whatever when done processing the image
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -