CAPTURA WEBCAM MINIMIZADA VB.NET
Mais uma vez quebrando a cabeça rsrs;
Bom fiz um app para capturar imagem da webcam funcionando certinho porem se eu minimizar ela não captura mais!
Alias ela captura sim porem fica repetindo a mesma foto de quando o app não tava minimizado ou seja depois que menimizar fica repetindo a mesma foto sempre....
Bom fiz um app para capturar imagem da webcam funcionando certinho porem se eu minimizar ela não captura mais!
Alias ela captura sim porem fica repetindo a mesma foto de quando o app não tava minimizado ou seja depois que menimizar fica repetindo a mesma foto sempre....
RODRIGO29,
Precisamos ver o código, senão fica difÃcil...
[][ô]s,
Tunusat.
Precisamos ver o código, senão fica difÃcil...
[][ô]s,
Tunusat.
Pesquisei e estou usando esse código que achei na net, queria fazer uma webcam de segurança:
Imports System.Runtime.InteropServices
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
Public Const WM_CAP_GET_STATUS As Integer = WM_CAP + 54
Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP + 41
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
Private DeviceID As Integer = 0 [ô] Current device ID
Private 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, _
ByRef lParam As CAPSTATUS) As Boolean
Declare Function SendMessage Lib [Ô]user32[Ô] Alias [Ô]SendMessageA[Ô] _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Boolean, _
ByRef lParam As Integer) As Boolean
Declare Function SendMessage Lib [Ô]user32[Ô] Alias [Ô]SendMessageA[Ô] _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByRef lParam As Integer) As Boolean
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
Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Public Structure CAPSTATUS
Dim uiImageWidth As Integer [ô]// Width of the image
Dim uiImageHeight As Integer [ô]// Height of the image
Dim fLiveWindow As Integer [ô]// Now Previewing video?
Dim fOverlayWindow As Integer [ô]// Now Overlaying video?
Dim fScale As Integer [ô]// Scale image to client?
Dim ptScroll As POINTAPI [ô]// Scroll position
Dim fUsingDefaultPalette As Integer [ô]// Using default driver palette?
Dim fAudioHardware As Integer [ô]// Audio hardware present?
Dim fCapFileExists As Integer [ô]// Does capture file exist?
Dim dwCurrentVideoFrame As Integer [ô]// # of video frames cap[ô]td
Dim dwCurrentVideoFramesDropped As Integer [ô]// # of video frames dropped
Dim dwCurrentWaveSamples As Integer [ô]// # of wave samples cap[ô]td
Dim dwCurrentTimeElapsedMS As Integer [ô]// Elapsed capture duration
Dim hPalCurrent As Integer [ô]// Current palette in use
Dim fCapturingNow As Integer [ô]// Capture in progress?
Dim dwReturn As Integer [ô]// Error value after any operation
Dim wNumVideoAllocated As Integer [ô]// Actual number of video buffers
Dim wNumAudioAllocated As Integer [ô]// Actual number of audio buffers
End Structure
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
Me.AutoScrollMinSize = New Size(100, 100)
btnStop.Enabled = False
btnSave.Enabled = False
btnInfo.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 Short = 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 += CType(1, Short)
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(DeviceID.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 1280, _
1024, picCapture.Handle.ToInt32, 0)
[ô]
[ô] Connect to device
[ô]
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 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)
btnStop.Enabled = True
btnStart.Enabled = False
Else
[ô]
[ô] Error connecting to device close window
[ô]
DestroyWindow(hHwnd)
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
DeviceID = lstDevices.SelectedIndex
OpenPreviewWindow()
Dim bReturn As Boolean
Dim s As CAPSTATUS
bReturn = SendMessage(hHwnd, WM_CAP_GET_STATUS, Marshal.SizeOf(s), s)
Debug.WriteLine(String.Format([Ô]Video Size {0} x {1}[Ô], s.uiImageWidth, s.uiImageHeight))
End Sub
Private Sub ClosePreviewWindow()
[ô]
[ô] Disconnect from device
[ô]
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 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()
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
Private Sub btnEsconder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEsconder.Click
Me.Hide()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim caminho As String = [Ô]C:\HP\[Ô]
Dim data As IDataObject
Dim bmap As Bitmap
[ô]
[ô] 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)), Bitmap)
picCapture.Image = bmap
btnStop.Enabled = False
btnStart.Enabled = True
btnInfo.Enabled = False
Trace.Assert(Not (bmap Is Nothing))
bmap.Save(caminho & Format(DateTime.Now.Date, [Ô]ddMMyyyy[Ô]) & DateTime.Now.Hour & DateTime.Now.Minute & DateTime.Now.Second & DateTime.Now.Millisecond & [Ô].jpg[Ô], Imaging.ImageFormat.Jpeg)
End If
End Sub
End Class
Imports System.Runtime.InteropServices
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
Public Const WM_CAP_GET_STATUS As Integer = WM_CAP + 54
Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP + 41
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
Private DeviceID As Integer = 0 [ô] Current device ID
Private 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, _
ByRef lParam As CAPSTATUS) As Boolean
Declare Function SendMessage Lib [Ô]user32[Ô] Alias [Ô]SendMessageA[Ô] _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Boolean, _
ByRef lParam As Integer) As Boolean
Declare Function SendMessage Lib [Ô]user32[Ô] Alias [Ô]SendMessageA[Ô] _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByRef lParam As Integer) As Boolean
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
Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Public Structure CAPSTATUS
Dim uiImageWidth As Integer [ô]// Width of the image
Dim uiImageHeight As Integer [ô]// Height of the image
Dim fLiveWindow As Integer [ô]// Now Previewing video?
Dim fOverlayWindow As Integer [ô]// Now Overlaying video?
Dim fScale As Integer [ô]// Scale image to client?
Dim ptScroll As POINTAPI [ô]// Scroll position
Dim fUsingDefaultPalette As Integer [ô]// Using default driver palette?
Dim fAudioHardware As Integer [ô]// Audio hardware present?
Dim fCapFileExists As Integer [ô]// Does capture file exist?
Dim dwCurrentVideoFrame As Integer [ô]// # of video frames cap[ô]td
Dim dwCurrentVideoFramesDropped As Integer [ô]// # of video frames dropped
Dim dwCurrentWaveSamples As Integer [ô]// # of wave samples cap[ô]td
Dim dwCurrentTimeElapsedMS As Integer [ô]// Elapsed capture duration
Dim hPalCurrent As Integer [ô]// Current palette in use
Dim fCapturingNow As Integer [ô]// Capture in progress?
Dim dwReturn As Integer [ô]// Error value after any operation
Dim wNumVideoAllocated As Integer [ô]// Actual number of video buffers
Dim wNumAudioAllocated As Integer [ô]// Actual number of audio buffers
End Structure
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
Me.AutoScrollMinSize = New Size(100, 100)
btnStop.Enabled = False
btnSave.Enabled = False
btnInfo.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 Short = 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 += CType(1, Short)
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(DeviceID.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 1280, _
1024, picCapture.Handle.ToInt32, 0)
[ô]
[ô] Connect to device
[ô]
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 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)
btnStop.Enabled = True
btnStart.Enabled = False
Else
[ô]
[ô] Error connecting to device close window
[ô]
DestroyWindow(hHwnd)
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
DeviceID = lstDevices.SelectedIndex
OpenPreviewWindow()
Dim bReturn As Boolean
Dim s As CAPSTATUS
bReturn = SendMessage(hHwnd, WM_CAP_GET_STATUS, Marshal.SizeOf(s), s)
Debug.WriteLine(String.Format([Ô]Video Size {0} x {1}[Ô], s.uiImageWidth, s.uiImageHeight))
End Sub
Private Sub ClosePreviewWindow()
[ô]
[ô] Disconnect from device
[ô]
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 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()
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
Private Sub btnEsconder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEsconder.Click
Me.Hide()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim caminho As String = [Ô]C:\HP\[Ô]
Dim data As IDataObject
Dim bmap As Bitmap
[ô]
[ô] 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)), Bitmap)
picCapture.Image = bmap
btnStop.Enabled = False
btnStart.Enabled = True
btnInfo.Enabled = False
Trace.Assert(Not (bmap Is Nothing))
bmap.Save(caminho & Format(DateTime.Now.Date, [Ô]ddMMyyyy[Ô]) & DateTime.Now.Hour & DateTime.Now.Minute & DateTime.Now.Second & DateTime.Now.Millisecond & [Ô].jpg[Ô], Imaging.ImageFormat.Jpeg)
End If
End Sub
End Class
Rodrigo,
Montei um exemplo em Visual Studio 2010 baseado no código acima. Coloquei no [Ô]Form2[Ô].
O código exemplo do Macoratti está no [Ô]Form1[Ô] (http://www.macoratti.net/07/08/vbn_wbc.htm)
Mas não consegui simular o que você relatou como erro... talvez esteja faltando algo. Complete o código e depois me mande de volta, okay?
[][ô]s,
Tunusat.
Montei um exemplo em Visual Studio 2010 baseado no código acima. Coloquei no [Ô]Form2[Ô].
O código exemplo do Macoratti está no [Ô]Form1[Ô] (http://www.macoratti.net/07/08/vbn_wbc.htm)
Mas não consegui simular o que você relatou como erro... talvez esteja faltando algo. Complete o código e depois me mande de volta, okay?
[][ô]s,
Tunusat.
TUNUSAT,
Obrigado amigo pela ajuda só pudi responder hoje fiz certinho como me mandou no vb 2010 e executei o projeto que me mandou mas a mesma coisa se minimizar ou esconder o app ele fica repetindo a mesma captura da webcam de quando o form estava visivel, quando tu testou ai ele minimizado ou em hide ele funcionou corretante tirando novas capturas ou ficou repetindo?
Obrigado amigo pela ajuda só pudi responder hoje fiz certinho como me mandou no vb 2010 e executei o projeto que me mandou mas a mesma coisa se minimizar ou esconder o app ele fica repetindo a mesma captura da webcam de quando o form estava visivel, quando tu testou ai ele minimizado ou em hide ele funcionou corretante tirando novas capturas ou ficou repetindo?
RODRIGO29,
Então. Aqui eu minimizei o form e abri de novo e ele voltou normalmente continuando a mostrar o que a câmera vê.
Só deu um problema que quando paro a câmera à s vezes parece que se perde e não consegue reiniciá-la.
Os dois forms estão com este problema? Será que se você testar em outra máquina com outra câmera não funcionaria?
[][ô]s,
Tunusat.
Então. Aqui eu minimizei o form e abri de novo e ele voltou normalmente continuando a mostrar o que a câmera vê.
Só deu um problema que quando paro a câmera à s vezes parece que se perde e não consegue reiniciá-la.
Os dois forms estão com este problema? Será que se você testar em outra máquina com outra câmera não funcionaria?
[][ô]s,
Tunusat.
Boa noite, até que um dia consegui recuperar minha senha,
Alguém pode me ajudar ainda estou com o mesmo problema.
coloco o código de capturar imagem da webcam em um timer e funciona perfeitamente porem se o form é minimizado ou ocultado ele fica repetindo as capturas da webcam de quando o form ainda estava visÃvel.
E se deixo o form visÃvel ele captura normalmente a webcam sem repetir as fotos...
Obrigado TUNUSAT, mas testei aqui e não deu o que me enviou acho que pode ter esquecido de ligar o timer. pois não salvas as capturas.
Alguém pode me ajudar ainda estou com o mesmo problema.
coloco o código de capturar imagem da webcam em um timer e funciona perfeitamente porem se o form é minimizado ou ocultado ele fica repetindo as capturas da webcam de quando o form ainda estava visÃvel.
E se deixo o form visÃvel ele captura normalmente a webcam sem repetir as fotos...
Obrigado TUNUSAT, mas testei aqui e não deu o que me enviou acho que pode ter esquecido de ligar o timer. pois não salvas as capturas.
Tópico encerrado , respostas não são mais permitidas