Capture Webcam Dengan Visual Basic 2008

Webcam

Ilustrasi: Webcam


Webcam merupakan salah satu device yang saat ini hadir pada laptop kita. Mungkin suatu waktu terbesit pada fikiran kita, bagaimana cara menampilkan dan menyimpan gambar sendiri?
Dengan menggunakan bantuan Visual Basic 2008, Anda dapat membuat sendiri sebuah program dasar untuk menampilkan dan menyimpan gambar menggunakan webcam, baik yang sudah menyatu dengan laptop maupun dengan webcam tambahan.
Pada dasarnya sama, antara memakai webcam yang sudah ada pada laptop dengan webcam tambahan, sehingga tidak ada kesulitan untuk menggunakan salah satunya.
Untuk menampilkan device yang terdeteksi pada laptop dapat menggunakan code berikut:
[sourcecode]
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
[/sourcecode]
Untuk menampilkan gambar webcam:
[sourcecode]
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
[/sourcecode]
Untuk menutup gambar webcam:
[sourcecode]
Private Sub ClosePreviewWindow()

‘ Disconnect from device

SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

‘ close window

DestroyWindow(hHwnd)
End Sub
[/sourcecode]
Untuk menyimpan gambar:
[sourcecode]
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
[/sourcecode]
Hasil tampilan
Capture1
Demikian, semoga bermanfaat.
Download sourcecode.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *