Search This Blog

Loading...

Saturday 16 July 2011

Webcam Integration Using VB.Net

This post will help to you to integrate webcam in your application.
To Integrate Webcam in your project, plz. follow the following steps :
1. Create a new class file in Vb.Net
2. Name it as BGWebCamControl
3. Copy/ Paste the following code to your new created class


Imports System.Windows.Forms

Imports System.Drawing

Public Class WebCam

Inherits System.Windows.Forms.Form

'Create constant using attend in function of DLL file.

Dim img As imagecrop.MyImage

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

Friend savefilepath As String

Dim sfdimage As New OpenFileDialog

Friend WithEvents lstdevices As New ListBox

Dim iDevice As Integer = 0 ' Normal device ID

Dim hHwnd As Integer ' Handle value to preview window

Friend WithEvents btnsave As New Button

Friend WithEvents formname As New System.Windows.Forms.Form

Friend WithEvents btnstart As New Button

Friend WithEvents btnstop As New Button

Friend WithEvents piccapture As New PictureBox

' Declare function from AVI capture DLL.

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _

(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _

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



' To display the output from a video capture device, you need to create a capture window.



' Use SendMessage to copy the data to the clipboard Then transfer the image to the picture box.



Public Sub SaveButtonClickEvent(ByVal picbox As PictureBox, ByVal FileSavePath As String, ByVal savebtn As Button, ByVal stopbtn As Button, ByVal startbtn As Button)

Me.piccapture = picbox

savefilepath = FileSavePath

Me.btnsave = savebtn

Me.btnstop = stopbtn

Me.btnstart = startbtn

Me.saving_routine()

End Sub

Public Sub freezeButtonClickEvent(ByVal picbox As PictureBox, ByVal FileSavePath As String, ByVal stopbtn As Button, ByVal startbtn As Button)

Me.piccapture = picbox

savefilepath = FileSavePath

'Me.btnsave = savebtn

Me.btnstop = stopbtn

Me.btnstart = startbtn

Me.saving_routine()

End Sub

Public Sub freezeButtonClickEvent(ByVal picbox As PictureBox, ByVal FileSavePath As String)

Me.piccapture = picbox

savefilepath = FileSavePath

Me.saving_routine()

End Sub

' Finally, to close the preview window, disconnect from the device and destroy the preview window.

Public Sub StartButtonClickEvent(ByVal picbox As PictureBox, ByVal stopbtn As Button, ByVal startbtn As Button)

Me.btnstop = stopbtn

Me.btnstart = startbtn

Me.piccapture = picbox

Me.btnstop.Enabled = True

Me.btnstart.Enabled = False

picbox.SizeMode = PictureBoxSizeMode.StretchImage

Me.OpenPreviewWindow()

End Sub

Public Sub StartButtonClickEvent(ByVal picbox As PictureBox)

Me.piccapture = picbox

picbox.SizeMode = PictureBoxSizeMode.StretchImage

Me.OpenPreviewWindow()

End Sub

Public Sub StopButtonClickEvent(ByVal stopbtn As Button, ByVal startbtn As Button)

Me.ClosePreviewWindow()

Me.btnstart = startbtn

Me.btnstop = stopbtn

Me.btnstop.Enabled = False

Me.btnstart.Enabled = True

Me.btnsave.Enabled = False

End Sub

Public Sub StopButtonClickEvent()

Me.ClosePreviewWindow()

End Sub

Public Sub BtnLoadDeviceClickEvent(ByVal DeviceList As ListBox)

Me.lstdevices = DeviceList

Me.LoadDeviceList()

End Sub

Public Function GetPictureDIGICAM(ByVal picbox As Windows.Forms.PictureBox) As Image

Dim opendialogue As New Windows.Forms.OpenFileDialog

'Me.btnstop.PerformClick()

If opendialogue.ShowDialog = DialogResult.OK Then

picbox.SizeMode = Windows.Forms.PictureBoxSizeMode.StretchImage

picbox.Image = picbox.Image.FromFile(opendialogue.FileName)

End If

opendialogue.RestoreDirectory = False

Return picbox.Image

End Function

Public Function GetDIGIPicture(ByVal picbox As PictureBox)

Dim opendialogue As New Windows.Forms.OpenFileDialog

opendialogue.RestoreDirectory = True

If opendialogue.ShowDialog = DialogResult.OK Then

Me.img = New imagecrop.MyImage(opendialogue.FileName)

picbox.Image = img._inputImage.FromFile(Me.img._pathOutputImage)

picbox.SizeMode = Windows.Forms.PictureBoxSizeMode.StretchImage

End If

End Function

Private Sub ClosePreviewWindow()

' Disconnect from device

SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

' close window

DestroyWindow(hHwnd)

End Sub

Private Sub saving_routine()

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

bmap.Save(savefilepath, Imaging.ImageFormat.Bmp)

End If

End Sub

Private Sub OpenPreviewWindow()

Dim iHeight As Integer = piccapture.Height

Dim iWidth As Integer = piccapture.Width

' Open Preview window in picturebox .

' Create a child window with capCreateCaptureWindowA so you can display it in a 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)



Else

' Error connecting to device close window

DestroyWindow(hHwnd)

btnsave.Enabled =
False

End If

End Sub

' Connect to the device.

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 InitializeComponent()

'

'WebCam

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Name = "WebCam"

End Sub

End Class




4. Save this file
5. Build this file, you will get BGWebCamControl.dll in your output directory normaly in bin folder
plz. make sure that you have connected a webcam with appropriate drivers installed before making Windows Form Application for Webcam Integration

6. Create a New Windows Form Application with following controls with names as mentioned (because code will work only if control names are named as mentioned)

Plz. look at the following picture for control namings



7. Add reference of BGWebCamControl.dll to your newly created Windows Form

8. On double-click the form you will see the code window. Plz. copy paste the following code :

Public Class ImageCapturing

Dim webcam As New BGWebCamControl.WebCam

Dim defaultimagepath As String = "c:\web.jpg"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.btnstopweb.Enabled = True

Me.btnstopweb.PerformClick()

Me.Close()

End Sub

Private Sub ImageCapturing_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.RadioWeb.Checked = True

Me.webcam.BtnLoadDeviceClickEvent(Me.ListBox1)

End Sub

Private Sub camperaoption()

Try

If Me.RadioWeb.Checked = True Then

Me.btnstopweb.Enabled = False

Me.btnfreeze.Enabled = False

Me.panelwebcontrols.Visible = True

Me.paneldigicam.Visible = False

Me.panelwebcontrols.Location = New Point(Me.PictureBOX.Location.X, Me.PictureBOX.Location.Y + Me.PictureBOX.Height)

Me.panelwebcontrols.Width = Me.PictureBOX.Width

Else

Me.btnstopweb.Enabled = True

Me.btnstopweb.PerformClick()

Me.PictureBOX.BackColor = Color.Black

Me.panelwebcontrols.Visible = False

Me.paneldigicam.Visible = True

Me.paneldigicam.Location = New Point(Me.PictureBOX.Location.X, Me.PictureBOX.Location.Y + Me.PictureBOX.Height)

Me.paneldigicam.Width = Me.PictureBOX.Width

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub RadioWeb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioWeb.CheckedChanged

Me.camperaoption()

End Sub

Private Sub RadioDigiCam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioDigiCam.CheckedChanged

Me.camperaoption()

End Sub

Private Sub btnstartwebcam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstartwebcam.Click

Try

Me.btnstartwebcam.Enabled = False

Me.btnstopweb.Enabled = True

Me.btnfreeze.Enabled = True

Me.webcam.StartButtonClickEvent(Me.PictureBOX)

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub btnfreeze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfreeze.Click

Try

Me.btnstartwebcam.Enabled = True

Me.btnstopweb.Enabled = False

Me.btnfreeze.Enabled = False

Me.webcam.freezeButtonClickEvent(Me.PictureBOX, defaultimagepath)

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub btnstopweb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstopweb.Click

Try

Me.btnstartwebcam.Enabled = True

Me.btnstopweb.Enabled = False

Me.btnfreeze.Enabled = False

Me.webcam.StopButtonClickEvent()

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub gtngetpicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gtngetpicture.Click

Try

Me.webcam.GetDIGIPicture(Me.PictureBOX)

Me.PictureBOX.Image.Save(defaultimagepath)

Catch ex As Exception

Me.PictureBOX.Text = "File is not in correct format"

End Try

End Sub

End Class



9. Plz. clear all the bugs (may be due to wrong naming of controls)
10.Run Your Project

11. On Successfull running, you will find saved file captured from webcam in root of c: drive with name "web.jpg"

3 comments:

  1. That's fine, little naming modifications required, rst is fine

    ReplyDelete
    Replies
    1. sorry to thanks after 2 years, I was away from VB in between

      Delete
  2. sorry to thanks after 2 years, I was away from VB in between

    ReplyDelete