سلام کسی اگه کامپونتتی که بشه با وبکم ارتباط برقرار کنه معرفی کنه ممنون میشم .
من سرچ زدم و فایلهایی پیدا کردم که بشه باهاش عکس گرفت ولی میخوام که فیلم برداری کنم با استفاده از وبکم
Printable View
سلام کسی اگه کامپونتتی که بشه با وبکم ارتباط برقرار کنه معرفی کنه ممنون میشم .
من سرچ زدم و فایلهایی پیدا کردم که بشه باهاش عکس گرفت ولی میخوام که فیلم برداری کنم با استفاده از وبکم
آقا همچین چیزی (Dll - Ocx .....) وجود نداره؟!؟؟!
C#
public class CaptureDevice : IVideoSource
{
private string source;
private object userData = null;
private int framesReceived;
private Thread thread = null;
private ManualResetEvent stopEvent = null;
// new frame event
public event CameraEventHandler NewFrame;
// VideoSource property
public virtual string VideoSource
{
get { return source; }
set { source = value; }
}
// Login property
public string Login
{
get { return null; }
set { }
}
// Password property
public string Password
{
get { return null; }
set { }
}
// FramesReceived property
public int FramesReceived
{
get
{
int frames = framesReceived;
framesReceived = 0;
return frames;
}
}
// BytesReceived property
public int BytesReceived
{
get { return 0; }
}
// UserData property
public object UserData
{
get { return userData; }
set { userData = value; }
}
// Get state of the video source thread
public bool Running
{
get
{
if (thread != null)
{
if (thread.Join(0) == false)
return true;
// the thread is not running, so free resources
Free();
}
return false;
}
}
// Constructor
public CaptureDevice()
{
}
// Start work
public void Start()
{
if (thread == null)
{
framesReceived = 0;
// create events
stopEvent = new ManualResetEvent(false);
// create and start new thread
thread = new Thread(new ThreadStart(WorkerThread));
thread.Name = source;
thread.Start();
}
}
// Signal thread to stop work
public void SignalToStop()
{
// stop thread
if (thread != null)
{
// signal to stop
stopEvent.Set();
}
}
// Wait for thread stop
public void WaitForStop()
{
if (thread != null)
{
// wait for thread stop
thread.Join();
Free();
}
}
// Abort thread
public void Stop()
{
if (this.Running)
{
thread.Abort();
// WaitForStop();
}
}
// Free resources
private void Free()
{
thread = null;
// release events
stopEvent.Close();
stopEvent = null;
}
// Thread entry point
public void WorkerThread()
{
// grabber
Grabber grabber = new Grabber(this);
// objects
object graphObj = null;
object sourceObj = null;
object grabberObj = null;
// interfaces
IGraphBuilder graph = null;
IBaseFilter sourceBase = null;
IBaseFilter grabberBase = null;
ISampleGrabber sg = null;
IMediaControl mc = null;
try
{
// Get type for filter graph
Type srvType = Type.GetTypeFromCLSID(Clsid.FilterGraph);
if (srvType == null)
throw new ApplicationException("Failed creating filter graph");
// create filter graph
graphObj = Activator.CreateInstance(srvType);
graph = (IGraphBuilder) graphObj;
// ----
UCOMIBindCtx bindCtx = null;
UCOMIMoniker moniker = null;
int n = 0;
// create bind context
if (Win32.CreateBindCtx(0, out bindCtx) == 0)
{
// convert moniker`s string to a moniker
if (Win32.MkParseDisplayName(bindCtx, source, ref n, out moniker) == 0)
{
// get device base filter
Guid filterId = typeof(IBaseFilter).GUID;
moniker.BindToObject(null, null, ref filterId, out sourceObj);
Marshal.ReleaseComObject(moniker);
moniker = null;
}
Marshal.ReleaseComObject(bindCtx);
bindCtx = null;
}
// ----
if (sourceObj == null)
throw new ApplicationException("Failed creating device object for moniker");
sourceBase = (IBaseFilter) sourceObj ;
// Get type for sample grabber
srvType = Type.GetTypeFromCLSID(Clsid.SampleGrabber);
if (srvType == null)
throw new ApplicationException("Failed creating sample grabber");
// create sample grabber
grabberObj = Activator.CreateInstance(srvType);
sg = (ISampleGrabber) grabberObj;
grabberBase = (IBaseFilter) grabberObj;
// add source filter to graph
graph.AddFilter(sourceBase, "source");
graph.AddFilter(grabberBase, "grabber");
// set media type
AMMediaType mt = new AMMediaType();
mt.majorType = MediaType.Video;
mt.subType = MediaSubType.RGB24;
sg.SetMediaType(mt);
// connect pins
if (graph.Connect(DSTools.GetOutPin(sourceBase, 0), DSTools.GetInPin(grabberBase, 0)) < 0)
throw new ApplicationException("Failed connecting filters");
// get media type
if (sg.GetConnectedMediaType(mt) == 0)
{
VideoInfoHeader vih = (VideoInfoHeader) Marshal.PtrToStructure(mt.formatPtr, typeof(VideoInfoHeader));
System.Diagnostics.Debug.WriteLine("width = " + vih.BmiHeader.Width + ", height = " + vih.BmiHeader.Height);
grabber.Width = vih.BmiHeader.Width;
grabber.Height = vih.BmiHeader.Height;
mt.Dispose();
}
// render
graph.Render(DSTools.GetOutPin(grabberBase, 0));
//
sg.SetBufferSamples(false);
sg.SetOneShot(false);
sg.SetCallback(grabber, 1);
// window
IVideoWindow win = (IVideoWindow) graphObj;
win.put_AutoShow(false);
win = null;
// get media control
mc = (IMediaControl) graphObj;
// run
mc.Run();
while (!stopEvent.WaitOne(0, true))
{
Thread.Sleep(100);
}
mc.StopWhenReady();
}
// catch any exceptions
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("----: " + e.Message);
}
// finalization block
finally
{
// release all objects
mc = null;
graph = null;
sourceBase = null;
grabberBase = null;
sg = null;
if (graphObj != null)
{
Marshal.ReleaseComObject(graphObj);
graphObj = null;
}
if (sourceObj != null)
{
Marshal.ReleaseComObject(sourceObj);
sourceObj = null;
}
if (grabberObj != null)
{
Marshal.ReleaseComObject(grabberObj);
grabberObj = null;
}
}
}
// new frame
protected void OnNewFrame(Bitmap image)
{
framesReceived++;
if ((!stopEvent.WaitOne(0, true)) && (NewFrame != null))
NewFrame(this, new CameraEventArgs(image));
}
// Grabber
private class Grabber : ISampleGrabberCB
{
private CaptureDevice parent;
private int width, height;
// Width property
public int Width
{
get { return width; }
set { width = value; }
}
// Height property
public int Height
{
get { return height; }
set { height = value; }
}
// Constructor
public Grabber(CaptureDevice parent)
{
this.parent = parent;
}
//
public int SampleCB(double SampleTime, IntPtr pSample)
{
return 0;
}
// Callback method that receives a pointer to the sample buffer
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
// create new image
System.Drawing.Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);
// lock bitmap data
BitmapData bmData = img.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// copy image data
int srcStride = bmData.Stride;
int dstStride = bmData.Stride;
int dst = bmData.Scan0.ToInt32() + dstStride * (height - 1);
int src = pBuffer.ToInt32();
for (int y = 0; y < height; y++)
{
Win32.memcpy(dst, src, srcStride);
dst -= dstStride;
src += srcStride;
}
// unlock bitmap data
img.UnlockBits(bmData);
// notify parent
parent.OnNewFrame(img);
// release the image
img.Dispose();
return 0;
}
}
}
public interface IVideoSource
{
/// <summary>
/// New frame event - notify client about the new frame
/// </summary>
event CameraEventHandler NewFrame;
/// <summary>
/// Video source property
/// </summary>
string VideoSource{get; set;}
/// <summary>
/// Login property
/// </summary>
string Login{get; set;}
/// <summary>
/// Password property
/// </summary>
string Password{get; set;}
/// <summary>
/// FramesReceived property
/// get number of frames the video source received from the last
/// access to the property
/// </summary>
int FramesReceived{get;}
/// <summary>
/// BytesReceived property
/// get number of bytes the video source received from the last
/// access to the property
/// </summary>
int BytesReceived{get;}
/// <summary>
/// UserData property
/// allows to associate user data with an object
/// </summary>
object UserData{get; set;}
/// <summary>
/// Get state of video source
/// </summary>
bool Running{get;}
/// <summary>
/// Start receiving video frames
/// </summary>
void Start();
/// <summary>
/// Stop receiving video frames
/// </summary>
void SignalToStop();
/// <summary>
/// Wait for stop
/// </summary>
void WaitForStop();
/// <summary>
/// Stop work
/// </summary>
void Stop();
}
public delegate void CameraEventHandler(object sender, CameraEventArgs e);
/// <summary>
/// Camera event arguments
/// </summary>
public class CameraEventArgs : EventArgs
{
private System.Drawing.Bitmap bmp;
// Constructor
public CameraEventArgs(System.Drawing.Bitmap bmp)
{
this.bmp = bmp;
}
// Bitmap property
public System.Drawing.Bitmap Bitmap
{
get { return bmp; }
}
}
[ComImport,
Guid("56A868B1-0AD4-11CE-B03A-0020AF0BA770"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMediaControl
{
// Switches the entire filter graph into running mode
[PreserveSig]
int Run();
// Pauses all filters in the filter graph
[PreserveSig]
int Pause();
// Switches all filters in the filter graph to a stopped state
[PreserveSig]
int Stop();
// Retrieves the state of the filter graph
[PreserveSig]
int GetState(
int msTimeout,
out int pfs);
// Adds and connects filters needed to play the specified file
[PreserveSig]
int RenderFile(
string strFilename);
// Adds to the graph the source filter that can read the given file name
[PreserveSig]
int AddSourceFilter(
[In] string strFilename,
[Out, MarshalAs(UnmanagedType.IDispatch)] out object ppUnk);
//
[PreserveSig]
int get_FilterCollection(
[Out, MarshalAs(UnmanagedType.IDispatch)] out object ppUnk);
//
[PreserveSig]
int get_RegFilterCollection(
[Out, MarshalAs(UnmanagedType.IDispatch)] out object ppUnk);
// Waits for an operation such as Pause to complete,
// allowing filters to queue up data, then stops the filter graph
[PreserveSig]
int StopWhenReady();
}
[ComImport,
Guid("56A868A9-0AD4-11CE-B03A-0020AF0BA770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IGraphBuilder
{
// --- IFilterGraph Methods
// Adds a filter to the graph and names it
// by using the pName parameter
[PreserveSig]
int AddFilter(
[In] IBaseFilter pFilter,
[In, MarshalAs(UnmanagedType.LPWStr)] string pName);
// Removes a filter from the graph
[PreserveSig]
int RemoveFilter(
[In] IBaseFilter pFilter);
// Provides an enumerator for all filters in the graph
[PreserveSig]
// int EnumFilters(
// [Out] out IEnumFilters ppEnum);
int EnumFilters(
[Out] out IntPtr ppEnum);
// Finds a filter that was added
// to the filter graph with a specific name
[PreserveSig]
int FindFilterByName(
[In, MarshalAs(UnmanagedType.LPWStr)] string pName,
[Out] out IBaseFilter ppFilter);
// Connects the two pins directly
[PreserveSig]
int ConnectDirect(
[In] IPin ppinOut,
[In] IPin ppinIn,
[In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
// Disconnects this and the pin to which it connects and
// then reconnects it to the same pin
[PreserveSig]
int Reconnect(
[In] IPin ppin);
// Disconnects this pin
[PreserveSig]
int Disconnect(
[In] IPin ppin);
// Sets the default source of synchronization
[PreserveSig]
int SetDefaultSyncSource();
// --- IGraphBuilder methods
// Connects the two pins, using intermediates if necessary
[PreserveSig]
int Connect(
[In] IPin ppinOut,
[In] IPin ppinIn);
// Builds a filter graph that renders the data from this output pin
[PreserveSig]
int Render(
[In] IPin ppinOut);
// Builds a filter graph that renders the specified file
[PreserveSig]
int RenderFile(
[In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFile,
[In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrPlayList);
// Adds a source filter to the filter graph for a specific file
[PreserveSig]
int AddSourceFilter(
[In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFileName,
[In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFilterName,
[Out] out IBaseFilter ppFilter);
// Sets the file into which actions taken in attempting
// to perform an operation are logged
[PreserveSig]
int SetLogFile(IntPtr hFile);
//
[PreserveSig]
int Abort();
//
[PreserveSig]
int ShouldOperationContinue();
}
[ComImport,
Guid("56A86895-0AD4-11CE-B03A-0020AF0BA770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IBaseFilter
{
// --- IPersist Methods
// Retrieves the class identifier (CLSID) of an object
[PreserveSig]
int GetClassID(
[Out] out Guid pClassID);
// --- IMediaFilter Methods
// Informs the filter to transition to the new state
[PreserveSig]
int Stop();
// Informs the filter to transition to the new (paused) state
[PreserveSig]
int Pause();
// Informs the filter to transition to the new (running) state
[PreserveSig]
int Run(
long tStart);
// Determines the state of the filter
[PreserveSig]
int GetState(
int dwMilliSecsTimeout,
[Out] out int filtState);
// Identifies the reference clock to which the
// filter should synchronize activity
[PreserveSig]
// int SetSyncSource(
// [In] IReferenceClock pClock);
int SetSyncSource(
[In] IntPtr pClock);
// Retrieves the current reference clock in use by this filter
[PreserveSig]
// int GetSyncSource(
// [Out] out IReferenceClock pClock);
int GetSyncSource(
[Out] out IntPtr pClock);
// --- IBaseFilter Methods
// Enumerates the pins on this filter
[PreserveSig]
int EnumPins(
[Out] out IEnumPins ppEnum);
// Retrieves the pin with the specified identifier
[PreserveSig]
int FindPin(
[In, MarshalAs(UnmanagedType.LPWStr)] string Id,
[Out] out IPin ppPin);
// Retrieves information about the filter
[PreserveSig]
int QueryFilterInfo(
[Out] FilterInfo pInfo);
// Notifies the filter that it has joined or left the filter graph
[PreserveSig]
int JoinFilterGraph(
[In] IFilterGraph pGraph,
[In, MarshalAs(UnmanagedType.LPWStr)] string pName);
// Retrieves a string containing vendor information
[PreserveSig]
int QueryVendorInfo(
[Out, MarshalAs(UnmanagedType.LPWStr)] out string pVendorInfo);
}
// ISampleGrabber interface
//
// This interface provides methods for retrieving individual
// media samples as they move through the filter graph
//
[ComImport,
Guid("6B652FFF-11FE-4FCE-92AD-0266B5D7C78F"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface ISampleGrabber
{
// Specifies whether the filter should stop the graph
// after receiving one sample
[PreserveSig]
int SetOneShot(
[In, MarshalAs(UnmanagedType.Bool)] bool OneShot);
// Specifies the media type for the connection on
// the Sample Grabber's input pin
[PreserveSig]
int SetMediaType(
[In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
// Retrieves the media type for the connection on
// the Sample Grabber's input pin
[PreserveSig]
int GetConnectedMediaType(
[Out, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
// Specifies whether to copy sample data into a buffer
// as it goes through the filter
[PreserveSig]
int SetBufferSamples(
[In, MarshalAs(UnmanagedType.Bool)] bool BufferThem);
// Retrieves a copy of the sample that
// the filter received most recently
[PreserveSig]
int GetCurrentBuffer(
ref int pBufferSize,
IntPtr pBuffer);
//
[PreserveSig]
int GetCurrentSample(
IntPtr ppSample);
// Specifies a callback method to call on incoming samples
[PreserveSig]
int SetCallback(
ISampleGrabberCB pCallback,
int WhichMethodToCallback);
}
// ISampleGrabberCB interface
//
// This interface provides callback methods for the
// ISampleGrabber::SetCallback method
//
[ComImport,
Guid("0579154A-2B53-4994-B0D0-E773148EFF85"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface ISampleGrabberCB
{
// Callback method that receives a pointer to the media sample
[PreserveSig]
// int SampleCB(
// double SampleTime,
// IMediaSample pSample);
int SampleCB(
double SampleTime,
IntPtr pSample);
// Callback method that receives a pointer to the sample buffer
[PreserveSig]
int BufferCB(
double SampleTime,
IntPtr pBuffer,
int BufferLen);
}
اگر چیزی رو فراموش کردم pm بزن.
تو تاپیکام بگرد چیزای خوبی پیدا میکنی یکیشون خیلی کامله
هنوز چک نکردم ولی در هر صورت ممنون.