Const WM_CAP As Integer = &H400
Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP + 10 'ติดต่อ webcam(โดยหาจาก driver)
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP + 11 'Disconect webcam อันนี้สำคัญทีเดียว
Const WM_CAP_EDIT_COPY As Long = WM_CAP + 30 'Copy ภาพจาก frame buffer นำมาเก็บในรูปของ clipboard เอาไว้ใช้สำหรับ save ภาพนิ่ง(jpg,bmp..)
Const WM_CAP_SET_PREVIEW As Long = WM_CAP + 50 'นำภาพจาก hardware มาเก็บที่ system memory แล้วนำมาแสดงบน window ผ่าน GDI function
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP + 52 'Set frame
Const WM_CAP_SET_SCALE As Long = WM_CAP + 53 'ให้ able(True) หรือ disable scale(False) ภาพ
Const WS_CHILD As Long = &H40000000 'Window style จ้า
Const WS_VISIBLE As Long = &H10000000 'Window style จ้า
Const SWP_NOMOVE As Long = &H2
Const SWP_NOSIZE As Integer = 1
Const SWP_NOZORDER As Integer = &H4
Const HWND_BOTTOM As Integer = 1
Dim iDevice As Long ' Device ID ที่รับได้
Dim hHwnd As Long ' Handle to preview window
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hndw As Long) As Boolean
Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Integer, ByVal hWndParent As Long, ByVal nID As Long) As Long
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Long, ByVal lpszName As String, ByVal cbName As Long, ByVal lpszVer As String, ByVal cbVer As Long) As Boolean
Private Sub Form_Load()
LoadDeviceList 'ก่อนอื่นต้องหาว่ามีกล้อง(webcam)ติดตั้งอยู่หรือไม่
If lstDevices.ListCount > 0 Then
lstDevices.Selected(0) = True 'ถ้ามีก็ OKey เตรียมตัวคลิก start ได้เรยยย มัน enable แล้ว
Else
cmdStart.Enabled = False 'ถ้าไม่มีก็จบ(ห่)
lstDevices.AddItem ("No Device Available")
End If
cmdStop.Enabled = False
cmdSave.Enabled = False
End Sub
Private Sub LoadDeviceList() 'ส่วนนี้ใช้หาว่ามีการติดตั้ง webcam อยู่หรือไม่
Dim strName As String
Dim strVer As String
Dim iReturn As Boolean
Dim x As Long
x = 0
strName = Space(100)
strVer = Space(100)
Do
iReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100) ' โหลด Driver และ version
If iReturn Then lstDevices.AddItem Trim$(strName) ' โหลดชื่อ Device ที่ detect ได้ลงใน lstDevices
x = x + 1
Loop Until iReturn = False
End Sub
Private Sub cmdStart_Click()
iDevice = lstDevices.ListIndex 'โหลด Device(Driver)จาก list.....
OpenPreviewWindow 'ไปดูการทำงานที่โปรแกรมย่อย OpenPreviewWindow
End Sub
Private Sub OpenPreviewWindow() 'เมื่อทุกอย่างพร้อมแล้วก็ลุยเรยยยยย
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.hwnd, 0) 'ให้รับภาพผ่านทาง picture box
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then 'ติดต่อ webcam
SendMessage hHwnd, WM_CAP_SET_SCALE, True, 0 'ตั้งค่า scale ให้เป็น False เพราะขนาดภาพจริงจะถูก fix ไว้ ถ้าเป็น True ขนาดภาพจะขยายเท่ากับ window preview
SendMessage hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0 'ตั้งค่า preview rate
SendMessage hHwnd, WM_CAP_SET_PREVIEW, True, 0 'ให้เริ่มรับภาพ(True) จาก webcam
cmdSave.Enabled = True
cmdStop.Enabled = True
cmdStart.Enabled = False
Else
DestroyWindow hHwnd 'ถ้ามีข้อผิดพลาดก็ให้ออกจาก window(แบบถูกที่ถูกทาง)
cmdSave.Enabled = False
End If
End Sub
Private Sub ClosePreviewWindow()
SendMessage hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0 'ยกเลิกการติดต่อโดยใช้ message WM_CAP_DRIVER_DISCONNECT ถ้าไม่ออกวิธีนี้ error แน่นอน ลองดูสิ
DestroyWindow hHwnd 'ออกจาก window
End Sub
Private Sub cmdStop_Click()
ClosePreviewWindow
cmdStop.Enabled = False
cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub
Public Class Form1
Const WM_CAP As Integer = &H400
Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP + 10 'ติดต่อ webcam(โดยหาจาก driver)
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP + 11 'Disconect webcam อันนี้สำคัญทีเดียว
Const WM_CAP_EDIT_COPY As Long = WM_CAP + 30 'Copy ภาพจาก frame buffer นำมาเก็บในรูปของ clipboard เอาไว้ใช้สำหรับ save ภาพนิ่ง(jpg,bmp..)
Const WM_CAP_SET_PREVIEW As Long = WM_CAP + 50 'นำภาพจาก hardware มาเก็บที่ system memory แล้วนำมาแสดงบน window ผ่าน GDI function
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP + 52 'Set frame
Const WM_CAP_SET_SCALE As Long = WM_CAP + 53 'ให้ able(True) หรือ disable scale(False) ภาพ
Const WS_CHILD As Long = &H40000000 'Window style จ้า
Const WS_VISIBLE As Long = &H10000000 'Window style จ้า
Const SWP_NOMOVE As Long = &H2S
Const SWP_NOSIZE As Integer = 1
Const SWP_NOZORDER As Integer = &H4S
Const HWND_BOTTOM As Integer = 1
Dim iDevice As Long ' Device ID ที่รับได้
Dim hHwnd As Long ' Handle to preview window
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
''Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hndw As IntPtr) As Boolean
Private 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 Integer, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Integer, 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() 'ก่อนอื่นต้องหาว่ามีกล้อง(webcam)ติดตั้งอยู่หรือไม่
If lstDevices.Items.Count > 0 Then
lstDevices.SelectedItem = True 'ถ้ามีก็ OKey เตรียมตัวคลิก start ได้เรยยย มัน enable แล้ว
Else
cmdStart.Enabled = False 'ถ้าไม่มีก็จบ(ห่)
lstDevices.Items.Add("No Device Available")
End If
cmdStop.Enabled = False
'cmdSave.Enabled = False
End Sub
Private Sub LoadDeviceList() 'ส่วนนี้ใช้หาว่ามีการติดตั้ง webcam อยู่หรือไม่
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim iReturn As Boolean
Dim x As Long = 0
Do
iReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100) ' โหลด Driver และ version
If iReturn Then lstDevices.Items.Add(strName.Trim) ' โหลดชื่อ Device ที่ detect ได้ลงใน lstDevices
x = x + 1
Loop Until iReturn = False
End Sub
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
iDevice = lstDevices.SelectedIndex 'โหลด Device(Driver)จาก list.....
OpenPreviewWindow() 'ไปดูการทำงานที่โปรแกรมย่อย OpenPreviewWindow
End Sub
Private Sub OpenPreviewWindow() 'เมื่อทุกอย่างพร้อมแล้วก็ลุยเรยยยยย
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0) 'ให้รับภาพผ่านทาง picture box
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then 'ติดต่อ webcam
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0) 'ตั้งค่า scale ให้เป็น False เพราะขนาดภาพจริงจะถูก fix ไว้ ถ้าเป็น True ขนาดภาพจะขยายเท่ากับ window preview
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0) 'ตั้งค่า preview rate
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0) 'ให้เริ่มรับภาพ(True) จาก webcam
'cmdSave.Enabled = True
cmdStop.Enabled = True
cmdStart.Enabled = False
Else
DestroyWindow(hHwnd) 'ถ้ามีข้อผิดพลาดก็ให้ออกจาก window(แบบถูกที่ถูกทาง)
'cmdSave.Enabled = False
End If
End Sub
Private Sub ClosePreviewWindow()
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0) 'ยกเลิกการติดต่อโดยใช้ message WM_CAP_DRIVER_DISCONNECT ถ้าไม่ออกวิธีนี้ error แน่นอน ลองดูสิ
DestroyWindow(hHwnd) 'ออกจาก window
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
ClosePreviewWindow()
cmdStop.Enabled = False
'cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub
End Class
Public Class Form1
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
Dim hHwnd As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
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 IntPtr) 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 LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
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
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
cmdSave.Enabled = True
cmdStop.Enabled = True
cmdStart.Enabled = False
Else
DestroyWindow(hHwnd)
cmdSave.Enabled = False
End If
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim data As IDataObject
Dim bmap As Image
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
ClosePreviewWindow()
cmdSave.Enabled = False
cmdStop.Enabled = False
cmdStart.Enabled = True
If sfdImage.ShowDialog = DialogResult.OK Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
End If
End If
End Sub
Private Sub ClosePreviewWindow()
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
DestroyWindow(hHwnd)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDeviceList()
End Sub
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
OpenPreviewWindow()
cmdStart.Enabled = False
cmdStop.Enabled = True
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
ClosePreviewWindow()
cmdStart.Enabled = True
cmdStop.Enabled = False
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click, cmdStop.Click
End Sub
End Class