标题:摄像头编程大全(源码)
只看楼主
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
结帖率:100%
 问题点数:0 回复次数:28 
摄像头编程大全(源码)

using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace PickHead
{
/// <summary>
/// 一个控制摄像头的类
/// </summary>
public class Pick
{
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;
private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;
private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;
private IntPtr hWndC;
private bool bStat = false;

private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;

/// <summary>
/// 初始化摄像头
/// </summary>
/// <param name="handle">控件的句柄</param>
/// <param name="left">开始显示的左边距</param>
/// <param name="top">开始显示的上边距</param>
/// <param name="width">要显示的宽度</param>
/// <param name="height">要显示的长度</param>
public Pick(IntPtr handle, int left, int top, int width,int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}

[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);

[DllImport("avicap32.dll")]
private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);

/// <summary>
/// 开始显示图像
/// </summary>
public void Start()
{
if (bStat)
return;

bStat = true;
byte[] lpszName = new byte[100];

hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);

if (hWndC.ToInt32() != 0)
{
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
}

return;


}

/// <summary>
/// 停止显示
/// </summary>
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bStat = false;
}

/// <summary>
/// 抓图
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(string path)
{

IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());

}

/// <summary>
/// 录像
/// </summary>
/// <param name="path">要保存avi文件的路径</param>
public void Kinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64());
SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}

/// <summary>
/// 停止录像
/// </summary>
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}

}
}



C#捕捉视频头(源码)

using System;
using System.Runtime.InteropServices;

namespace Wuyin.ShoesManager
{
/// <summary>
/// VedioCapture 的摘要说明。
/// </summary>
public class VedioCapture
{
private int hCaptureM;
private bool isUnLoad = false;
public VedioCapture()
{
}
[DllImport("avicap32.dll")]
private static extern int capCreateCaptureWindow( string strWindowName, int dwStyle, int x, int y ,int width, int height , int hwdParent, int nID );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , int lParam );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , string lParam );
[DllImport("Kernel32.dll")]
private static extern bool CloseHandle( int hObject );
public bool Initialize( System.Windows.Forms.Control aContainer , int intWidth, int intHeight )
{
hCaptureM = capCreateCaptureWindow( "", 0x40000000 | 0x10000000, 0,0,intWidth,intHeight,aContainer.Handle.ToInt32() ,1 );
if( hCaptureM == 0 ) return false;

int ret = SendMessage( hCaptureM , 1034, 0,0 );
if( ret == 0 )
{
CloseHandle(hCaptureM);
return false;
}
//WM_CAP_SET_PREVIEW
ret = SendMessage( hCaptureM, 1074, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_SCALE
ret = SendMessage( hCaptureM, 1077, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_PREVIEWRATE
ret = SendMessage( hCaptureM, 1076, 66, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
return true;
}

public void SingleFrameBegin()
{
//
int ret = SendMessage( hCaptureM, 1094 , 0, 0 );
}
public void SingleFrameEnd()
{
//
int ret = SendMessage( hCaptureM, 1095 , 0, 0 );
}

public void SingleFrameMode()
{
//WM_CAP_GRAB_FRAME
int ret = SendMessage( hCaptureM, 1084 , 0, 0 );
//WM_CAP_SET_PREVIEW
//int ret = SendMessage( hCaptureM, 1074 , 0, 0 );
//WM_CAP_SINGLE_FRAME
//ret = SendMessage( hCaptureM, 1096 , 0, 0 );
}
public void PreviewMode()
{
int ret = SendMessage( hCaptureM, 1074 , 1, 0 );
}

public void UnLoad()
{
int ret = SendMessage( hCaptureM, 1035, 0, 0 );
CloseHandle( this.hCaptureM );
isUnLoad = true;
}

public void CopyToClipBorad()
{
int ret = SendMessage( hCaptureM, 1054, 0, 0 );
}

public void ShowFormatDialog()
{
int ret = SendMessage( hCaptureM, 1065, 0, 0 );
}
public void SaveToDIB( string fileName )
{
int ret = SendMessage( hCaptureM, 1049, 0, fileName );
}

public void ShowDisplayDialog()
{
int ret = SendMessage( hCaptureM, 1067, 0, 0 );
}
public System.Drawing.Image getCaptureImage()
{
System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
System.Drawing.Image retImage = null;
if( iData != null )
{
if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Bitmap ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Bitmap );
}
else if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Dib ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Dib );
}
}
return retImage;
}

~VedioCapture()
{
if( !isUnLoad )
{
this.UnLoad();
}
}
}
}

搜索更多相关主题的帖子: 摄像头 源码 大全 
2007-01-27 08:49
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
上面上两种方法用两种不同的色彩表示

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-01-27 08:49
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
得分:0 
感谢分享!

2007-01-27 09:04
magic_ln
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2005-12-20
得分:0 

收藏起来。谢啦


当你对自己满意时,那便是退步的开始!    
2007-01-27 09:48
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
得分:0 
好东西,加个精化.呵.

飘过~~
2007-01-27 19:44
ghdjby
Rank: 1
等 级:禁止访问
帖 子:189
专家分:0
注 册:2007-1-10
得分:0 
楼主请问一下avicap32.dll和User32.dll是你的摄像头驱动的动态链接库吗??你是怎么获取里面的参数的????
2007-01-27 22:07
IT浪子
Rank: 4
来 自:Nyist
等 级:贵宾
威 望:13
帖 子:402
专家分:20
注 册:2006-11-24
得分:0 
very niu!

你不能让所有人都满意,因为不是所有人都是人!
2007-01-27 23:12
YSKING
Rank: 5Rank: 5
来 自:中国绿城
等 级:贵宾
威 望:16
帖 子:1380
专家分:25
注 册:2006-11-11
得分:0 
看看

仍然自由自我,永远高唱我歌,走遍千里...
2007-01-27 23:34
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
user32.dll是我们都知道的系统类库,提供了许多常用操作功能,
avicap32.dll是系统自带处理摄像头一类的类库,很少人去使用它,所以难以发现,我也是从网上得知如此接口,06年底才看通的,发起来共享一下!

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-01-29 08:22
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
得分:0 
貌似不错,不过最近没时间呢~~~   jacklee几岁了?在工作还是读书?
2007-01-29 10:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-118373-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.153980 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved