一个驱动webcam的类,利用通用的摄像头驱动程序avicap32.dll [vb.net]

web|程序

 
'-------------------------------------------------

'--------Code By Ken Tucker-------------

'-------------------------------------------------

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents picCapture As System.Windows.Forms.PictureBox
    Friend WithEvents lstDevices As System.Windows.Forms.ListBox
    Friend WithEvents lblDevice As System.Windows.Forms.Label
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnSave As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.picCapture = New System.Windows.Forms.PictureBox()
        Me.lstDevices = New System.Windows.Forms.ListBox()
        Me.lblDevice = New System.Windows.Forms.Label()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.btnSave = New System.Windows.Forms.Button()
        Me.btnStop = New System.Windows.Forms.Button()
        Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
        Me.SuspendLayout()
        '
        'picCapture
        '
        Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.picCapture.Location = New System.Drawing.Point(208, 24)
        Me.picCapture.Name = "picCapture"
        Me.picCapture.Size = New System.Drawing.Size(256, 272)
        Me.picCapture.TabIndex = 0
        Me.picCapture.TabStop = False
        '
        'lstDevices
        '
        Me.lstDevices.Location = New System.Drawing.Point(8, 55)
        Me.lstDevices.Name = "lstDevices"
        Me.lstDevices.Size = New System.Drawing.Size(184, 238)
        Me.lstDevices.TabIndex = 1
        '
        'lblDevice
        '
        Me.lblDevice.Location = New System.Drawing.Point(8, 32)
        Me.lblDevice.Name = "lblDevice"
        Me.lblDevice.Size = New System.Drawing.Size(184, 16)
        Me.lblDevice.TabIndex = 2
        Me.lblDevice.Text = "Available Devices"
        Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(20, 320)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(112, 32)
        Me.btnStart.TabIndex = 3
        Me.btnStart.Text = "Start Preview"
        '
        'btnSave
        '
        Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnSave.Location = New System.Drawing.Point(348, 320)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(112, 32)
        Me.btnSave.TabIndex = 4
        Me.btnSave.Text = "Save Image"
        '
        'btnStop
        '
        Me.btnStop.Location = New System.Drawing.Point(184, 320)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(112, 32)
        Me.btnStop.TabIndex = 5
        Me.btnStop.Text = "Stop Preview"
        '
        'sfdImage
        '
        Me.sfdImage.FileName = "Webcam1"
        Me.sfdImage.Filter = "Bitmap|*.bmp"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 382)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnSave, Me.btnStart, Me.lblDevice, Me.lstDevices, Me.picCapture})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Video Capture"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Const WM_CAP As Short = &H400S

    Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30

    Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
    Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    Const WS_CHILD As Integer = &H40000000
    Const WS_VISIBLE As Integer = &H10000000
    Const SWP_NOMOVE As Short = &H2S
    Const SWP_NOSIZE As Short = 1
    Const SWP_NOZORDER As Short = &H4S
    Const HWND_BOTTOM As Short = 1

    Dim iDevice As Integer = 0 ' Current device ID
    Dim hHwnd As Integer ' Handle to preview window

    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
        <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

    Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
        ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
        ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

    Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean

    Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
        (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
        ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
        ByVal nHeight As Short, ByVal hWndParent As Integer, _
        ByVal nID As Integer) As Integer

    Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
        ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
        ByVal cbVer As Integer) As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDeviceList()
        If lstDevices.Items.Count > 0 Then
            btnStart.Enabled = True
            lstDevices.SelectedIndex = 0
            btnStart.Enabled = True
        Else
            lstDevices.Items.Add("No Capture Device")
            btnStart.Enabled = False
        End If

        btnStop.Enabled = False
        btnSave.Enabled = False
        picCapture.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub LoadDeviceList()
        Dim strName As String = Space(100)
        Dim strVer As String = Space(100)
        Dim bReturn As Boolean
        Dim x As Integer = 0

        '
        ' Load name of all avialable devices into the lstDevices
        '

        Do
            '
            '   Get Driver name and version
            '
            bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)

            '
            ' If there was a device add device name to the list
            '
            If bReturn Then lstDevices.Items.Add(strName.Trim)
            x += 1
        Loop Until bReturn = False
    End Sub

    Private Sub OpenPreviewWindow()
        Dim iHeight As Integer = picCapture.Height
        Dim iWidth As Integer = picCapture.Width

        '
        ' Open Preview window in picturebox
        '
        hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
            480, picCapture.Handle.ToInt32, 0)

        '
        ' Connect to device
        '
        If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
            '
            'Set the preview scale
            '
            SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)

            '
            'Set the preview rate in milliseconds
            '
            SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)

            '
            'Start previewing the image from the camera
            '
            SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)

            '
            ' Resize window to fit in picturebox
            '
            SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
                    SWP_NOMOVE Or SWP_NOZORDER)

            btnSave.Enabled = True
            btnStop.Enabled = True
            btnStart.Enabled = False
        Else
            '
            ' Error connecting to device close window
            '
            DestroyWindow(hHwnd)

            btnSave.Enabled = False
        End If
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        iDevice = lstDevices.SelectedIndex
        OpenPreviewWindow()
    End Sub

    Private Sub ClosePreviewWindow()
        '
        ' Disconnect from device
        '
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

        '
        ' close window
        '

        DestroyWindow(hHwnd)
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        ClosePreviewWindow()
        btnSave.Enabled = False
        btnStart.Enabled = True
        btnStop.Enabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As IDataObject
        Dim bmap As Image

        '
        ' Copy image to clipboard
        '
        SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)

        '
        ' Get image from clipboard and convert it to a bitmap
        '
        data = Clipboard.GetDataObject()
        If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
            bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
            picCapture.Image = bmap
            ClosePreviewWindow()
            btnSave.Enabled = False
            btnStop.Enabled = False
            btnStart.Enabled = True

            If sfdImage.ShowDialog = DialogResult.OK Then
                bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
            End If

        End If
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If btnStop.Enabled Then
            ClosePreviewWindow()
        End If
    End Sub
End Class

时间: 2025-01-20 19:23:55

一个驱动webcam的类,利用通用的摄像头驱动程序avicap32.dll [vb.net]的相关文章

【12】设计一个不能被继承的类

题目:用C++实现一个不能被继承的类 思路:1. 在C++中如果一个类能够被继承,那么子类的对象在构造的时候会先调用父类的构造函数,再执行自己的构造函数.子类对象在销毁的时候会先调用本身析构函数再调用父类析构函数            2. 如果我们把一个类的构造函数和一个类的析构函数权限设置为private,那么当一个类试图去继承的时候会报错,可以达到不能被继承的效果            3. 怎么得到类的实例呢?和单例模式类似,我们可以采用静态成员函数来得到类的实例,销毁和时候也直接利用类

Django 基于类的通用视图的详解

在早期,我们认识到在视图开发过程中有共同的用法和模式.这时我们引入基于函数的通用视图来抽象这些模式以简化常见情形的视图开发. 基于函数视图的用法有以下三种:       def index(request):         return HttpResponse('hello world!')       def index(request):         result = {'demo':'demo'}         return render(request, 'blog/about

java-为什么不可能创建一个继承OnTouchListener的类?

问题描述 为什么不可能创建一个继承OnTouchListener的类? Android SDK在TouchEvent有一个接口(android.view.View.OnTouchListener).我不知道为什么下面这一行是错误的.Eclipse在OnTouchListener报错. public class TouchHandler extends OnTouchListener{ 如果我改为public interface TouchHandler extends OnTouchListen

封装的一个asp.net验证码类

第一个反映是验证码的这些属性是否可以设置(也就是说是封装成一个类,然后以暴露公有属性和方法的方式来使用的,还是在直接在一般处理程序中实现了验证码的生成到输出),结果比较可惜,是后者...... 里面的算法感觉挺不错,至少对于我这种对算法几乎是不懂的人来说是这样,于是想到去封装一下她然后无耻地纳入自己的类库中去^^ old原文件如下:点击下载 首先分析一下这份代码中的不足(自己觉得的): 1.面向过程式的编程,如果要修改验证码的属性,例如修改字体的大小.背景颜色等细节的东西,需要去一般处理程序中找

一个PHP MySQL操作类...

<?php /** * MySQL 数据库操作工具类, 方便数据库操作. * 示例见底部注释. * @author: http://www.ideawu.net/blog/ */ class Mysql{ var $conn; var $query_list = array(); public $query_count = 0; public function __construct($c){ if(!isset($c['port'])){ $c['port'] = '3306'; } $ser

给网站添加一个注册表 之 “类的应用”

注册表 下面,我们用这个"注册表"类来写一个简单的"计数器"应用,代码如下: pagecount.asp: <!-- #Include File="registry_class_inc.asp" --><%Dim RegSet Reg = New RegistryObjectReg.Path = Server.MapPath("pagecount.xml") Const BaseCount = 8000 '计

ASP编写完整的一个IP所在地搜索类

ASP编写完整的一个IP所在地搜索类的修正文稿 修正了查询方法,查询的方法和追捕的一致:只是追捕会自动更正IP. 还有个函数的书写错误,也已经修正: 包括增加了一个IP地址正确性的验证函数.(只是从格式上判断) <% Server.ScriptTimeout = &HE10 '&H3C Response.Buffer = ("S.F." = "S.F.") Dim IpSearch '建立对象 Set IpSearch = New clsIpS

一个CFileFind的扩展类CFileFindExt

一.类CfileFindExt功能说明 大家经常需要对指定目录下的所有文件进行搜索或其它相同操作.若文件数很多,逐一对每个文件手工操作,很费时.本文提供一个搜索或处理指定目录下的所有文件的C++类.作者用基于层次遍历的非递归算法(以减少运行时的时间和空间开销)实现了一个 CFileFind 的扩展类 CFileFindExt,该类可以对指定目录下的所有文件进行搜索或相同的处理操作. 二.类 CFileFindExt 的接口说明及用法示例: 1.头文件.处理函数.以及类的声明: #include

介绍一个增强的数据库类CDataSet

因为MFC完全支持数据库应用程序的开发,所以大多数数据库应用都使用CDatabase和CRecordset类,并且类向导(Class Wizard)提供了快速简易的方式来使用这两个类.有一点不足的就是当应用程序涉及到多表数据库时,类向导将产生大量的关于记录集的源码文件使得工程给人的感觉很臃肿混乱. 本文介绍如何使用一个模板记录集类来降低类向导所产生的记录集文件的数量,同时增强记录集类(CRecordset)的功能.这个模板记录集类叫做:CDataSet.它的主要目的是降低代码量,为数据对象数组提