您的位置:首页 > 编程语言

简单摄像头编程代码!

2005-04-01 08:14 441 查看
ShowVideo.cs代码:
namespace webcam
{
using System;
using System.Runtime.InteropServices;
public class showVideo
{
// Methods
public showVideo()
{
}
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
[DllImport("avicap32.dll")]
public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
public static void Copy(int ptr, byte[] data)
{
showVideo.Copy(new IntPtr(ptr), data);
}
public static void Copy(IntPtr ptr, byte[] data)
{
Marshal.Copy(ptr, data, 0, data.Length);
}
public static object GetStructure(int ptr, ValueType structure)
{
return showVideo.GetStructure(new IntPtr(ptr), structure);
}
public static object GetStructure(IntPtr ptr, ValueType structure)
{
return Marshal.PtrToStructure(ptr, structure.GetType());
}
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);
[DllImport("User32.dll")]
public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
public static int SizeOf(object structure)
{
return Marshal.SizeOf(structure);
}

// Nested Types
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
[MarshalAs(UnmanagedType.Struct)]
public showVideo.BITMAPINFOHEADER bmiHeader;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=0x400)]
public int[] bmiColors;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
[MarshalAs(UnmanagedType.I4)]
public int biSize;
[MarshalAs(UnmanagedType.I4)]
public int biWidth;
[MarshalAs(UnmanagedType.I4)]
public int biHeight;
[MarshalAs(UnmanagedType.I2)]
public short biPlanes;
[MarshalAs(UnmanagedType.I2)]
public short biBitCount;
[MarshalAs(UnmanagedType.I4)]
public int biCompression;
[MarshalAs(UnmanagedType.I4)]
public int biSizeImage;
[MarshalAs(UnmanagedType.I4)]
public int biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]
public int biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]
public int biClrUsed;
[MarshalAs(UnmanagedType.I4)]
public int biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
public struct VIDEOHDR
{
[MarshalAs(UnmanagedType.I4)]
public int lpData;
[MarshalAs(UnmanagedType.I4)]
public int dwBufferLength;
[MarshalAs(UnmanagedType.I4)]
public int dwBytesUsed;
[MarshalAs(UnmanagedType.I4)]
public int dwTimeCaptured;
[MarshalAs(UnmanagedType.I4)]
public int dwUser;
[MarshalAs(UnmanagedType.I4)]
public int dwFlags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public int[] dwReserved;
}
}
}
WebCamera.cs代码:
namespace webcam
{
using System;
public class WebCamera
{
// Methods
public WebCamera(IntPtr handle, int width, int height)
{
this.mControlPtr = handle;
this.mWidth = width;
this.mHeight = height;
}
private bool capDriverConnect(IntPtr lwnd, short i)
{
return showVideo.SendMessage(lwnd, 0x40a, i, 0);
}
private bool capDriverDisconnect(IntPtr lwnd)
{
return showVideo.SendMessage(lwnd, 0x40b, (short) 0, 0);
}
private bool capPreview(IntPtr lwnd, bool f)
{
return showVideo.SendMessage(lwnd, 0x432, f, 0);
}
private bool capPreviewRate(IntPtr lwnd, short wMS)
{
return showVideo.SendMessage(lwnd, 0x434, wMS, 0);
}
private bool capSetVideoFormat(IntPtr hCapWnd, ref showVideo.BITMAPINFO BmpFormat, int CapFormatSize)
{
return showVideo.SendMessage(hCapWnd, 0x42d, CapFormatSize, ref BmpFormat);
}
public void CloseWebcam()
{
this.capDriverDisconnect(this.lwndC);
}
public void StartWebCam()
{
byte[] buffer1 = new byte[100];
byte[] buffer2 = new byte[100];
showVideo.capGetDriverDescriptionA(0, buffer1, 100, buffer2, 100);
this.lwndC = showVideo.capCreateCaptureWindowA(buffer1, 1342177280, 0, 0, this.mWidth, this.mHeight, this.mControlPtr, 0);
if (this.capDriverConnect(this.lwndC, 0))
{
showVideo.BITMAPINFO bitmapinfo1;
this.capPreviewRate(this.lwndC, 0x42);
this.capPreview(this.lwndC, true);
bitmapinfo1 = new showVideo.BITMAPINFO();
bitmapinfo1.bmiHeader.biSize = showVideo.SizeOf(bitmapinfo1.bmiHeader);
bitmapinfo1.bmiHeader.biWidth = 320;
bitmapinfo1.bmiHeader.biHeight = 240;
bitmapinfo1.bmiHeader.biPlanes = 1;
bitmapinfo1.bmiHeader.biBitCount = 0x18;
this.capSetVideoFormat(this.lwndC, ref bitmapinfo1, showVideo.SizeOf(bitmapinfo1));
showVideo.SetWindowPos(this.lwndC, 0, 0, 0, this.mWidth, this.mHeight, 6);
}
}

// Fields
private IntPtr lwndC;
private IntPtr mControlPtr;
private int mHeight;
private int mWidth;
}
}
Form1.cs代码:
namespace webcam
{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
// Methods
public Form1()
{
this.components = null;
this.InitializeComponent();
}
private void b_stop_Click(object sender, EventArgs e)
{
this.b_play.Enabled = true;
this.b_stop.Enabled = false;
this.wc.CloseWebcam();
}
private void button1_Click(object sender, EventArgs e)
{
this.b_play.Enabled = false;
this.b_stop.Enabled = true;
this.panelPreview.Size = new Size(320, 240);
this.wc = new WebCamera(this.panelPreview.Handle, this.panelPreview.Width, this.panelPreview.Height);
this.wc.StartWebCam();
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void Form1_Load(object sender, EventArgs e)
{
this.b_play.Enabled = false;
this.b_stop.Enabled = true;
this.panelPreview.Size = new Size(320, 240);
this.wc = new WebCamera(this.panelPreview.Handle, this.panelPreview.Width, this.panelPreview.Height);
this.wc.StartWebCam();
}
private void InitializeComponent()
{
this.b_play = new System.Windows.Forms.Button();
this.panelPreview = new System.Windows.Forms.Panel();
this.b_stop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// b_play
//
this.b_play.Location = new System.Drawing.Point(83, 264);
this.b_play.Name = "b_play";
this.b_play.TabIndex = 0;
this.b_play.Text = "&Play";
this.b_play.Click += new System.EventHandler(this.button1_Click);
//
// panelPreview
//
this.panelPreview.Location = new System.Drawing.Point(0, 8);
this.panelPreview.Name = "panelPreview";
this.panelPreview.Size = new System.Drawing.Size(320, 240);
this.panelPreview.TabIndex = 1;
//
// b_stop
//
this.b_stop.Enabled = false;
this.b_stop.Location = new System.Drawing.Point(163, 264);
this.b_stop.Name = "b_stop";
this.b_stop.TabIndex = 2;
this.b_stop.Text = "&Stop";
this.b_stop.Click += new System.EventHandler(this.b_stop_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 310);
this.Controls.Add(this.b_stop);
this.Controls.Add(this.panelPreview);
this.Controls.Add(this.b_play);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "GoodView test Web Camera";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
[STAThread]
private static void Main()
{
Application.Run(new Form1());
}

// Fields
private Button b_play;
private Button b_stop;
private Container components;
private Panel panelPreview;
private WebCamera wc;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: