青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 74, 文章 - 0, 評論 - 26, 引用 - 0
數(shù)據(jù)加載中……

在Windows Mobile 5中使用DirectShow控制攝像頭-轉(zhuǎn)

By Amit Ranjan
July 21, 2006

A number of Windows Mobile 5.0 APIs (for example, SHCameraCapture) make it trivial for a mobile application developer to access a camera, but their ease of use comes at a price—flexibility. Most of the time, using the API directly would offer a solution, but sometimes you need more control and flexibility. That's where Microsoft's DirectShow framework comes in. This article shows how to use DirectShow to access a camera. It demonstrates how to build a filter graph manually and how to handle graph events in the application message handler. Having some prior knowledge of DirectShow and COM will be helpful, but it's not necessary.

Figure 1 depicts the components in the filter graph you will use to capture video.

Figure 1: Filter Graph for Video Capture

The camera is the hardware component. For an application to interact with the camera, it would need to talk to its drivers. Next, the video capture filter enables an application to capture video. After capture, you encode the data using WMV9EncMediaObject, a DirectX Media Object (DMO). You can use a DMO inside a filter graph with the help of a DMO Wrapper filter. Next, the encoded video data needs to be multiplexed. You use a Windows Media ASF writer filter for this task. The ASF writer multiplexes the video data and writes it to an .asf file. With that, your filter graph is ready. Now, it's just a matter of running it. As you will see, building the graph is pretty easy too.

Set the Build Environment

First, you need to set the build environment. Add the following libraries in the linker setting of a Visual Studio 2005 Smart Device project:

  • dmoguids.lib
  • strmiids.lib
  • strmbase.lib
  • uuid.lib

Also include the following header files in your project:

  • atlbase.h
  • dmodshow.h
  • dmoreg.h
  • wmcodecids.h

 

Note: For the sake of clarity, this example doesn't show error handling. However, a real world application would require error handling.

Building the Graph

A filter graph that performs audio or video capture is known as a Capture graph. DirectShow provides a Capture Graph Builder object that exposes an interface called ICaptureGraphBuilder2; it exposes methods to help build and control a capture graph.

First, create instances of IGraphBuilder and ICaptureGraphBuilder2 by using the COM function CoCreateInstance:

HRESULT hResult = S_OK;

IGraphBuilder *pFilterGraph;

ICaptureGraphBuilder2 *pCaptureGraphBuilder;

hResult=CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,

                         IID_IGraphBuilder,(void**)&pFilterGraph);

hResult=CoCreateInstance(CLSID_CaptureGraphBuilder, NULL,

                         CLSCTX_INPROC, IID_ICaptureGraphBuilder2,

                         (void**)& pCaptureGraphBuilder);

CoCreateInstance takes five parameters:

  1. The first is a class ID.
  2. The second decides whether the object created is part of an aggregator.
  3. The third specifies the context in which the newly created object would run.
  4. The fourth parameter is a reference to the identifier of the interface you will use to communicate with the object.
  5. The last parameter is the address of the variable that receives the interface pointer requested.

Once you have created the IGraphBuilder and ICaptureGraphBulder2 instances, you need to call the SetFilterGraph method of the ICaptureGraphBuilder2 interface:

hResult = m_pCaptureGraphBuilder->SetFiltergraph( pFilterGraph );

The SetFilterGraph method takes a pointer to the IGraphBuilder interface. This specifies which filter graph the capture graph builder will use. If you don't call the SetFilterGraph method, the Capture graph builder automatically creates a graph when it needs it.

Now, you're ready to create an instance of the video capture filter. The following code initializes a Video capture filter, the pointer of which is returned by the CoCreateInstance:

IBaseFilter *pVideoCaptureFilter;

hResult=CoCreateInstance(CLSID_VideoCapture, NULL, CLSCTX_INPROC,

                         IID_IBaseFilter, (void**)&pVideoCaptureFilter);

You then need to get a pointer to IPersistPropertyBag from the video capture filter. You use this pointer to set the capture device (in other words, the camera) that the capture filter will use, as follows:

IPersistPropertyBag *pPropertyBag;

hResult=pVideoCaptureFilter->QueryInterface( &pPropertyBag );

Now, you need to get a handle on the camera you will use to capture video. You can enumerate the available camera devices by using the FindFirstDevice and FindNextDevice functions. You can have multiple cameras present on a device. (HTC Universal is one example.) To keep the code simple for this example, use FindFirstDevice to get the first available camera on the device as follows:

DEVMGR_DEVICE_INFORMATION devInfo;

CComVariant  CamName;

CPropertyBag PropBag;

GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A, 0x93,

                    0x3E, 0x4D, 0x7E, 0x3C, 0x86 };

devInfo.dwSize = sizeof(devInfo);

FindFirstDevice( DeviceSearchByGuid, &guidCamera, & devInfo);

CamName=devInfo.szLegacyName

PropBag.Write( _T("VCapName"), &CamName );

pPropertyBag->Load( &PropBag, NULL );

hResult =pFilterGraph->AddFilter( pVideoCaptureFilter,

                                  _T("Video Capture Filter") );

pPropertyBag.Release();

Note the first parameter in the FindFirstDevice, DeviceSearchByGuid. It specifies the search type. Other options are DeviceSearchByLegacyName, DeviceSearchByDeviceName, and so forth. DeviceSearchByGuid is the most reliable way to find a capture device. The information regarding the device is returned in the DEVMGR_DEVICE_INFORMATION structure. You store the szLegacyName value in the CComVariant variable, and you need an object that has implemented IPropertyBag interface.

In the code sample, CPropertyBag is a custom class that has implemented IPropertyBag. This object is needed to pass the capture device name to the filter. The string VCapName identifies the filter property for the name of the video capture device. Once you have set the capture device, you can add the Video capture filter to the filter graph. You use the AddFilter method of the graph manager for this. This method takes two parameters: the first is the pointer to the filter that is to be added, and the second is the name of the filter. The second parameter can be NULL; in this case, the filter graph manager generates a unique name for the filter. If you have provided a name that conflicts with some other filter, the manager will modify the name to make it unique.

You then need to instantiate the WMV9 encoder:

IBaseFilter *pVideoEncoder;

IDMOWrapperFilter *pWrapperFilter;

hResult=CoCreateInstance(CLSID_DMOWrapperFilter, NULL,CLSCTX_INPROC,

                         IID_IBaseFilter, (void**)&pVideoEncoder);

hResult =pVideoEncoder->QueryInterface( &pWrapperFilter );

hResult =pWrapperFilter->Init( CLSID_CWMV9EncMediaObject,

                               DMOCATEGORY_VIDEO_ENCODER );

hResult=pFilterGraph->AddFilter( pVideoEncoder, L"WMV9DMO Encoder");

Because the WMV9 encoder is a DMO, you can't add/use it like other filters. But DirectShow provides a wrapper filter that enables you to use a DMO like any other filter. You first create an instance of the DMO wrapper filter and then initialize the WMV9 encoder DMO with it. After initializing the DMO, you add it into the filter graph as follows:

IBaseFilter *pASFMultiplexer;

IFileSinkFilter *pFileSinkFilter;

hResult = pCaptureGraphBuilder->SetOutputFileName(&MEDIASUBTYPE_Asf, T("\\test.asf"), &pASFMultiplexer, &pFileSinkFilter );

You have added the source and the transform filter in the filter graph, so the last thing remaining is adding a sink filter in the graph. For this, you call the SetOutputFileName method of ICaptureGraphBuilder2. The first parameter is a media subtype; the second parameter is the name of the file in which you want to save the video; the third parameter is the address of a pointer that receives the multiplexer's interface; and the fourth parameter receives the file writers' interface.

With that, your filter graph is ready. All you need to do is connect the source filter, encoder, and multiplexer. You can achieve this by using the RenderStream method of the graph builder, as follows:

hResult = pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE,

                                         &MEDIATYPE_Video,

                                         m_pVideoCaptureFilter,

                                         pVideoEncoder,

                                         pASFMultiplexer );

 

The first parameter is the pin category, which can be NULL to match any category.The second parameter specifies the media type. The third, fourth, and fifth parameters specify a starting filter, an intermediate filter, and a sink filter, respectively. The method connects the source filter to the transform filter and then the transform filter to the sink filter.

Now your graph is ready, and you can start capturing the video.

Controlling the Graph

Before capturing video, you need two more things: the ImediaEventEx and IMediaControl pointers. IMediaEventEx derives from IMediaEvent, which supports event notification from the filter graph and individual filters to the application.ImediaEventEx provides a method to the register window that receives a message when any event occurs.

IMediaControl is an interface exposed by the filter graph that allows an application to control the streaming media through the graph. The application can use this to start, stop, or pause the running graph.The following code sample first queries the filter graph for its IMediaEventEx interface. Once it gets the pointer to the IMediaEventEx interface, it then calls its method SetNotifyWindow, passing it the handle to the window that handles the message. The second parameter is the message that will be passed as notification to the Windows message handler. The third parameter is the instance data (this can be 0):

IMediaEventEx *pMediaEvent;

IMediaControl *pMediaControl;

#define WM_GRAPHNOTIFY WM_APP+1

hResult =pFilterGraph->QueryInterface( IID_IMediaEventEx, (void**)&pMediaEvent );

hResult =pMediaEvent->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY,0);

hResult=pFilterGraph->QueryInterface(&pMediaControl);

hResult =pMediaControl->Run();

When an event occurs, DirectShow will send WM_GRAPHNOTIFY to the specified windows.

Note: WM_GRAPHNOTIFY is used here as an example. This can be any application-defined message.

Next, you get the pointer to the IMediaControl interface. You'll use this interface to control the graph. Call its Run method to put the entire graph into a running state. The following code shows how to start and stop capture by throwing the

ControlStream method of CaptureGraphBuilder:

LONGLONG dwStart  = 0, dwEnd = 0;

WORD wStartCookie = 1, wEndCookie = 2;

dwEnd=MAXLONGLONG;

 

//start capturing

hResult=pCaptureGraphBuilder->ControlStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,pVideoCaptureFilter, &dwStart, &dwEnd,wStartCookie, wEndCookie);

 

//Stop capturing

dwStart=0;

hResult=pFilterGraph->QueryInterface(&pMediaSeeking );

hResult=pMediaSeeking->GetCurrentPosition( &dwEnd );

hResult= pCaptureGraphBuilder->ControlStream(

   &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pVideoCaptureFilter,

   &dwStart, &dwEnd, wStartCookie, wEndCookie );

The code uses the search criteria supplied in the method call to locate an output pin on the capture filter. ControlStream enables an application to control streams without it needing to enumerate filters and pins in the graph.Start and End specify the start and stop times (MAX_LONGLONG is the largest possible reference time value). When you start, the End is set to MAXLONLONG. When you want to stop, you first get the current position of the stream by using the GetCurrentPosition method of the IMediaSeeking interface. You then call the ControlStream method with Start set at 0 and End set at the current position.You now have the graph ready and running. You can start using it to capture and save in an .asf file.

Handling the Graph Events

Because an application will control the graph, you need to write the code to facilitate that. You already have registered the window and message with the filter graph, so the only thing remaining is to handle the message in the window's message handler as follows:

BOOL CALLBACK VidCapDlgProc(HWND hDlg,UINT Msg,WPARAM wParam, LPARAM lParam)

{

   ... ... ... ...

   case WM_GRAPHNOTIFY:

      {

         ProcessGraphMessage();

      }

   ... ... ... ...

}

ProcessGraphMessage()

{

   HRESULT hResult=S_OK;

   long leventCode, param1, param2;

   while(hResult=pEvent->GetEvent(&leventCode, &param1, &param2, 0),

   SUCCEEDED(hResult))

   {

      hResult = pEvent->FreeEventParams(leventCode, param1, param2);

      if (EC_STREAM_CONTROL_STOPPED == leventCode)

      {

         pMediaControl->Stop();

         break;

      }

      else if(EC_CAP_FILE_COMPLETED== leventCode)

      {

         //Handle the file capture completed event

      }

      else if(EC_CAP_FILE_WRITE_ERROR== leventCode)

      {

         //Handle the file write error event

      }

   }

}

You handle the WM_GRAPHNOTIFY message in the windows handler. DirectShow sends this message to the application when any event arises. The application calls a user-defined method to process the events. The GetEvent method of the IMediaEvent interface retrieves the event code and two event parameters from the queue.Because the message loop and event notification are asynchronous, the queue might hold more then one event. Hence, the GetEvent code is called in a loop until it returns a failure code. Also, whenever you call GetEvent, it's important to call FreeEvent to free the resource associated with the event parameter. And, being the good programmer that you are, you won't forget to release the resources afterwards, will you? Call Release on every object that you have created, as follows:

PVideoCaptureFilter->Release ();

pVideoEncoder->Release ();

pMediaEvent ->Release();

pMediaSeeking ->Release();

pASFMultiplexer->Release();

pFileSinkFilter->Release();

pWrapperFilter ->Release();

pFilterGraph->Release();

pCaptureGraphBuilder->Release();

 

What Have You Learned?

You now understand how to create, run, and control a filter graph manually. By using the DirectShow framework to capture from a camera, you gain good control with ease.

posted on 2008-11-28 14:06 井泉 閱讀(7853) 評論(10)  編輯 收藏 引用

評論

# re: 在Windows Mobile 5中使用DirectShow控制攝像頭-轉(zhuǎn)  回復(fù)  更多評論   

LZ對我?guī)椭艽?。:?

《編程使用手機(jī)攝像頭》
http://whbell.spaces.live.com/blog/cns!5EE1383181C65478!194.entry

2009-01-24 13:41 | bell.wang

# re: 在Windows Mobile 5中使用DirectShow控制攝像頭-轉(zhuǎn)  回復(fù)  更多評論   

頂一下,還不錯
2010-03-09 16:06 | nfl 2010

# re: 在Windows Mobile 5中使用DirectShow控制攝像頭-轉(zhuǎn)  回復(fù)  更多評論   

頂一下,還不錯 頂一下,還不錯
2010-06-03 00:11 | baby loopstoel

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美日韩色婷婷| 亚洲人成网站精品片在线观看| 久久www免费人成看片高清| 亚洲高清资源| 国产精品综合av一区二区国产馆| 美女脱光内衣内裤视频久久影院| 欧美一区亚洲一区| 久久久久国产精品一区三寸| 欧美专区在线播放| 久久亚洲精品欧美| 欧美成人亚洲| 欧美亚州一区二区三区| 国产精品一二三四区| 国产欧美日韩在线视频| 国产一区久久| 亚洲精品久久| 亚洲先锋成人| 久久99伊人| 久久久精品五月天| 欧美成人tv| 99在线精品视频| 午夜视频精品| 欧美电影在线| 国产欧美精品| 亚洲人成网站精品片在线观看| 国产精品99久久久久久久久久久久 | 国产日韩精品久久久| 韩日精品在线| 一区二区三区欧美在线| 性一交一乱一区二区洋洋av| 麻豆av福利av久久av| 亚洲精品国产日韩| 亚洲日本欧美日韩高观看| 亚洲午夜久久久久久久久电影院 | 欧美在线影院在线视频| 另类av导航| 国产精品久久7| 亚洲国产日韩精品| 亚洲欧美福利一区二区| 欧美成ee人免费视频| 亚洲一区二区三区高清不卡| 久久偷窥视频| 国产亚洲精品美女| 亚洲一二三四区| 亚洲第一偷拍| 久久精品人人| 国产私拍一区| 亚洲女同性videos| 亚洲精品日韩在线观看| 久久视频国产精品免费视频在线| 国产精品二区影院| 亚洲电影在线看| 久久久91精品国产一区二区三区| 久久亚洲高清| 精品成人一区二区| 欧美一区中文字幕| 亚洲精选成人| 欧美激情综合亚洲一二区| 国产综合一区二区| 欧美淫片网站| 亚洲欧美日韩国产另类专区| 欧美日韩一视频区二区| 一本久道久久久| 欧美国产一区视频在线观看| 久久久亚洲午夜电影| 国产视频一区三区| 久久久精品999| 亚洲专区在线视频| 国产精品毛片va一区二区三区 | 欧美日韩在线观看一区二区| 亚洲人成网在线播放| 欧美黄色视屏| 欧美国产在线视频| 亚洲片在线资源| 亚洲国产成人午夜在线一区 | 欧美在线资源| 狠狠久久婷婷| 欧美岛国激情| 欧美激情偷拍| 一本一本久久| 一本色道精品久久一区二区三区 | 久久性天堂网| 在线欧美视频| 亚洲精品日韩激情在线电影| 91久久国产综合久久91精品网站| 裸体丰满少妇做受久久99精品| 亚洲人成毛片在线播放| 亚洲日本在线视频观看| 欧美性事在线| 久久久久免费| 欧美连裤袜在线视频| 亚洲一区影院| 久久精品一区二区三区不卡牛牛 | 黄色国产精品一区二区三区| 男人的天堂亚洲| 欧美成人激情在线| 亚洲视频在线播放| 一区二区三区精密机械公司| 国产精品视频专区| 免费久久99精品国产| 欧美日韩国产在线播放| 午夜精品久久久久久| 老妇喷水一区二区三区| 亚洲一区二区动漫| 久久夜色精品国产| 亚洲图片欧洲图片av| 亚洲日本激情| 香蕉久久久久久久av网站| 中文无字幕一区二区三区| 国内精品久久久久久| 亚洲黄网站黄| 国内精品国产成人| 日韩午夜中文字幕| 影音先锋亚洲精品| 99riav国产精品| 永久555www成人免费| 一区二区三区四区国产精品| 亚洲国产精品999| 亚洲一区二区三区高清| 日韩一级在线观看| 欧美一区三区三区高中清蜜桃| 欧美精品一区二区在线观看| 欧美日本一区| 亚洲自拍16p| 免费亚洲婷婷| 国产一区二区三区久久久久久久久| 国产精品99久久久久久有的能看 | 久久福利精品| 国产最新精品精品你懂的| 免费在线看一区| 在线中文字幕一区| 在线午夜精品自拍| 久久综合成人精品亚洲另类欧美| 亚洲欧美卡通另类91av| 国产亚洲综合性久久久影院| 午夜亚洲影视| 欧美激情aaaa| 先锋影音久久久| 亚洲国产精品一区| 欧美日韩1区2区| 欧美亚洲免费| 亚洲综合欧美日韩| 国产精品久久久久国产a级| 欧美日韩亚洲另类| 国产视频观看一区| 欧美精品久久久久久久久久| 久久精品国产99| 久久人人97超碰国产公开结果| 另类激情亚洲| 国产精品v欧美精品∨日韩| 国产精品一页| 亚洲午夜激情网页| 欧美影视一区| 亚洲人成人99网站| 91久久精品国产| 欧美怡红院视频| 午夜精品久久久久久久久久久久| 欧美国产视频一区二区| 久久精品国产999大香线蕉| 免费的成人av| 亚洲国产你懂的| 91久久精品一区二区别| 国模大胆一区二区三区| 欧美91大片| 亚洲免费av片| 欧美日韩aaaaa| 亚洲天堂av在线免费| 午夜在线精品| 国产亚洲欧洲| 免费一区视频| 亚洲免费久久| 欧美在线日韩在线| 亚洲国产99| 欧美日韩在线影院| 蜜臀av国产精品久久久久| 看欧美日韩国产| 欧美激情视频一区二区三区在线播放| 亚洲欧洲日产国产综合网| 欧美激情黄色片| 亚洲一区二区三区免费在线观看 | 亚洲女女做受ⅹxx高潮| 国产欧美精品日韩| 欧美成人午夜影院| 久久成人国产精品| 玉米视频成人免费看| 欧美精品在线免费| 亚洲一区观看| 91久久在线观看| 亚洲永久视频| 亚洲国产专区校园欧美| 国产精品久久久久久久免费软件 | 亚洲女人天堂成人av在线| 欧美成在线观看| 欧美日韩日本视频| 日韩视频免费大全中文字幕| 久久国产综合精品| 99国产精品国产精品久久| 国产在线乱码一区二区三区| aa成人免费视频| 欧美日韩成人在线|