c#摄像头编程实例

webcam(C#调用摄像头) C#调用摄像头,C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头C#调用摄像头 立即下载
 

c#摄像头编程实例(转) http://www.chenjiliang.com/Article/View.aspx?ArticleID=3974&TypeID=84

安装摄像头后,一般可以找到一个avicap32.dll文件

这是一个关于摄像头的类

using   system;
using   System.Runtime.InteropServices;

namespace   webcam
{
///  
///   avicap   的摘要说明。
///  
public   class   showVideo
{
//   showVideo   calls
[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("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,   short   wParam,   FrameEventHandler   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);
[DllImport("avicap32.dll")]public   static   extern   int   capGetVideoFormat(IntPtr   hWnd,   IntPtr   psVideoFormat,   int   wSize   );

//   constants
public   const   int   WM_USER   =   0x400;
public   const   int   WS_CHILD   =   0x40000000;
public   const   int   WS_VISIBLE   =   0x10000000;
public   const   int   SWP_NOMOVE   =   0x2;
public   const   int   SWP_NOZORDER   =   0x4;
public   const   int   WM_CAP_DRIVER_CONNECT   =   WM_USER   +   10;
public   const   int   WM_CAP_DRIVER_DISCONNECT   =   WM_USER   +   11;
public   const   int   WM_CAP_SET_CALLBACK_FRAME   =   WM_USER   +   5;
public   const   int   WM_CAP_SET_PREVIEW   =   WM_USER   +   50;
public   const   int   WM_CAP_SET_PREVIEWRATE   =   WM_USER   +   52;
public   const   int   WM_CAP_SET_VIDEOFORMAT   =   WM_USER   +   45;
  
//   Structures
[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;
}

[structlayout(layoutkind.sequential)]   public   struct   bitmapinfoheader
{
[MarshalAs(UnmanagedType.I4)]   public   Int32   biSize   ;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biWidth   ;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biHeight   ;
[MarshalAs(UnmanagedType.I2)]   public   short   biPlanes;
[MarshalAs(UnmanagedType.I2)]   public   short   biBitCount   ;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biCompression;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biSizeImage;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biClrUsed;
[MarshalAs(UnmanagedType.I4)]   public   Int32   biClrImportant;
}  

[structlayout(layoutkind.sequential)]   public   struct   bitmapinfo
{
[MarshalAs(UnmanagedType.Struct,   SizeConst=40)]   public   BITMAPINFOHEADER   bmiHeader;
[MarshalAs(UnmanagedType.ByValArray,   SizeConst=1024)]   public   Int32[]   bmiColors;
}

public   delegate   void   FrameEventHandler(IntPtr   lwnd,   IntPtr   lpVHdr);  
//   Public   methods
public   static   object   GetStructure(IntPtr   ptr,valueType   structure)
{
return   Marshal.PtrToStructure(ptr,structure.GetType());
}
public   static   object   GetStructure(int   ptr,valueType   structure)
{
return   GetStructure(new   IntPtr(ptr),structure);
}  
public   static   void   Copy(IntPtr   ptr,byte[]   data)
{
Marshal.Copy(ptr,data,0,data.Length);
}  
public   static   void   Copy(int   ptr,byte[]   data)
{
Copy(new   IntPtr(ptr),data);
}  
public   static   int   SizeOf(object   structure)
{
return   Marshal.SizeOf(structure);  
}
}

//web   camera   class
public   class   WebCamera
{
//   Constructur
public   WebCamera(IntPtr   handle,   int   width,int   height)
{
mControlPtr   =   handle;
mWidth   =   width;
mHeight   =   height;
}  
//   delegate   for   frame   callback
public   delegate   void   RecievedFrameEventHandler(byte[]   data);
public   event   RecievedFrameEventHandler   RecievedFrame;  
private   IntPtr   lwndC;   //   Holds   the   unmanaged   handle   of   the   control
private   IntPtr   mControlPtr;   //   Holds   the   managed   pointer   of   the   control
private   int   mWidth;
private   int   mHeight;  
private   showVideo.FrameEventHandler   mFrameEventHandler;   //   Delegate   instance   for   the   frame   callback   -   must   keep   alive!   gc   should   NOT   collect   it  
//   Close   the   web   camera
public   void   CloseWebcam()
{
this.capDriverDisconnect(this.lwndC);
}  
//   start   the   web   camera
public   void   StartWebCam()
{
byte[]   lpszName   =   new   byte[100];
byte[]   lpszVer   =   new   byte[100];
  
showVideo.capGetDriverDescriptionA(0,   lpszName,   100,lpszVer,   100);
this.lwndC   =   showVideo.capCreateCaptureWindowA(lpszName,   showVideo.WS_VISIBLE   +   showVideo.WS_CHILD,   0,   0,   mWidth,   mHeight,   mControlPtr,   0);
   if   (this.capDriverConnect(this.lwndC,   0))
{
this.capPreviewRate(this.lwndC,   66);
this.capPreview(this.lwndC,   true);
showVideo.BITMAPINFO   bitmapinfo   =   new   showVideo.BITMAPINFO();  
bitmapinfo.bmiHeader.biSize   =   showVideo.SizeOf(bitmapinfo.bmiHeader);
bitmapinfo.bmiHeader.biWidth   =   352;
bitmapinfo.bmiHeader.biHeight   =   288;
bitmapinfo.bmiHeader.biPlanes   =   1;
bitmapinfo.bmiHeader.biBitCount   =   24;
this.capSetVideoFormat(this.lwndC,   ref   bitmapinfo,   showVideo.SizeOf(bitmapinfo));
this.mFrameEventHandler   =   new   showVideo.FrameEventHandler(FrameCallBack);
this.capSetCallbackOnFrame(this.lwndC,   this.mFrameEventHandler);
showVideo.SetWindowPos(this.lwndC,   0,   0,   0,   mWidth   ,   mHeight   ,   6);
}  
}

//   private   functions
private   bool   capDriverConnect(IntPtr   lwnd,   short   i)
{
return   showVideo.SendMessage(lwnd,   showVideo.WM_CAP_DRIVER_CONNECT,   i,   0);
}

private   bool   capdriverdisconnect(intptr   lwnd)
{
return   showVideo.SendMessage(lwnd,   showVideo.WM_CAP_DRIVER_DISCONNECT,   0,   0);
}  
private   bool   capPreview(IntPtr   lwnd,   bool   f)
{
return   showVideo.SendMessage(lwnd,   showVideo.WM_CAP_SET_PREVIEW   ,   f,   0);
}

private   bool   cappreviewrate(intptr   lwnd,   short   wms)
{
return   showVideo.SendMessage(lwnd,   showVideo.WM_CAP_SET_PREVIEWRATE,   wMS,   0);
}  
private   bool   capSetCallbackOnFrame(IntPtr   lwnd,   showVideo.FrameEventHandler   lpProc)
{  
return   showVideo.SendMessage(lwnd,   showVideo.WM_CAP_SET_CALLBACK_FRAME,   0,   lpProc);
}

private   bool   capsetvideoformat(intptr   hcapwnd,   ref   showvideo.bitmapinfo   bmpformat,   int   capformatsize)
{
return   showVideo.SendMessage(hCapWnd,   showVideo.WM_CAP_SET_VIDEOFORMAT,   CapFormatSize,   ref   BmpFormat);
}

private   void   framecallback(intptr   lwnd,   intptr   lpvhdr)
{
showVideo.VIDEOHDR   videoHeader   =   new   showVideo.VIDEOHDR();
byte[]   VideoData;
videoHeader   =   (showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr,videoHeader);
VideoData   =   new   byte[videoHeader.dwBytesUsed];
showVideo.Copy(videoHeader.lpData   ,VideoData);
if   (this.RecievedFrame   !=   null)
this.RecievedFrame   (VideoData);
}
}

}

具体调用如下:

using   system;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   webcam;

namespace   webcam
{
///  
///   Form1   的摘要说明。
///  
public   class   Form1   :   System.Windows.Forms.Form
{
private   System.Windows.Forms.Panel   panelPreview;
private   System.Windows.Forms.Button   b_play;
private   System.Windows.Forms.Button   b_stop;
///  
///   必需的设计器变量。
///  
private   System.ComponentModel.Container   components   =   null;
WebCamera   wc;

public   form1()
{
//
//   Windows   窗体设计器支持所必需的
//
InitializeComponent();

//
//   TODO:   在   InitializeComponent   调用后添加任何构造函数代码
//
}

///  
///   清理所有正在使用的资源。
///  
protected   override   void   Dispose(   bool   disposing   )
{
if(   disposing   )
{
if   (components   !=   null)  
{
components.Dispose();
}
}
base.Dispose(   disposing   );
}

#region   windows   窗体设计器生成的代码
///  
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///  
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(280,   368);
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(8,   8);
this.panelPreview.Name   =   "panelPreview";
this.panelPreview.Size   =   new   System.Drawing.Size(344,   272);
this.panelPreview.TabIndex   =   1;
//  
//   b_stop
//  
this.b_stop.Enabled   =   false;
this.b_stop.Location   =   new   System.Drawing.Point(360,   368);
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(464,   413);
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);

}
#endregion

///  
///   应用程序的主入口点。
///  
[STAThread]
static   void   Main()  
{
Application.Run(new   Form1());
}

private   void   form1_load(object   sender,   system.eventargs   e)
{
b_play.Enabled   =   false;
b_stop.Enabled   =   true;
panelPreview.Size   =   new   Size(330,330);
wc   =   new   WebCamera(   panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();
}

private   void   button1_click(object   sender,   system.eventargs   e)
{
b_play.Enabled   =   false;
b_stop.Enabled   =   true;
panelPreview.Size   =   new   Size(330,330);
wc   =   new   WebCamera(   panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();
}

private   void   b_stop_click(object   sender,   system.eventargs   e)
{
b_play.Enabled   =   true;
b_stop.Enabled   =   false;
wc.CloseWebcam();
}
}
}

C#摄像头拍照,封装成DLL 摄像头拍照,封装成DLL安装包,可供其他程序调用 立即下载

相关推荐

c# 采集摄像头的流

c# 摄像头编程 可以采集摄像头的流 非常有启发的例子

c#摄像头编程实例(转)

安装摄像头后,一般可以找到一个avicap32.dll文件 这是一个关于摄像头的类 using system; using System.Runtime.InteropServices; namespace webcam { /// /// avicap 的摘要说明。 /// public class showVideo ...

weixin_30519071的博客 124

c#实时摄像头监控显示

该工程实现摄像头实时监控显示,默认调用电脑自带摄像头,并且能够实现拍照录像功能,录像的时候有点小bug,不过还是可以用的,已经实现了全部功能,可以使用。

C# websocket流读取摄像头,实现web页面实时监控

基于RTSP协议转发,websocket推流到web页面。websocket流读取摄像头,实现web页面实时监控。前端使用free.player.1.8进行流解析输出。

acheng 2374

C# 摄像头编程摄像头编程

摄像头编程C# 摄像头编程C# 摄像头编程 摄像头编程C# 摄像头编程C# 摄像头编程 摄像头编程C# 摄像头编程C# 摄像头编程 摄像头编程C# 摄像头编程C# 摄像头编程

多功能WebCam C#库

易于使用的C#WebCam库不受怪异的DLL引用或PInvoke调用的困扰

用c#实现摄像头连接和拍照及麦克风、扬声器音量获取及设置

再拖一个button控件,将button属性的text改为”扫描“;再拖三个button控件,属性的text分别设置为”连接“、”拍照“、”关闭“再分别拖两个button控件,text分别设置为获取麦克风音量,设置麦克风音量;再拖一个label控件,text设为待检测;再分别拖两个button控件,text分别设置为获取扬声器音量,设置扬声器音量;分别双击Form1的”扫描“、”连接“、”拍照“、”关闭“控件 ,写入以下代码。(最好不要直接复制代码,先双击控件再在出来的代码段添加代码,不然可能运行不了)

m0_65466579的博客 6573

SAP SD 顾问面试问题 consultant interview questionnaire

以下是我个人目前面试团队 SD顾问问题的小结,希望对大家有所帮助, 也可能有回答错误的地方,希望同行不吝赐教。 也希望各位顾问们讲你们遇到的一些面试问题做个跟贴,方便大家共同进步,谢谢。   面试问题清单:  一、销售订单类型影响: 可用性检查,交货类型,输出,信贷,定价,需求,报表,合作伙伴,文本,不完整日志,交货开票,订单开票,公司间开票,统计组,屏幕顺序组,参数文件...

weixin_33825683的博客 1809

网络摄像头+net_C# 外接(网口)双摄像头视频获取

【注意事项】------------------------------------1. 更新设备网络SDK时,SDK开发包【库文件】里的HCNetSDK.dll、HCCore.dll、PlayCtrl.dll、SuperRender.dll、AudioRender.dll、HCNetSDKCom文件夹、ssleay32.dll、libeay32.dll、hlog.dll、hpr.dll...

weixin_39669202的博客 1489

Windows 11安装realtek audio control

Windows 11安装realtek audio control,解决无法链接RPC问题

m0_46144825的博客 8万+

手把手教你用STM32F303和LAN9252搭建EtherCAT从站(附IO/AD/DA完整代码)

本文详细介绍了如何使用STM32F303和LAN9252搭建EtherCAT从站,涵盖硬件选型、开发环境配置、协议栈移植及IO/AD/DA功能实现。通过实战指南和完整代码,帮助开发者快速掌握工业自动化中的实时通信技术,提升系统性能与可靠性。

weixin_26802933的博客 235

C#摄像头编程实例

安装摄像头后,一般可以找到一个avicap32.dll文件,此文件中封装了对摄像头的相关操作API 然后自己写一个关于摄像头的类 using  system; using  System.Runtime.InteropServices; namespace  we

李君老师的BLOG 1860

Uncertainty-Aware Mean Teacher(UA-MT)

本文提出了一种新的基于不确定性的半监督学习框架(UA-MT),通过额外利用未标记的数据从3D MR图像中分割左心房。具体地说,本文建立了一个教师模型和一个学生模型,学生模型通过最小化标注数据上的分割监督损失和所有输入数据上的与教师模型预测输出的一致性损失进行优化。如果没有未标记输入中的注释,教师模型的预测目标可能不可靠且有噪声。:在估计不确定性 U 的指导下,我们过滤掉相对不可靠**(高不确定性),整个体积** U 的不确定性**是 {u} ∈ RH×W ×D。过滤掉高不确定性的,也就是熵大于某个值的。

Alexa_的博客 1869

C#检测摄像头被哪个进程占用

本文介绍了检测电脑中摄像头占用进程的解决方案。作者发现市面上缺乏相关方法后,提出了两步实现方案:首先使用DirectShowLib获取摄像头实例路径并简化处理;然后调用微软handle.exe工具,通过扫描句柄反查关联进程。该方法成功解决了多软件摄像头资源冲突的监控问题,能够准确识别占用特定摄像头的进程名称,为类似场景提供了实用参考。

xdlijinming的博客 334

详细教程:如何使用elasticsearch 8.x进行向量搜索

大模型的热度使得向量数据库和embedding也成了ai领域的热门话题,有别于从头开始训练一个大模型或基于基础模型进行微调的方式,embedding检索相关上下文是对大模型进行定制的各种方法中成本最低、技术实现最便捷的方式。从技术实现的角度,

qq_50790981的博客 1万+

千兆以太网 PCB 设计实战:4层板 100Ω 差分对布线,EMI 降低 6dB 实测

本文详细解析了千兆以太网 PCB 设计中的关键技术与实战经验,重点介绍了4层板上100Ω差分对的优化布线方法,通过实测数据展示了EMI降低6dB的有效方案。内容涵盖层叠设计、阻抗计算、布线技巧及EMI抑制措施,为硬件工程师提供了一套完整的高速电路设计解决方案。

weixin_29175469的博客 424

图像采集---摄像头取像程序

要入门AI,不就是眼和耳吗?所以你需要打通关键的声音采集,和图像采集。特别是编程,他的重要性不言而喻。虽然这都是玩一玩,在微软支撑下,简单,过气,但是他激发的探索和兴致(求知欲)从来不会过时,这会让你打开更加专业的basler相机,灰点(grey)相机图像采集(针对机器视觉)的大门,如果你不会对眼和耳采集的花花世界感兴趣,怎么会对算法和意识感兴趣呢? 请看整理的c#摄像头取像程序:我们给他起了一...

ganggangwawa的博客 2238

深入解析Unity3D中PostLateUpdate突然占用大量时间的原因

在Unity3D的生命周期中,是一个在LateUpdate之后被调用的方法。它通常用于处理一些需要在所有更新(包括物理更新和动画更新)之后进行的逻辑。由于它在游戏循环的后期阶段被调用,因此它通常用于处理与渲染直接相关的任务,如相机调整、UI更新等。突然占用大量时间的问题可能由多种原因引起,包括复杂的逻辑处理、频繁的资源加载、不恰当的渲染优化、插件或第三方库的副作用以及内存泄漏或垃圾回收等。为了解决这个问题,开发者需要仔细审查和优化中的代码,同时利用Unity提供的工具来监控和分析游戏性能。

Yori_22的博客 979

Unity调用外接摄像头的实例代码(C#)

using UnityEngine; using System.Collections; using System.Threading; public class BtnControl : MonoBehaviour { public WebCamTexture webTex; public string deviceName; void Start() { } void

Cocoa_China的专栏 1万+

Unreal5 从入门到精通之 如何实现 离线语音识别

本文介绍了如何使用离线语音识别插件 Offline Speech Recognition 进行本地语音转文本处理。该插件支持离线运行,用户可以通过下载并运行语言服务器(如 Vosk)来处理语音数据。文章详细说明了如何设置语言服务器、下载语言模型,并在虚幻引擎(UE)项目中配置麦克风和语音捕获。此外,还介绍了如何将 SoundWave 作为输入,以及如何通过节点将语音数据发送到语言服务器进行处理。最后,提供了相关文档链接以供进一步参考。

TxNet 903

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

上一篇: goto語句
下一篇: 使用.NET实现视频播放
msikruby
博客等级 码龄19年 11粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值