//无窗模式,可按钮触发截图
#include <Windows.h>
#include <tchar.h>
#include <DShow.h>
#include <atlbase.h>
#include <streams.h>
#include <assert.h>
#include <d3d9.h>
#include <vmr9.h>
#pragma include_alias( "dxtrans.h", "qedit.h" )
#define __IDxtCompositor_INTERFACE_DEFINED__
#define __IDxtAlphaSetter_INTERFACE_DEFINED__
#define __IDxtJpeg_INTERFACE_DEFINED__
#define __IDxtKey_INTERFACE_DEFINED__
#include <qedit.h>
#define DEFAULT_VIDEO_WIDTH 640
#define DEFAULT_VIDEO_HEIGHT 480
#define WIN_STYLE (WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX)
#define EXT_STYLE (WS_EX_APPWINDOW | WS_EX_OVERLAPPEDWINDOW)
enum PLAYSTATE {Stopped, Paused, Running, Init};
typedef HANDLE zbar_event_t;
typedef struct zbar_mutex_s {
int count;
CRITICAL_SECTION mutex;
} zbar_mutex_t;
struct video_state_s {
HANDLE captured;
HWND hwnd; /* vfw interface */
HANDLE notify; /* capture thread status change */
int bi_size; /* size of bih */
BITMAPINFOHEADER *bih; /* video format details */
//主要的用于摄像头预览
IGraphBuilder* m_pGraph;
ICaptureGraphBuilder2* m_pBuilder;
IBaseFilter* pSrcFilter;
IMediaControl* g_pMC;
IVideoWindow* g_pVW;
IMediaEventEx * g_pME;
CComPtr<ISampleGrabber> pGrabber;
HINSTANCE hInstance;
//静态拍照
IAMVideoControl* m_pAMVideoControl;
IBaseFilter* m_pTSG_Filter;
ISampleGrabber* m_pTSG;
IBaseFilter *m_pNull;
AM_MEDIA_TYPE mt;
AM_MEDIA_TYPE g_StillMediaType;
IVMRWindowlessControl9 *m_pVMR9;
IBaseFilter *m_pRenderFilter;
IVMRFilterConfig* m_pConfig;;
};
struct zbar_processor_s {
const void *userdata; /* application data */
unsigned req_width, req_height; /* application requested video size */
int req_intf, req_iomode; /* application requested interface */
int input; /* user input status */
int threaded;
int visible; /* output window mapped to display */
int streaming; /* video enabled */
int dumping; /* debug image dump */
void *display; /* X display connection */
unsigned long xwin; /* toplevel window */
zbar_mutex_t mutex; /* shared data mutex */
int lock_level;
HINSTANCE hInstance;
};
typedef struct video_state_s video_state_t;
static inline int _zbar_mutex_init (zbar_mutex_t *lock)
{
lock->count = 1;
InitializeCriticalSection(&lock->mutex); //函数功能初始化一个临界资源对象
return(0);
}
static inline void _zbar_mutex_destroy (zbar_mutex_t *lock)
{
DeleteCriticalSection(&lock->mutex);
}
static inline int _zbar_mutex_lock (zbar_mutex_t *lock)
{
EnterCriticalSection(&lock->mutex);
if(lock->count++ < 1)
assert(0);
return(0);
}
static inline int _zbar_mutex_unlock (zbar_mutex_t *lock)
{
if(lock->count-- <= 1)
assert(0);
LeaveCriticalSection(&lock->mutex);
return(0);
}
class CSampleGrabberCB : public ISampleGrabberCB
{
public:
long Width;
long Height;
HWND cb_hwnd;
bool bFirst;
CSampleGrabberCB( )
{
Width = 0;
Height = 0;
cb_hwnd = NULL;
bFirst = false;
}
STDMETHODIMP_(ULONG) AddRef() { return 1; }
STDMETHODIMP_(ULONG) Release() { return 2; }
STDMETHODIMP QueryInterface(REFIID riid, void** ppvObject)
{
if (NULL == ppvObject)
return E_POINTER;
if (riid == __uuidof(IUnknown))
{
*ppvObject = static_cast<IUnknown*>(this);
return S_OK;
}
if (riid == __uuidof(ISampleGrabberCB))
{
*ppvObject = static_cast<ISampleGrabberCB*>(this);
return S_OK;
}
return E_NOTIMPL;
}
STDMETHODIMP SampleCB(double SampleTime,IMediaSample* pSample)
{
return E_NOTIMPL;
}
STDMETHODIMP BufferCB(double Time,BYTE* pBuffer,long lBufferSize)
{
video_state_t *state = (video_state_t *)GetWindowLong(cb_hwnd,0);
HWND cb_hwndTemp = (HWND)GetWindowLong(cb_hwnd,4);
if (!bFirst)//初次启动会有消息过来
{
bFirst = true;
return S_OK;
}
if (!pBuffer)
return E_POINTER;
if ((state->g_StillMediaType.majortype != MEDIATYPE_Video)
directshow 无窗模式,可按钮触发截图事件
最新推荐文章于 2025-04-02 11:12:24 发布