首页 > 编程 > .NET > 正文

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

2024-07-10 13:07:38
字体:
来源:转载
供稿:网友
 

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

'--------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


最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表