📄 cameratest.cpp
字号:
// CameraTest.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "CameraTest.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
PVOID VirtualAllocCopy(unsigned size,char *str,PVOID pVirtualAddress);
#define QUERYESCSUPPORT 8
// OEM escape code base
#define ESCAPECODEBASE 100000
#define DISPLAYPAGE (ESCAPECODEBASE + 1)
#define GETPALETTERAMPHYSICAL (ESCAPECODEBASE + 2)
#define VERTICALBLANKINTERRUPT (ESCAPECODEBASE + 3)
#define OS_SCREENACCESS (ESCAPECODEBASE + 4)
#define SCROLL (ESCAPECODEBASE + 5)
#define OVERLAY2_ENABLE (ESCAPECODEBASE + 6)
#define OVERLAY2_DISABLE (ESCAPECODEBASE + 7)
#define OVERLAY1_ENABLE (ESCAPECODEBASE + 8)
#define OVERLAY1_DISABLE (ESCAPECODEBASE + 9)
#define GET_OVERLAY1_ADDRESS (ESCAPECODEBASE + 10)
#define GET_OVERLAY2_ADDRESS (ESCAPECODEBASE + 11)
#define CAMERA_INITIALIZE (ESCAPECODEBASE + 12)
#define CAMERA_START_VIDEO_CAPTURE (ESCAPECODEBASE + 13)
#define CAMERA_STOP_VIDEO_CAPTURE (ESCAPECODEBASE + 14)
#define CAMERA_RELEASE_FRAME (ESCAPECODEBASE + 15)
// Use the following for configuring YUV overlay format
#define FORMAT_RGB 0x0
#define FORMAT_PACKED_444 0x1
#define FORMAT_PLANAR_444 0x2
#define FORMAT_PLANAR_422 0x3
#define FORMAT_PLANAR_420 0x4
#define CAMERA_EVENT_NAME _T("CameraFrameComplete")
typedef struct
{
unsigned long OverlayHeight; // app fills this in
unsigned long OverlayWidth; // app fills this in
unsigned long X_Position; // app fills this in
unsigned long Y_Position; // app fills this in
unsigned long Format; // app fills this in
unsigned long DegradeBaseFrame; // app fills this in
unsigned long CH2_Y; // driver fills this in
unsigned long CH3_Cb; // driver fills this in
unsigned long CH4_Cr; // driver fills this in
unsigned long OverlayBPP; // driver fills this in
unsigned long TmpBPP; // driver fills this in
unsigned long ch2_size; // driver fills this in
unsigned long ch3_size; // driver fills this in
unsigned long ch4_size; // driver fills this in
} XLLP_OVERLAY_T, *P_XLLP_OVERLAY_T;
#define MAX_FRAMES 3
typedef struct
{
unsigned long VideoWidth; // app fills this in
unsigned long VideoHeight; // app fills this in
unsigned long StillWidth; // app fills this in
unsigned long StillHeight; // app fills this in
unsigned long CaptureFormat; // app fills this in
unsigned long FrameRate; // app fills this in
void *ring_buffer_address[MAX_FRAMES]; // driver fills this in
BOOL bFrame_available; // driver maintains this
} CAMERA_APP_T, *P_CAMERA_APP_T;
XLLP_OVERLAY_T XllpOverlay;
CAMERA_APP_T CameraApp;
BOOL bDegraded=FALSE;
BOOL bCapturingVideo = FALSE;
unsigned char *fbpY;
unsigned char *fbpCr;
unsigned char *fbpCb;
PBYTE pOverlay2_Y = NULL;
PBYTE pOverlay2_Cb = NULL;
PBYTE pOverlay2_Cr = NULL;
unsigned int frameCount = 0;
void *ring_buffer_address[MAX_FRAMES];
HANDLE hEvent = NULL;
HANDLE hCameraHandle ;
// "Start video capture"
void CameraCaptureThread(long *p)
{
int cbInput, cbOutput;
cbInput = sizeof(XLLP_OVERLAY_T);
cbOutput = 0;
CeSetThreadPriority(GetCurrentThread(), 1);
bCapturingVideo = TRUE;
// The driver has filled in the ring_buffer_address[] to use
// Now we can start video capture and wait to be signalled to start processing buffers
// The camera driver has created an event specified by CAMERA_EVENT_NAME.
// Each time this event is signalled, that means we have at least one new frame of video to process.
// Because the application processing of the frame is inherently asynchronous to the actual frame capture,
// a ring buffer is used to store the video frames in order to buffer the data source from the data sink.
// Because the image acquisition may run faster than our application's ability to process the frames,
// we need to see if more than one frame is available to us each time we are signalled.
if (hEvent)
{
// Give command to start video capture
//ExtEscape(hDC, CAMERA_START_VIDEO_CAPTURE, cbInput, (LPCSTR)&CameraApp, cbOutput, NULL);
DeviceIoControl(hCameraHandle,CAMERA_START_VIDEO_CAPTURE,&CameraApp,sizeof(CameraApp),NULL,0,0,NULL) ;
while(bCapturingVideo)
{
WaitForSingleObject(hEvent, INFINITE);
// Received notification that a frame is ready, let's do something with it.
// Read out all frames that are waiting in the buffer ( while bFrame_available )
do
{
// Copy a frame to the YCrCb overlay to display it, or MPEG compress it and stuff it out the network, or...
//memcpy(fbpY, ring_buffer_address[frameCount], XllpOverlay.ch2_size);
//memcpy(fbpCb, (void *)((BYTE *)ring_buffer_address[frameCount] + XllpOverlay.ch2_size), XllpOverlay.ch3_size);
//memcpy(fbpCr, (void *)((BYTE *)ring_buffer_address[frameCount] + XllpOverlay.ch2_size + XllpOverlay.ch3_size), XllpOverlay.ch4_size);
memcpy(fbpY, ring_buffer_address[frameCount], XllpOverlay.ch2_size);
memcpy(fbpCb, (void *)((BYTE *)ring_buffer_address[frameCount] + XllpOverlay.ch2_size), XllpOverlay.ch3_size);
memcpy(fbpCr, (void *)((BYTE *)ring_buffer_address[frameCount] + XllpOverlay.ch2_size + XllpOverlay.ch3_size), XllpOverlay.ch4_size);
// Now release this frame back to the buffer pool.
//ExtEscape(hDC, CAMERA_RELEASE_FRAME, cbInput, (LPCSTR)&CameraApp, cbOutput, NULL);
DeviceIoControl(hCameraHandle,CAMERA_RELEASE_FRAME,&CameraApp,sizeof(CameraApp),NULL,0,0,NULL) ;
// maintain the frame counter
frameCount = (frameCount + 1) % MAX_FRAMES;
} while(CameraApp.bFrame_available);
}
// call the stop capture function... bCapturingVideo set externally to this function
//ExtEscape(hDC, CAMERA_STOP_VIDEO_CAPTURE, cbInput, (LPCSTR)&CameraApp, cbOutput, NULL);
DeviceIoControl(hCameraHandle,CAMERA_STOP_VIDEO_CAPTURE,&CameraApp,sizeof(CameraApp),NULL,0,0,NULL) ;
}
}
void StartCapture()
{
HANDLE hthrd;
int cbInput, cbOutput;
cbInput = 0;
cbOutput = 0;
cbInput = sizeof(XLLP_OVERLAY_T);
if (bCapturingVideo == FALSE)
{
//ExtEscape(hDC, OVERLAY2_ENABLE, cbInput, (LPCSTR)&XllpOverlay, cbOutput, NULL);
DeviceIoControl(hCameraHandle,OVERLAY2_ENABLE,&XllpOverlay,sizeof(XllpOverlay),NULL,0,0,NULL) ;
hthrd = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)CameraCaptureThread,NULL,0,NULL);
CloseHandle(hthrd);
}
}
void StopCapture()
{
int cbInput, cbOutput;
cbInput = 0;
cbOutput = 0;
cbInput = sizeof(XLLP_OVERLAY_T);
// can't disable when the L clk is too high
//ExtEscape(hDC, OVERLAY2_DISABLE, cbInput, (LPCSTR)&XllpOverlay, cbOutput, NULL);
DeviceIoControl(hCameraHandle,OVERLAY2_DISABLE,&XllpOverlay,sizeof(XllpOverlay),NULL,0,0,NULL) ;
bCapturingVideo = FALSE;
}
void CaptureInit()
{
int cbInput, cbOutput, i;
DWORD iReturn ;
cbInput = sizeof(XLLP_OVERLAY_T);
cbOutput = 0;
//open the camera
hCameraHandle = CreateFile(TEXT("CAM1:"),0,0,NULL,OPEN_EXISTING,0,NULL) ;
if ( INVALID_HANDLE_VALUE == hCameraHandle )
{
RETAILMSG(1,(TEXT("error=%d\r\n"),GetLastError()));
RETAILMSG(1,(TEXT("Open camera failed-------\r\n"))) ;
}
// Fill in the overlay parameters, then query for the dynamic address of the overlay channels
XllpOverlay.OverlayWidth = 320;//224;
XllpOverlay.OverlayHeight = 240;//240;
XllpOverlay.X_Position = 160;//(240 - XllpOverlay.OverlayWidth) / 2;
XllpOverlay.Y_Position = 28;
XllpOverlay.Format = FORMAT_PLANAR_422;
XllpOverlay.DegradeBaseFrame = bDegraded;
//ExtEscape(hDC, GET_OVERLAY2_ADDRESS, cbInput, (LPCSTR)&XllpOverlay, cbOutput, NULL);
BOOL bRet =DeviceIoControl(hCameraHandle,GET_OVERLAY2_ADDRESS,&XllpOverlay,sizeof(XLLP_OVERLAY_T),NULL,0,&iReturn,NULL) ;
if ( !bRet )
{
RETAILMSG(1,(TEXT("error=%d\r\n"),GetLastError()));
}
//RETAILMSG(1,(TEXT("Data------!!!!!!!!!!!!!!!!\r\n")));
// Copy YUV data to buffers pointed to by .CH2, .CH3, .CH4 (addresses filled in by video driver)
pOverlay2_Y = (PBYTE)VirtualAllocCopy(320*240,"pOverlay2_Y",(PVOID)(XllpOverlay.CH2_Y));
pOverlay2_Cb = (PBYTE)VirtualAllocCopy(320*240,"pOverlay2_Cb",(PVOID)(XllpOverlay.CH3_Cb));
pOverlay2_Cr = (PBYTE)VirtualAllocCopy(320*240,"pOverlay2_Cr",(PVOID)(XllpOverlay.CH4_Cr));
fbpY=(unsigned char *)pOverlay2_Y;
fbpCb=(unsigned char *)pOverlay2_Cb;
fbpCr=(unsigned char *)pOverlay2_Cr;
cbInput = sizeof(XLLP_OVERLAY_T);
CameraApp.VideoWidth = XllpOverlay.OverlayWidth;
CameraApp.VideoHeight = XllpOverlay.OverlayHeight;
CameraApp.StillWidth = 640;
CameraApp.StillHeight = 480;
CameraApp.CaptureFormat = FORMAT_PLANAR_422;
CameraApp.FrameRate = 0; // 0 = max frame rate available
// Initialize the Camera with our settings
cbInput = sizeof(CAMERA_APP_T);
//ExtEscape(hDC, CAMERA_INITIALIZE, cbInput, (LPCSTR)&CameraApp, cbOutput, NULL);
DeviceIoControl(hCameraHandle,CAMERA_INITIALIZE,&CameraApp,sizeof(CAMERA_APP_T),NULL,0,0,NULL) ;
for (i = 0; i < MAX_FRAMES; i++)
{
ring_buffer_address[i] = (void *)VirtualAllocCopy(CameraApp.VideoWidth * CameraApp.VideoHeight * 2 ,(char*)TEXT("ringbuffer"),(PVOID)(CameraApp.ring_buffer_address[i]));
}
// capture thread waits on this event for each frame
hEvent = CreateEvent(NULL, FALSE, FALSE, CAMERA_EVENT_NAME);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -