directshow 无窗模式,可按钮触发截图事件

//无窗模式,可按钮触发截图
#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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值