(原)Microsoft Source Reader的简单使用
感觉Microsoft Source Reader还是比较坑的,只是由于需要,不得不使用。其实按照Microsoft提供的示例,基本上可以正常的调试出程序来。
下面的例子,简单的给出了Source Reader的代码。同时,HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource)函数中,使用了指针的指针,能正确的传出ppSource,不是NULL。之前调试时,使用HRESULT CreateVideoDeviceSource(IMFMediaSource *ppSource),出来后,ppSource依旧为NULL。后来直接把代码展开,没有继续深入考虑。感谢前两天@wuruifang 的建议,之后在网上搜了一下,指针的指针可以使得输入参数为NULL时输出正常。因而此处重新写成一个函数(以前不清楚为啥用指针的指针时出错,现在倒是没有)。
主要代码:
int _tmain(int argc, _TCHAR* argv[])
{
IMFMediaSource *ppSource = NULL;
CreateVideoDeviceSource(&ppSource); // hr = EnumerateCaptureFormats(ppSource); // This can show the formats the camera support. Notice that output window of visual studio shows the infomation.
// if (FAILED(hr))
// abort();
HRESULT hr;
IMFSourceReader *pReader;
hr = MFCreateSourceReaderFromMediaSource(ppSource, NULL, &pReader);
if (FAILED(hr))
abort(); hr = SetDeviceFormat(ppSource, ); //I need to configure the camera to format 6.
if (FAILED(hr))
abort(); ProcessSamples(pReader); SafeRelease(&pReader);
SafeRelease(&ppSource);
MFShutdown();
CoUninitialize();
} HRESULT ProcessSamples(IMFSourceReader *pReader)
{
HRESULT hr = S_OK;
IMFSample *pSample = NULL;
size_t cSamples = ; _LARGE_INTEGER time_start; /*begin time */
_LARGE_INTEGER time_over; /*end time*/
double dqFreq; /*timer frequence*/
LARGE_INTEGER f; /*timer frequence*/
QueryPerformanceFrequency(&f);
dqFreq = (double)f.QuadPart; QueryPerformanceCounter(&time_start); bool quit = false;
while (!quit)
{
DWORD streamIndex, flags;
LONGLONG llTimeStamp; hr = pReader->ReadSample(
MF_SOURCE_READER_ANY_STREAM, // Stream index.
, // Flags.
&streamIndex, // Receives the actual stream index.
&flags, // Receives status flags.
&llTimeStamp, // Receives the time stamp.
&pSample // Receives the sample or NULL.
); if (FAILED(hr))
break; if (flags & MF_SOURCE_READERF_ENDOFSTREAM)
{
wprintf(L"\tEnd of stream\n");
quit = true;
} if (pSample)
{
BYTE* data;
IMFMediaBuffer* buffer;
DWORD max, current; // printf(" cSamples = %d\n", cSamples);
++cSamples;
pSample->GetBufferByIndex(, &buffer);
buffer->Lock(&data, &max, ¤t); // saveBMP(data, cSamples, IMGWIDTH, IMGHEIGHT); buffer->Unlock();
SafeRelease(&buffer); QueryPerformanceCounter(&time_over); //In order to find the frames per second of the camera.
double usedtime = ((time_over.QuadPart - time_start.QuadPart) / dqFreq);
if (usedtime>)
{
printf(" cSamples = %d\n", cSamples);
cSamples = ;
QueryPerformanceCounter(&time_start);
}
}
SafeRelease(&pSample);
} SafeRelease(&pSample);
return hr;
} HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource)
{
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
abort();
hr = MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET);
if (FAILED(hr))
abort(); *ppSource = NULL; IMFMediaSource *pSource = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL; // Create an attribute store to specify the enumeration parameters.
/*HRESULT*/ hr = MFCreateAttributes(&pAttributes, );
if (FAILED(hr))
abort(); // Source type: video capture devices
hr = pAttributes->SetGUID( MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID );
if (FAILED(hr))
abort(); // Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
if (FAILED(hr))
abort();
if (count == )
{
hr = E_FAIL;
return hr;
} // Create the media source object.
hr = ppDevices[]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
abort(); *ppSource = pSource;
(*ppSource)->AddRef(); // release part
SafeRelease(&pAttributes); for (DWORD i = ; i < count; i++)
{
SafeRelease(&ppDevices[i]);
}
CoTaskMemFree(ppDevices);
SafeRelease(&pSource); //此处不确定,是否需要SafeRelease。
return hr;
}
得到的图片(程序中保存的是BMP图片,由于cnblogs不支持BMP,因而转成jpg):

完整代码(不会插入超链接,抱歉)见:https://github.com/darkknightzh/Microsoft-Source-Reader
参考(其他的记不清了,见谅):
Microsoft :http://msdn.microsoft.com/en-us/library/windows/desktop/dd389281(v=vs.85).aspx
指针的指针(详见4楼,14楼) :http://bbs.csdn.net/topics/210076970
(原)Microsoft Source Reader的简单使用的更多相关文章
- 【FLYabroad 】微软内部代码检查工具 (Microsoft Source Analysis for C#)[转]
SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. 早就听说了微软内部的静态代码检查和代码强制格式美化工具 StyleCop ...
- C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件
项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...
- Microsoft.AspNet.Identity 的简单使用
要完成一个简单的注册,登陆,至少需要实现Identity中的3个接口 IUser IUserStore<TUser> : IDisposable where TUser : IUser I ...
- [原][译][lua][luabridge]一个简单的luabridge与c++例子结合例子
参考:https://eliasdaler.wordpress.com/tag/luabridge/ https://eliasdaler.wordpress.com/2015/08/10/using ...
- Source Insight 项目简单使用说明
SI(Source Insight) 是我一直写代码的好伙伴, 相信这强大的软件也是广大程序猿编写软件的利器. 正所谓" 工欲善其事, 必先利其器", 我们要学会利用这款软件. 先 ...
- 【原】shell编写一个简单的jmeter自动化压测脚本
在公司做压力测试也挺长时间了,每次测试前环境数据准备都需要话费较长时间,所以一直在考虑能不能将整个过程实现自动化进行,于是就抽空写了一个自动化脚本,当然这个脚本目前功能十分简陋,代码也不完善,很有很多 ...
- Android系统--输入系统(十一)Reader线程_简单处理
Android系统--输入系统(十一)Reader线程_简单处理 1. 引入 Reader线程主要负责三件事情 获得输入事件 简单处理 上传给Dispatch线程 InputReader.cpp vo ...
- Java IO: Reader And Writer
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) Java IO的Reader和Writer除了基于字符之外,其他方面都与InputStre ...
- 给Source Insight做个外挂系列之四--分析“Source Insight”
外挂的目的就是将代码注入到其它进程中,所以必须要有目标进程才能完成注入,而所谓的目标进程通常是某软件的一部分或者是全部,所以要对目标程序有深入地了解.一般外挂都是针对某个应用程序开发的,其装载.运行都 ...
随机推荐
- C#检测串口被拔掉等一些触发事件合集
// //设备异常重载 // protected override void WndProc(ref Message m) { if (m.Msg == 0x0219) {//设备被拔出 if (m. ...
- (原)10-folder交叉验证
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069731.html 参考网址: https://github.com/cmusatyalab/ope ...
- DOM 之selection
有关文章的集合 MOZILLA 开发者网络 selection: MOZILLA DEVELOPER NETWORK document.activeElement MOZILLA DEVELOPER ...
- Python Set集合,函数,深入拷贝,浅入拷贝,文件处理
1.Set基本数据类型 a.set集合,是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set o ...
- 制作 leanote docker 镜像
leanote 使用 mongodb 存储数据,如果把 mongodb 单独做成一个镜像,初始化数据时比较麻烦,所以最后还是决定把 mongodb 和 leanote 放到同一个镜像里边. docke ...
- Linux - Reset a MySQL root password
Use the following steps to reset a MySQL root password by using the command line interface. Stop the ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- scaletype
http://www.myexception.cn/image/726203.html 图片说明Andorid中ImageView的不同属性ScaleType的区别 ImageView是Android ...
- OpenSSl 加密解密 示例(终于有编程实践了)
OPenSSl的加密主要有三个重要的函数.看懂下面的代码就基本上知道该如何使用openssL来加密了. 不过注意,要先将libssl.so.1.0和libcrypto.so.1.0文件复制到执行的文件 ...