视频捕捉程序开发的基本步骤
2 视频捕捉程序开发的基本步骤
2.1 使用AVICap窗口类
笔者使用的是AVICap窗口类来开发视频捕捉程序。AVICap类支持实时视频流捕捉和单帧捕捉,并提供对视频源的控制。通常使用的MCI控件虽然也提供了数字视频服务。并为视频叠加提供了Overlay命令集等,但这些命令主要是基于文件的操作,还不能满足实时地从视频缓存中提取数据的要求。对于使用没有视频叠加能力的捕捉卡的PC机来说,用MCI提供的命令集是无法捕捉视频流的。而AVICap窗口类在捕捉视频方面具有一定的优势,它能直接访问视频缓冲区,而不需要生成中间文件,因而实时性很强,效率也很高。另外,它还可将数字视频捕捉到一个文件中。
2.2 开发的基本步骤
开发视频捕捉程序主要有以下四个步骤:
(1)创建“捕捉窗”。
在进行视频捕捉之前必需要先创建一个“捕捉窗”,并应以此为基础进行所有的捕捉及设置操作。“捕捉窗”可用AVICap窗口类的“Cap Create Capture Window”函数来创建,
其窗口风格可设置为WSCHILD和WS_VISIBLE参数。
“捕捉窗”类似于
控件,它具有下列功能:
将视频流和音频流捕捉到一个AVI文件中;
动态地同视频和音频输入器件连接或断开;
以Overlay或Preview模式对输入的视频流进行实时显示;
在捕捉时,可指定所用的文件名,并可将捕捉文件的内容拷贝到另一个文件;
设置捕捉速率;
显示控制视频源、视频格式及视频压缩的对话框;
创建、保存或载入调色板;
将图像和相关的调色板拷贝到剪贴板;
将捕捉的单帧图像保存到DIB格式文件。
(2)关联捕捉窗和驱动程序
单独定义的捕捉窗是不能工作的,它须与一个设备相关联才能取得视频信号。用函数CapDriver Connect可使捕捉窗与其设备驱动程序相关联。
(3)设置视频设备的属性
通过设置TcaptureParms结构变量的各个成员变量,可以控制设备的采样频率、中断采样按键、状态行为。设置好TcaptureParms结构变量后,可以用函CapCaptureSetSetup
使设置生效。之后还可以用CapPreviewScale、CapPreviewRate设置预览的比例与速度,也可以直接使用设备的默认值。
(4)打开预览
利用函数CapOverlay可选择是否采用叠加模式预览,以使系统资源占用小,视频显示速度加快。然后用CapPreview启动预览功能,这时就可以在屏幕上看到来自摄像机的图像了。
通过以上四步就可以建立一个基本的视频捕捉程序,但假如想自己处理从设备捕捉到的视频数据,则要使用捕捉窗回调函数来处理,比如一帧一帧地获得视频数据或以流的方式获得视频数据等。
3 基于Delphi的视频捕捉程序
根据系统对系统访问、处理速度等方面的非凡需求,笔者选用Delphi作为开发工具。下面以开发一个逐帧从视频设备上捕捉视频数据的程序为例,来说明每个函数的作用以及开发的具体过程。所给例程的功能是可以在屏幕上显示捕捉到的视频,并可以获得每一帧的图像数据。具体步骤如下:
(1)新建一个
,并将AVICAP32.PAS包含到USES中。
{PASCAL INTERFACE for AVICAP32 DLL}
{Converted from microsoft Header file by ArTee}
{Free to use - I'm NOT responsible fo bugs}
unit avicap32;
interface
uses Windows, Messages, SysUtils, MMSystem;
//This part might be buggy.
//It 'Assumes' types because I cannot find the correct unit type
HVIDEO = longword; //Handle;
UINT = longword;
POINT = record
x, y: integer
end;
LPPOINT = ^POINT;
FOURCC = record
c1, c2, c3, c4: char;
end;
mmioFOURCC = FOURCC;
LPVOID = Pointer;
//LPWAVEHDR, LPVIDEOHDR worden gebruikt in de call-back functions! ik heb
ze maar gereplaced met integers.
{***************************************************************************
*
* avicap.h
*
* Main include file.
*
* Microsoft Video for Windows Sample Capture Class
*
* Copyright (c) 1992, 1993 Microsoft Corporation. All Rights Reserved.
*
* You have a royalty-free right to use, modify, reproduce and
* distribute the Sample Files (and/or any modified version) in
* any way you find useful, provided that you agree that
* Microsoft has no warranty obligations or liability for any
* Sample Application Files which are modified.
*
**************************************************************************}
//const AVICapSM =SendMessage; { SendMessage in C }
// ------------------------------------------------------------------
// Window Messages WM_CAP... which can be sent to an AVICAP window
// ------------------------------------------------------------------
// Defines start of the message range
const
WM_CAP_START = WM_USER;
const
WM_CAP_GET_CAPSTREAMPTR = (WM_CAP_START + 1); const
WM_CAP_SET_CALLBACK_ERROR = (WM_CAP_START + 2); const
WM_CAP_SET_CALLBACK_STATUS = (WM_CAP_START + 3); const
WM_CAP_SET_CALLBACK_YIELD = (WM_CAP_START + 4); const
WM_CAP_SET_CALLBACK_FRAME = (WM_CAP_START + 5); const
WM_CAP_SET_CALLBACK_VIDEOSTREAM = (WM_CAP_START + 6); const
WM_CAP_SET_CALLBACK_WAVESTREAM = (WM_CAP_START + 7); const
WM_CAP_GET_USER_DATA = (WM_CAP_START + 8); const
WM_CAP_SET_USER_DATA = (WM_CAP_START + 9); const
WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10); const
WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11); const
WM_CAP_DRIVER_GET_NAME = (WM_CAP_START + 12); const
WM_CAP_DRIVER_GET_VERSION = (WM_CAP_START + 13); const
WM_CAP_DRIVER_GET_CAPS = (WM_CAP_START + 14); const
WM_CAP_FILE_SET_CAPTURE_FILE = (WM_CAP_START + 20); const
WM_CAP_FILE_GET_CAPTURE_FILE = (WM_CAP_START + 21);
const
WM_CAP_FILE_ALLOCATE = (WM_CAP_START + 22); const
WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23); const
WM_CAP_FILE_SET_INFOCHUNK = (WM_CAP_START + 24); const
WM_CAP_FILE_SAVEDIB = (WM_CAP_START + 25); const
WM_CAP_EDIT_COPY = (WM_CAP_START + 30); const
WM_CAP_SET_AUDIOFORMAT = (WM_CAP_START + 35); const
WM_CAP_GET_AUDIOFORMAT = (WM_CAP_START + 36); const
WM_CAP_DLG_VIDEOFORMAT = (WM_CAP_START + 41); const
WM_CAP_DLG_VIDEOSOURCE = (WM_CAP_START + 42); const
WM_CAP_DLG_VIDEODISPLAY = (WM_CAP_START + 43); const
WM_CAP_GET_VIDEOFORMAT = (WM_CAP_START + 44); const
WM_CAP_SET_VIDEOFORMAT = (WM_CAP_START + 45); const
WM_CAP_DLG_VIDEOCOMPRESSION = (WM_CAP_START + 46); const
WM_CAP_SET_PREVIEW = (WM_CAP_START + 50); const
WM_CAP_SET_OVERLAY = (WM_CAP_START + 51); const
WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52); const
WM_CAP_SET_SCALE = (WM_CAP_START + 53); const
WM_CAP_GET_STATUS = (WM_CAP_START + 54); const
WM_CAP_SET_SCROLL = (WM_CAP_START + 55); const
WM_CAP_GRAB_FRAME = (WM_CAP_START + 60);
const
WM_CAP_GRAB_FRAME_NOSTOP = (WM_CAP_START + 61); const
WM_CAP_SEQUENCE = (WM_CAP_START + 62); const
WM_CAP_SEQUENCE_NOFILE = (WM_CAP_START + 63); const
WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START + 64); const
WM_CAP_GET_SEQUENCE_SETUP = (WM_CAP_START + 65); const
WM_CAP_SET_MCI_DEVICE = (WM_CAP_START + 66); const
WM_CAP_GET_MCI_DEVICE = (WM_CAP_START + 67); const
WM_CAP_STOP = (WM_CAP_START + 68);
const
WM_CAP_ABORT = (WM_CAP_START + 69);
const
WM_CAP_SINGLE_FRAME_OPEN = (WM_CAP_START + 70); const
WM_CAP_SINGLE_FRAME_CLOSE = (WM_CAP_START + 71); const
WM_CAP_SINGLE_FRAME = (WM_CAP_START + 72); const
WM_CAP_PAL_OPEN = (WM_CAP_START + 80); const
WM_CAP_PAL_SAVE = (WM_CAP_START + 81); const
WM_CAP_PAL_PASTE = (WM_CAP_START + 82); const
WM_CAP_PAL_AUTOCREATE = (WM_CAP_START + 83); const
WM_CAP_PAL_MANUALCREATE = (WM_CAP_START + 84); // Following added post VFW 1.1
const
WM_CAP_SET_CALLBACK_CAPCONTROL = (WM_CAP_START + 85); // Defines end of the message range
const
WM_CAP_END = WM_CAP_SET_CALLBACK_CAPCONTROL;
// ------------------------------------------------------------------
// structures
// ------------------------------------------------------------------ type
tagCapDriverCaps = packed record
wDeviceIndex: word; // Driver index in system.ini
fHasOverlay: wordbool; // Can device overlay?
fHasDlgVideoSource: wordbool; // Has Video source dlg?
fHasDlgVideoFormat: wordbool; // Has Format dlg?
fHasDlgVideoDisplay: wordbool; // Has External out dlg?
fCaptureInitialized: wordbool; // Driver ready to capture?
fDriverSuppliesPalettes: wordbool; // Can driver make palettes?
hVideoIn: HVIDEO; // Driver In channel
hVideoOut: HVIDEO; // Driver Out channel
hVideoExtIn: HVIDEO; // Driver Ext In channel
hVideoExtOut: HVIDEO; // Driver Ext Out channel
end;
CAPDRIVERCAPS = tagCapDriverCaps;
PCAPDRIVERCAPS = ^tagCapDriverCaps;
LPCAPDRIVERCAPS = ^tagCapDriverCaps;
type
tagCapStatus = record
uiImageWidth: UInt; // Width of the image
uiImageHeight: UInt; // Height of the image
fLiveWindow: longbool; // Now Previewing video?
fOverlayWindow: longbool; // Now Overlaying video?
fScale: longbool; // Scale image to client?
ptScroll: POINT; // Scroll position
fUsingDefaultPalette: longbool; // Using default driver palette?
fAudioHardware: longbool; // Audio hardware present?
fCapFileExists: longbool; // Does capture file exist?
dwCurrentVideoFrame: DWORD; // # of video frames cap'td
dwCurrentVideoFramesDropped: DWORD; // # of video frames dropped
dwCurrentWaveSamples: DWORD; // # of wave samples cap'td
dwCurrentTimeElapsedMS: DWORD; // Elapsed capture duration
hPalCurrent: HPALETTE;// Current palette in use
fCapturingNow: longbool; // Capture in progress?
dwReturn: DWORD; // Error value after any operation
wNumVideoAllocated: word; // Actual number of video buffers
wNumAudioAllocated: word; // Actual number of audio buffers
end;
CAPSTATUS = tagCapStatus;
PCAPSTATUS = ^tagCapStatus;
LPCAPSTATUS = ^tagCapStatus;
// Default values in parenthesis
type
tagCaptureParms = record
dwRequestMicroSecPerFrame: DWORD; // Requested capture rate
fMakeUserHitOKToCapture: longbool; // Show "Hit OK to cap" dlg?
wPercentDropForError: word; // Give error msg if > (10%)
fYield: longbool; // Capture via background task?
dwIndexSize: DWORD; // Max index size in frames (32K)
wChunkGranularity: word; // Junk chunk granularity (2K)
fUsingDOSMemory: longbool; // Use DOS buffers?
wNumVideoRequested: word; // # video buffers, If 0, autocalc
fCaptureAudio: longbool; // Capture audio?
wNumAudioRequested: word; // # audio buffers, If 0, autocalc
vKeyAbort: word; // Virtual key causing abort
fAbortLeftMouse: longbool; // Abort on left mouse?
fAbortRightMouse: longbool; // Abort on right mouse?
fLimitEnabled: longbool; // Use wTimeLimit?
wTimeLimit: word; // Seconds to capture
fMCIControl: longbool; // Use MCI video source?
fStepMCIDevice: longbool; // Step MCI device?
dwMCIStartTime: DWORD; // Time to start in MS
dwMCIStopTime: DWORD; // Time to stop in MS
fStepCaptureAt2x: longbool; // Perform spatial averaging 2x
wStepCaptureAverageFrames: word; // Temporal average n Frames
dwAudioBufferSize: DWORD; // Size of audio bufs (0 : default)
fDisableWriteCache: longbool; // Attempt to disable write cache
end;
CAPTUREPARMS = tagCaptureParms;
PCAPTUREPARMS = ^tagCaptureParms;
LPCAPTUREPARMS = ^tagCaptureParms;
type
tagCapInfoChunk = record
fccInfoID: FOURCC; // Chunk ID, "ICOP" for copyright
lpData: LPVOID; // poSmallInter to data
cbData: integer; // size of lpData
end;
CAPINFOCHUNK = tagCapInfoChunk;
PCAPINFOCHUNK = ^tagCapInfoChunk;
LPCAPINFOCHUNK = ^tagCapInfoChunk;
//Type TVideoHDR is not present in avicap32.. we might need it so we add it type
TVideoHDR = record
lpData: Pointer {LPBYTE}; //* pointer to locked data buffer */
dwBufferLength: DWord; //* Length of data buffer */
dwBytesUsed: DWord; //* Bytes actually used */
dwTimeCaptured: DWord; //* Milliseconds from startof stream */
dwUser: DWord; //* for client's use */
dwFlags: DWord; // assorted flags(see defines)*/
dwReserved{[4]}: DWord; //* reserved for driver */
end;
PVideoHDR = ^TVideoHDR;
LPVideoHDR = PVideoHDR;
//* dwFlags field of VIDEOHDR */
const
VHDR_DONE = $00000001; //* Done bit */
const
VHDR_PREPARED = $00000002; //* Set if this header has been prepared */
const
VHDR_INQUEUE = $00000004; //* Reserved for driver */ const
VHDR_KEYFRAME = $00000008; //* Key Frame */
// ------------------------------------------------------------------
// Callback Definitions
// ------------------------------------------------------------------
//Nog zo'n mooi stukkie dat ik ff laat zitten.
// componentje van maken, zelf de callback doen. type
CAPYIELDCALLBACK = function(hWnd: HWND): LRESULT; stdcall; type
CAPSTATUSCALLBACK = function(hWnd: HWND; nID: smallint;
lpsz: LPCSTR): LRESULT; stdcall;
type
CAPERRORCALLBACK = function(hWnd: HWND; nID: smallint; lpsz: LPCSTR):
LRESULT; stdcall;
type
CAPVIDEOCALLBACK = function(hWnd: HWND; lpVHdr: integer{LPVIDEOHDR}): LRESULT;
stdcall;
type
CAPWAVECALLBACK = function(hWnd: HWND; lpWHdr: integer{LPWAVEHDR}): LRESULT;
stdcall;
type
CAPCONTROLCALLBACK = function(hWnd: HWND; nState: smallint): LRESULT; stdcall;
// ------------------------------------------------------------------
// CapControlCallback states
// ------------------------------------------------------------------ const
CONTROLCALLBACK_PREROLL = 1; { Waiting to start capture } const
CONTROLCALLBACK_CAPTURING = 2; { Now capturing }
// ------------------------------------------------------------------
// The only exported functions from AVICAP.DLL
// ------------------------------------------------------------------ function {VFWAPI} capCreateCaptureWindow(lpszWindowName: LPCSTR;
dwStyle: DWORD; x, y, nWidth, nHeight: integer; hwndParent: HWND;
nID: integer): HWND; stdcall;
external 'AVICAP32.DLL' Name 'capCreateCaptureWindowA'; function {VFWAPI} capGetDriverDescription(wDriverIndex: word;
lpszName: LPSTR; cbName: integer; lpszVer: LPSTR; cbVer: integer): longbool;
stdcall; external 'AVICAP32.DLL' Name 'capGetDriverDescriptionA';
// ------------------------------------------------------------------
// New Information chunk IDs
// ------------------------------------------------------------------ const
infotypeDIGITIZATION_TIME = ('IDIT');
const
infotypeSMPTE_TIME = ('ISMP');
// ------------------------------------------------------------------
// String IDs from status and error callbacks
// ------------------------------------------------------------------ const
IDS_CAP_BEGIN = 300; { "Capture Start" }
const
IDS_CAP_END = 301; { "Capture End" }
const
IDS_CAP_INFO = 401; { "%s" }
const
IDS_CAP_OUTOFMEM = 402; { "Out of memory" }
const
IDS_CAP_FILEEXISTS = 403; { "File '%s' exists -- overwrite it?" } const
IDS_CAP_ERRORPALOPEN = 404; { "Error opening palette '%s'" } const
IDS_CAP_ERRORPALSAVE = 405; { "Error saving palette '%s'" } const
IDS_CAP_ERRORDIBSAVE = 406; { "Error saving frame '%s'" } const
IDS_CAP_DEFAVIEXT = 407; { "avi" }
const
IDS_CAP_DEFPALEXT = 408; { "pal" }
const
IDS_CAP_CANTOPEN = 409; { "Cannot open '%s'" }
const
IDS_CAP_SEQ_MSGSTART = 410;
{ "Select OK to start capture\nof video sequence\nto %s." } const
IDS_CAP_SEQ_MSGSTOP = 411; { "Hit ESCAPE or click to end capture" } const
IDS_CAP_VIDEDITERR = 412; { "An error occurred while trying to run VidEdit." }
const
IDS_CAP_READONLYFILE = 413; { "The file '%s' is a read-only file." } const
IDS_CAP_WRITEERROR = 414; { "Unable to write to file '%s'.\nDisk may be full." }
const
IDS_CAP_NODISKSPACE = 415;
{ "There is no space to create a capture file on the specified device." } const
IDS_CAP_SETFILESIZE = 416; { "Set File Size" }
const
IDS_CAP_SAVEASPERCENT = 417; { "SaveAs: %2ld%% Hit Escape to abort." }
const
IDS_CAP_DRIVER_ERROR = 418; { Driver specific error message } const
IDS_CAP_WAVE_OPEN_ERROR = 419;
{ "Error: Cannot open the wave input device.\nCheck sample size, frequency, and channels." }
const
IDS_CAP_WAVE_ALLOC_ERROR = 420; { "Error: Out of memory for wave buffers." }
const
IDS_CAP_WAVE_PREPARE_ERROR = 421; { "Error: Cannot prepare wave buffers." }
const
IDS_CAP_WAVE_ADD_ERROR = 422; { "Error: Cannot add wave buffers." } const
IDS_CAP_WAVE_SIZE_ERROR = 423; { "Error: Bad wave size." } const
IDS_CAP_VIDEO_OPEN_ERROR = 424; { "Error: Cannot open the video input device." }
const
IDS_CAP_VIDEO_ALLOC_ERROR = 425; { "Error: Out of memory for video buffers." }
const
IDS_CAP_VIDEO_PREPARE_ERROR = 426; { "Error: Cannot prepare video buffers." }
const
IDS_CAP_VIDEO_ADD_ERROR = 427; { "Error: Cannot add video buffers." } const
IDS_CAP_VIDEO_SIZE_ERROR = 428; { "Error: Bad video size." } const
IDS_CAP_FILE_OPEN_ERROR = 429; { "Error: Cannot open capture file." } const
IDS_CAP_FILE_WRITE_ERROR = 430;
{ "Error: Cannot write to capture file. Disk may be full." } const
IDS_CAP_RECORDING_ERROR = 431;
{ "Error: Cannot write to capture file. Data rate too high or disk full." } const
IDS_CAP_RECORDING_ERROR2 = 432; { "Error while recording" } const
IDS_CAP_AVI_INIT_ERROR = 433; { "Error: Unable to initialize for capture." }
const
IDS_CAP_NO_FRAME_CAP_ERROR = 434;
{ "Warning: No frames captured.\nConfirm that vertical sync SmallInterrupts\nare configured and enabled." }
const
IDS_CAP_NO_PALETTE_WARN = 435; { "Warning: Using default palette." } const
IDS_CAP_MCI_CONTROL_ERROR = 436; { "Error: Unable to access MCI device." }
const
IDS_CAP_MCI_CANT_STEP_ERROR = 437; { "Error: Unable to step MCI device." }
const
IDS_CAP_NO_AUDIO_CAP_ERROR = 438;
{ "Error: No audio data captured.\nCheck audio card settings." } const
IDS_CAP_AVI_DRAWDIB_ERROR = 439; { "Error: Unable to draw this data format." }
const
IDS_CAP_COMPRESSOR_ERROR = 440; { "Error: Unable to initialize compressor." }
const
IDS_CAP_AUDIO_DROP_ERROR = 441;
{ "Error: Audio data was lost during capture, reduce capture rate." } { status string IDs }
const
IDS_CAP_STAT_LIVE_MODE = 500; { "Live window" }
const
IDS_CAP_STAT_OVERLAY_MODE = 501; { "Overlay window" } const
IDS_CAP_STAT_CAP_INIT = 502; { "Setting up for capture - Please wait" } const
IDS_CAP_STAT_CAP_FINI = 503; { "Finished capture, now writing frame %ld" }
const
IDS_CAP_STAT_PALETTE_BUILD = 504; { "Building palette map" } const
IDS_CAP_STAT_OPTPAL_BUILD = 505; { "Computing optimal palette" } const
IDS_CAP_STAT_I_FRAMES = 506; { "%d frames" }
const
IDS_CAP_STAT_L_FRAMES = 507; { "%ld frames" }
const
IDS_CAP_STAT_CAP_L_FRAMES = 508; { "Captured %ld frames" } const
IDS_CAP_STAT_CAP_AUDIO = 509; { "Capturing audio" }
const
IDS_CAP_STAT_VIDEOCURRENT = 510; { "Captured %ld frames (%ld dropped) %d.%03d sec." }
const
IDS_CAP_STAT_VIDEOAUDIO = 511;
{ "Captured %d.%03d sec. %ld frames (%ld dropped) (%d.%03d fps). %ld audio bytes (%d,%03d sps)" }
const
IDS_CAP_STAT_VIDEOONLY = 512;
{ "Captured %d.%03d sec. %ld frames (%ld dropped) (%d.%03d fps)" } const
IDS_CAP_STAT_FRAMESDROPPED = 513;
{ "Dropped %ld of %ld frames (%d.%02d%%) during capture." } function capSetCallbackOnStatus(Handle: HWND; fpProc: Pointer): integer; function capSetCallbackOnYield(Handle: HWND; fpProc: Pointer): integer; function capSetCallbackOnFrame(Handle: HWND; fpProc: Pointer): integer; function capSetCallbackOnVideoStream(Handle: HWND; fpProc: Pointer): integer;
function capSetCallbackOnWaveStream(Handle: HWND; fpProc: Pointer): integer;
function capSetCallbackOnCapControl(Handle: HWND; fpProc: Pointer): integer; function capSetUserData(Handle: HWND; lUser: integer): integer; function capGetUserData(Handle: HWND): integer;
function capDriverConnect(Handle: HWND; i: integer): integer; function capDriverDisconnect(Handle: HWND): integer;
function capDriverGetName(Handle: HWND; szName: PChar; wSize: integer): integer;
function capDriverGetVersion(Handle: HWND; szVer: integer; wSize: integer): integer;
function capDriverGetCaps(Handle: HWND; s: LPCapDriverCaps; wSize: integer): integer;
function capFileSetCaptureFile(Handle: HWND; szName: PChar): integer; function capFileGetCaptureFile(Handle: HWND; szName: PChar; wSize: integer): integer;
function capFileAlloc(Handle: HWND; dwSize: integer): integer; function capFileSaveAs(Handle: HWND; szName: PChar): integer; function capFileSetInfoChunk(Handle: HWND; lpInfoChunk: LPCapInfoChunk): integer;
function capFileSaveDIB(Handle: HWND; szName: PChar): integer;
function capEditCopy(Handle: HWND): integer;
function capSetAudioFormat(Handle: HWND; s: PWaveFormatEx; wSize: integer): integer;
function capGetAudioFormat(Handle: HWND; s: PWaveFormatEx; wSize: integer): DWord;
function capGetAudioFormatSize(Handle: HWND): DWord;
function capDlgVideoFormat(Handle: HWND): integer;
function capDlgVideoSource(Handle: HWND): integer;
function capDlgVideoDisplay(Handle: HWND): integer;
function capDlgVideoCompression(Handle: HWND): integer; function capGetVideoFormat(Handle: HWND; s: integer; wSize: integer): DWord;
function capGetVideoFormatSize(Handle: HWND): DWord;
function capSetVideoFormat(Handle: HWND; s: integer; wSize: integer): integer;
function capPreview(Handle: HWND; f: longbool): integer; function capPreviewRate(Handle: HWND; wMS: integer): integer; function capOverlay(Handle: HWND; f: longbool): integer; function capPreviewScale(Handle: HWND; f: longbool): integer; function capGetStatus(Handle: HWND; s: LPCapStatus; wSize: integer): integer;
function capSetScrollPos(Handle: HWND; lpP: LPPOINT): integer; function capGrabFrame(Handle: HWND): integer;
function capGrabFrameNoStop(Handle: HWND): integer;
function capCaptureSequence(Handle: HWND): integer;
function capCaptureSequenceNoFile(Handle: HWND): integer; function capCaptureStop(Handle: HWND): integer;
function capCaptureAbort(Handle: HWND): integer;
function capCaptureSingleFrameOpen(Handle: HWND): integer; function capCaptureSingleFrameClose(Handle: HWND): integer; function capCaptureSingleFrame(Handle: HWND): integer; function capCaptureGetSetup(Handle: HWND; s: LPCaptureParms; wSize: integer): integer;
function capCaptureSetSetup(Handle: HWND; s: LPCaptureParms; wSize: integer): integer;
function capSetMCIDeviceName(Handle: HWND; szName: PChar): integer; function capGetMCIDeviceName(Handle: HWND; szName: PChar; wSize: integer): longbool;
function capPaletteOpen(Handle: HWND; szName: PChar): integer; function capPaletteSave(Handle: HWND; szName: PChar): integer; function capPalettePaste(Handle: HWND): integer;
function capPaletteAuto(Handle: HWND; iFrames: integer; iColors: integer): integer;
function capPaletteManual(Handle: HWND; fGrab: integer; iColors: integer): integer;
implementation
// ------------------------------------------------------------------
// Message crackers for above
// ------------------------------------------------------------------
//Leuk, maar dat moet dan richting implemenation...
function capSetCallbackOnError(hwnd: integer; fpProc: Pointer): integer; begin
Result := sendmessage(hwnd, WM_CAP_SET_CALLBACK_ERROR, 0, integer(fpProc));
end;
function capSetCallbackOnStatus(Handle: HWND; fpProc: Pointer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_STATUS, 0, integer(fpProc));
end;
function capSetCallbackOnYield(Handle: HWND; fpProc: Pointer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_YIELD, 0, integer(fpProc));
end;
function capSetCallbackOnFrame(Handle: HWND; fpProc: Pointer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_FRAME, 0, integer(fpProc));
end;
function capSetCallbackOnVideoStream(Handle: HWND; fpProc: Pointer): integer;
begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, integer(fpProc));
end;
function capSetCallbackOnWaveStream(Handle: HWND; fpProc: Pointer): integer;
begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_WAVESTREAM, 0, integer(fpProc));
end;
function capSetCallbackOnCapControl(Handle: HWND; fpProc: Pointer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_CALLBACK_CAPCONTROL, 0, integer(fpProc));
end;
function capSetUserData(Handle: HWND; lUser: integer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_USER_DATA, 0, lUser); end;
function capGetUserData(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_GET_USER_DATA, 0, 0); end;
function capDriverConnect(Handle: HWND; i: integer): integer; begin
Result := sendmessage(handle, WM_CAP_DRIVER_CONNECT, i, 0); end;
function capDriverDisconnect(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_DRIVER_DISCONNECT, 0, 0); end;
function capDriverGetName(Handle: HWND; szName: PChar; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_DRIVER_GET_NAME, (wSize), integer(szName));
end;
function capDriverGetVersion(Handle: HWND; szVer: integer; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_DRIVER_GET_VERSION, (wSize), (szVer));
end;
function capDriverGetCaps(Handle: HWND; s: LPCapDriverCaps; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_DRIVER_GET_CAPS, (wSize), integer(s));
end;
function capFileSetCaptureFile(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_FILE_SET_CAPTURE_FILE, 0,
integer(szName));
end;
function capFileGetCaptureFile(Handle: HWND; szName: PChar; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_FILE_GET_CAPTURE_FILE, (wSize), integer(szName));
end;
function capFileAlloc(Handle: HWND; dwSize: integer): integer; begin
Result := sendmessage(handle, WM_CAP_FILE_ALLOCATE, 0, (dwSize)); end;
function capFileSaveAs(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_FILE_SAVEAS, 0, integer(szName));
end;
function capFileSetInfoChunk(Handle: HWND; lpInfoChunk: LPCapInfoChunk): integer;
begin
Result := sendmessage(handle, WM_CAP_FILE_SET_INFOCHUNK, 0, integer(lpInfoChunk));
end;
function capFileSaveDIB(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_FILE_SAVEDIB, 0, integer(szName));
end;
function capEditCopy(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_EDIT_COPY, 0, 0); end;
function capSetAudioFormat(Handle: HWND; s: PWaveFormatEx; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_SET_AUDIOFORMAT, (wSize), integer(s));
end;
function capGetAudioFormat(Handle: HWND; s: PWaveFormatEx; wSize: integer): DWord;
begin
Result := sendmessage(handle, WM_CAP_GET_AUDIOFORMAT, (wSize),
integer(s));
end;
function capGetAudioFormatSize(Handle: HWND): DWord;
begin
Result := sendmessage(handle, WM_CAP_GET_AUDIOFORMAT, 0, 0{ NULL}); end;
function capDlgVideoFormat(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_DLG_VIDEOFORMAT, 0, 0); end;
function capDlgVideoSource(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_DLG_VIDEOSOURCE, 0, 0); end;
function capDlgVideoDisplay(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_DLG_VIDEODISPLAY, 0, 0); end;
function capDlgVideoCompression(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0); end;
function capGetVideoFormat(Handle: HWND; s: integer; wSize: integer): DWord;
begin
Result := sendmessage(handle, WM_CAP_GET_VIDEOFORMAT, (wSize), (s)); end;
function capGetVideoFormatSize(Handle: HWND): DWord;
begin
Result := DWORD(sendmessage(handle, WM_CAP_GET_VIDEOFORMAT, 0, 0));
end;
function capSetVideoFormat(Handle: HWND; s: integer; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_SET_VIDEOFORMAT, (wSize), (s)); end;
function capPreview(Handle: HWND; f: longbool): integer; begin
Result := sendmessage(handle, WM_CAP_SET_PREVIEW, integer(f), 0); end;
function capPreviewRate(Handle: HWND; wMS: integer): integer; begin
Result := sendmessage(handle, WM_CAP_SET_PREVIEWRATE, (wMS), 0); end;
function capOverlay(Handle: HWND; f: longbool): integer; begin
Result := sendmessage(handle, WM_CAP_SET_OVERLAY, integer(f), 0); end;
function capPreviewScale(Handle: HWND; f: longbool): integer; begin
Result := sendmessage(handle, WM_CAP_SET_SCALE, integer(f), 0); end;
function capGetStatus(Handle: HWND; s: LPCapStatus; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_GET_STATUS, wSize, integer(s)); end;
function capSetScrollPos(Handle: HWND; lpP: LPPOINT): integer; begin
Result := sendmessage(handle, WM_CAP_SET_SCROLL, 0, integer(lpP)); end;
function capGrabFrame(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_GRAB_FRAME, 0, 0); end;
function capGrabFrameNoStop(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0); end;
function capCaptureSequence(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_SEQUENCE, 0, 0); end;
function capCaptureSequenceNoFile(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_SEQUENCE_NOFILE, 0, 0); end;
function capCaptureStop(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_STOP, 0, 0); end;
function capCaptureAbort(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_ABORT, 0, 0); end;
function capCaptureSingleFrameOpen(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_SINGLE_FRAME_OPEN, 0, 0); end;
function capCaptureSingleFrameClose(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_SINGLE_FRAME_CLOSE, 0, 0); end;
function capCaptureSingleFrame(Handle: HWND): integer; begin
Result := sendmessage(handle, WM_CAP_SINGLE_FRAME, 0, 0); end;
function capCaptureGetSetup(Handle: HWND; s: LPCaptureParms; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_GET_SEQUENCE_SETUP, (wSize), integer(s));
end;
function capCaptureSetSetup(Handle: HWND; s: LPCaptureParms; wSize: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_SET_SEQUENCE_SETUP, (wSize), integer(s));
end;
function capSetMCIDeviceName(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_SET_MCI_DEVICE, 0, integer(szName));
end;
function capGetMCIDeviceName(Handle: HWND; szName: PChar; wSize: integer): longbool;
begin
Result := longbool(sendmessage(handle, WM_CAP_GET_MCI_DEVICE, (wSize),
integer(szName)));
end;
function capPaletteOpen(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_PAL_OPEN, 0, integer(szName)); end;
function capPaletteSave(Handle: HWND; szName: PChar): integer; begin
Result := sendmessage(handle, WM_CAP_PAL_SAVE, 0, integer(szName)); end;
function capPalettePaste(Handle: HWND): integer;
begin
Result := sendmessage(handle, WM_CAP_PAL_PASTE, 0, 0); end;
function capPaletteAuto(Handle: HWND; iFrames: integer; iColors: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_PAL_AUTOCREATE, (iFrames), (iColors));
end;
function capPaletteManual(Handle: HWND; fGrab: integer; iColors: integer): integer;
begin
Result := sendmessage(handle, WM_CAP_PAL_MANUALCREATE, (fGrab), (iColors));
end;
end.
(2)在Form1上放置一个Tpanel控件,设Name为“gCapVideoArea”,该控件用于
显示视频。之后再放置两个Tbutton控件,一个Name为“Openvideo”,另一个Name为
“Closevideo”。
(3)定义全局变量
ghCapWnd:Thandle; //定义捕捉窗句柄
VideoStr:LPVIDEOHDR; //可以得到视频数据指针的结构变量,用于回调函数中
CapParms:TcaptureParms; //用于设置设备属性的结构变量
(4)编写代码
在Name为“Openvideo”的Tbutton的Click事件中写入以下代码: procedure Tform1.OpenvidoClick;
begin
//使用Tpanel控件来创建捕捉窗口
ghCapWnd:,CapCreateCaptureWindow,
WS_CHILD or WS_VISIBLE, //窗口样式
0,//X坐标
0,//Y坐标
gCapVideoArea,Width, //窗口宽
gCapVideoArea,Handle, //窗口句柄
0); //一般为0
为了能够捕捉视频,应启动一个捕捉帧回调函数VideoStreamC