Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Capturing Video from Web-camera on Windows 7 and 8 by using Media Foundation

, 10 Apr 2013 CPOL
Simple lib for capturing video from web-camera by using Media Foundation
SimpleCapture-exe.zip
Release
msvcp110.dll
msvcr110.dll
SimpleCapture.exe
SimpleCaptureVS2012.zip
SimpleCapture
Backup
SimpleCapture.aps
SimpleCapture.v11.suo
SimpleCapture.vcxproj.filters
SimpleCapture.vcxproj.user
TestResults
videoInput.lib
TestVideoInputVS2012x86-noexe.zip
TestVideoInput
Debug
TestVideoInput.ilk
ipch
testvideoinput-d1a6bc1c
testvideoinput-2a8f7f73.ipch
TestVideoInput.v11.suo
TestVideoInput
include
opencv
opencv2
calib3d
contrib
core
features2d
flann
gpu
device
detail
highgui
imgproc
legacy
ml
nonfree
objdetect
ocl
photo
stitching
detail
superres
ts
video
videostab
lib
opencv_core248d.lib
opencv_highgui248d.lib
TestVideoInput.vcxproj.filters
videoInput
videoInput.vcxproj.filters
videoInput.vcxproj.user
TestVideoInputVS2012x86.zip
opencv_core248d.dll
opencv_highgui248d.dll
TestVideoInput.exe
TestVideoInput.ilk
TestVideoInput.pdb
testvideoinput-2a8f7f73.ipch
TestResults
TestVideoInput.v11.suo
opencv_core248d.lib
opencv_highgui248d.lib
TestVideoInput.vcxproj.filters
videoInput.vcxproj.filters
videoInput.vcxproj.user
videoinput-staticlib-vs2012x86-noexe.zip
videoInput.lib
videoInput-staticlib-VS2012x86.zip
videoInput.lib
videoInputVS2012.zip
videoInput
videoInput.vcxproj.filters
videoInput.vcxproj.user
#pragma once

#include <guiddef.h>


struct IMFMediaSource;

// Structure for collecting info about types of video, which are supported by current video device
struct MediaType
{
	unsigned int MF_MT_FRAME_SIZE;

	unsigned int height;

	unsigned int width;
			
	unsigned int MF_MT_YUV_MATRIX;
	
	unsigned int MF_MT_VIDEO_LIGHTING;
	
	unsigned int MF_MT_DEFAULT_STRIDE;
	
	unsigned int MF_MT_VIDEO_CHROMA_SITING;
	
	GUID MF_MT_AM_FORMAT_TYPE;
	
	wchar_t *pMF_MT_AM_FORMAT_TYPEName;
	
	unsigned int MF_MT_FIXED_SIZE_SAMPLES;
	
	unsigned int MF_MT_VIDEO_NOMINAL_RANGE;
	
	unsigned int MF_MT_FRAME_RATE;

	unsigned int MF_MT_FRAME_RATE_low;
	
	unsigned int MF_MT_PIXEL_ASPECT_RATIO;

	unsigned int MF_MT_PIXEL_ASPECT_RATIO_low;
	
	unsigned int MF_MT_ALL_SAMPLES_INDEPENDENT;
	
	unsigned int MF_MT_FRAME_RATE_RANGE_MIN;
	
	unsigned int MF_MT_FRAME_RATE_RANGE_MIN_low;
	
	unsigned int MF_MT_SAMPLE_SIZE;
	
	unsigned int MF_MT_VIDEO_PRIMARIES;
	
	unsigned int MF_MT_INTERLACE_MODE;
	
	unsigned int MF_MT_FRAME_RATE_RANGE_MAX;
	
	unsigned int MF_MT_FRAME_RATE_RANGE_MAX_low;

	GUID MF_MT_MAJOR_TYPE;
	
	wchar_t *pMF_MT_MAJOR_TYPEName;
	
	GUID MF_MT_SUBTYPE;
	
	wchar_t *pMF_MT_SUBTYPEName;	

	MediaType();
	~MediaType();
	void Clear();
};

// Structure for collecting info about one parametr of current video device
struct Parametr
{
	long CurrentValue;

	long Min;
	
	long Max;
	
	long Step;
	
	long Default; 
	
	long Flag;

	Parametr();
};

// Structure for collecting info about 17 parametrs of current video device
struct CamParametrs
{
	    Parametr Brightness;
        Parametr Contrast;
        Parametr Hue;
        Parametr Saturation;
        Parametr Sharpness;
        Parametr Gamma;
        Parametr ColorEnable;
        Parametr WhiteBalance;
        Parametr BacklightCompensation;
        Parametr Gain;


		Parametr Pan;
        Parametr Tilt;
        Parametr Roll;
        Parametr Zoom;
        Parametr Exposure;
        Parametr Iris;
        Parametr Focus;
};

/// The only visiable class for controlling of video devices in format singelton
class videoInput
{
public:
	virtual ~videoInput(void);

	// Getting of static instance of videoInput class
	static videoInput& getInstance(); 

	// Closing video device with deviceID
	void closeDevice(unsigned int deviceID);
	
	// Setting callback function for emergency events(for example: removing video device with deviceID) with userData
	void setEmergencyStopEvent(unsigned int deviceID, void *userData, void(*func)(int, void *));

	// Closing all devices
	void closeAllDevices();

	// Getting of parametrs of video device with deviceID
	CamParametrs getParametrs(unsigned int deviceID);

	// Setting of parametrs of video device with deviceID
	void setParametrs(unsigned int deviceID, CamParametrs parametrs);

	// Getting numbers of existence videodevices with listing in consol
	unsigned int listDevices(bool silent = false);
		
	// Getting numbers of formats, which are supported by videodevice with deviceID
	unsigned int getCountFormats(unsigned int deviceID);

	// Getting width of image, which is getting from videodevice with deviceID
	unsigned int getWidth(unsigned int deviceID);

	// Getting height of image, which is getting from videodevice with deviceID
	unsigned int getHeight(unsigned int deviceID);

	// Getting name of videodevice with deviceID
	wchar_t *getNameVideoDevice(unsigned int deviceID);
	
	// Getting interface MediaSource for Media Foundation from videodevice with deviceID
	IMFMediaSource *getMediaSource(unsigned int deviceID);
	
	// Getting format with id, which is supported by videodevice with deviceID 
	MediaType getFormat(unsigned int deviceID, int unsigned id);

	// Checking of existence of the suitable video devices
	bool isDevicesAcceable();

	// Checking of using the videodevice with deviceID
	bool isDeviceSetup(unsigned int deviceID);

	// Checking of using MediaSource from videodevice with deviceID
	bool isDeviceMediaSource(unsigned int deviceID);
	
	// Checking of using Raw Data of pixels from videodevice with deviceID
	bool isDeviceRawDataSource(unsigned int deviceID);

	// Setting of the state of outprinting info in consol
	void setVerbose(bool state);
	
	// Initialization of video device with deviceID by media type with id
	bool setupDevice(unsigned int deviceID, unsigned int id = 0);

	// Initialization of video device with deviceID by wisth w, height h and fps idealFramerate
	bool setupDevice(unsigned int deviceID, unsigned int w, unsigned int h, unsigned int idealFramerate = 30);

	// Checking of recivig of new frame from video device with deviceID 
	bool isFrameNew(unsigned int deviceID);

	// Writing of Raw Data pixels from video device with deviceID with correction of RedAndBlue flipping flipRedAndBlue and vertical flipping flipImage
	bool getPixels(unsigned int deviceID, unsigned char * pixels, bool flipRedAndBlue = false, bool flipImage = false);
	
private: 

	bool accessToDevices;
	
    videoInput(void);

	void processPixels(unsigned char * src, unsigned char * dst, unsigned int width, unsigned int height, unsigned int bpp, bool bRGB, bool bFlip);
	
	void updateListOfDevices();
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Evgeny Pereguda
Software Developer
Australia Australia
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 10 Apr 2013
Article Copyright 2013 by Evgeny Pereguda
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid