CAPTURAR WEBCAM

VBSTUDENT 23/05/2013 13:03:42
#423939
Olá a todos,

estou criando uma aplicação na qual preciso capturar a webcam e me deparei com este código http://www.vbforums.com/showthread.php?511756-Recording-avi-in-VB-NET postado pelo autor deste tópico e quando fui testar, chamei em um botão a Sub de nome Record no meu Form1.vb claro que troquei o caminho a salvar o arquivo senão nunca iria achar o aqrui em meu pc rsrs. Entretanto o code que esse cara postou não faz nada. Já Coloquei as funções de driver connect e preview no evento FormLoad em meus testes, e mesmo assim não foi.

Por gentileza, alguém poderia me ajudar a funcionar esse código, pois achei bem completo para a minha finalidade e não me interessaria por outra sugestão.

Aguardo retorno. Toda ajuda será muito bem vinda!
MFLAVIO 23/05/2013 13:39:14
#423946
eu uso um form onde eu ligo a WewCam e depois fotografo
[c] Imports System.Runtime.InteropServices
Imports System.IO
Imports FirebirdSql.Data.FirebirdClient
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms


Public Class WebCam
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 picCaptura As System.Windows.Forms.PictureBox
Friend WithEvents lstDispositivos As System.Windows.Forms.ListBox
Friend WithEvents lblDevice As System.Windows.Forms.Label
Friend WithEvents btnIniciar As System.Windows.Forms.Button
Friend WithEvents btnSalvar As System.Windows.Forms.Button
Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(WebCam))
Me.picCaptura = New System.Windows.Forms.PictureBox()
Me.lstDispositivos = New System.Windows.Forms.ListBox()
Me.lblDevice = New System.Windows.Forms.Label()
Me.btnIniciar = New System.Windows.Forms.Button()
Me.btnSalvar = New System.Windows.Forms.Button()
Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
CType(Me.picCaptura, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
[ô]
[ô]picCaptura
[ô]
Me.picCaptura.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(192, Byte), Integer))
Me.picCaptura.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.picCaptura.InitialImage = Nothing
Me.picCaptura.Location = New System.Drawing.Point(12, 67)
Me.picCaptura.Name = [Ô]picCaptura[Ô]
Me.picCaptura.Size = New System.Drawing.Size(258, 302)
Me.picCaptura.TabIndex = 0
Me.picCaptura.TabStop = False
[ô]
[ô]lstDispositivos
[ô]
Me.lstDispositivos.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
Me.lstDispositivos.Location = New System.Drawing.Point(12, 67)
Me.lstDispositivos.Name = [Ô]lstDispositivos[Ô]
Me.lstDispositivos.Size = New System.Drawing.Size(133, 303)
Me.lstDispositivos.TabIndex = 1
[ô]
[ô]lblDevice
[ô]
Me.lblDevice.Location = New System.Drawing.Point(12, 46)
Me.lblDevice.Name = [Ô]lblDevice[Ô]
Me.lblDevice.Size = New System.Drawing.Size(136, 18)
Me.lblDevice.TabIndex = 2
Me.lblDevice.Text = [Ô]Dispositivos encontrados[Ô]
Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
[ô]
[ô]btnIniciar
[ô]
Me.btnIniciar.Location = New System.Drawing.Point(12, 375)
Me.btnIniciar.Name = [Ô]btnIniciar[Ô]
Me.btnIniciar.Size = New System.Drawing.Size(111, 39)
Me.btnIniciar.TabIndex = 3
Me.btnIniciar.Text = [Ô]Ligar Camera[Ô]
[ô]
[ô]btnSalvar
[ô]
Me.btnSalvar.Location = New System.Drawing.Point(146, 375)
Me.btnSalvar.Name = [Ô]btnSalvar[Ô]
Me.btnSalvar.Size = New System.Drawing.Size(124, 41)
Me.btnSalvar.TabIndex = 4
Me.btnSalvar.Text = [Ô]Fotografar[Ô]
[ô]
[ô]sfdImage
[ô]
Me.sfdImage.FileName = [Ô]Webcam1[Ô]
Me.sfdImage.Filter = [Ô]Bitmap|*.bmp[Ô]
[ô]
[ô]
[ô]WebCam
[ô]
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(285, 430)
Me.Controls.Add(Me.picCaptura)
Me.Controls.Add(Me.btnSalvar)
Me.Controls.Add(Me.btnIniciar)
Me.Controls.Add(Me.lblDevice)
Me.Controls.Add(Me.lstDispositivos)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Icon = CType(resources.GetObject([Ô]$this.Icon[Ô]), System.Drawing.Icon)
Me.Name = [Ô]WebCam[Ô]
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = [Ô]Captura de Vídeo[Ô]
Me.TransparencyKey = System.Drawing.Color.FromArgb(CType(CType(121, Byte), Integer), CType(CType(121, Byte), Integer), CType(CType(121, Byte), Integer))
CType(Me.picCaptura, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

[ô]constantes usadas na DLL

Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11

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 [ô] ID do dispositivo atual

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
[ô]verifica e carrega os dispositivos
carregaDispositivos()

If lstDispositivos.Items.Count > 0 Then
btnIniciar.Enabled = True
lstDispositivos.SelectedIndex = 0
btnIniciar.Enabled = True
Else
lstDispositivos.Items.Add([Ô]Não dispositivo de captura instalado.[Ô])
btnIniciar.Enabled = False
End If

btnSalvar.Enabled = False
picCaptura.SizeMode = PictureBoxSizeMode.StretchImage
Ligaweb()
End Sub

Private Sub carregaDispositivos()
Dim strNome As String = Space(100)
Dim strVer As String = Space(100)
Dim bRetorna As Boolean
Dim x As Integer = 0

Do
bRetorna = capGetDriverDescriptionA(x, strNome, 100, strVer, 100)

If bRetorna Then lstDispositivos.Items.Add(strNome.Trim)
x += 1
Loop Until bRetorna = False
End Sub

Private Sub abreJanelaVisualizacao()
Dim iHeight As Integer = picCaptura.Height
Dim iWidth As Integer = picCaptura.Width

[ô]
[ô] Abre a janela de visualização no picturebox
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
480, picCaptura.Handle.ToInt32, 0)

[ô]
[ô] Conecta com o drive
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
[ô]
[ô]Define a escala de previsão
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)

[ô]
[ô]Define a taxa de visualização em milisegundos
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)

[ô]
[ô]Iniciar a visualização da imagem a partir da camara
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)

[ô]
[ô] Redimensiona a janela para se ajustar no picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCaptura.Width, picCaptura.Height, _
SWP_NOMOVE Or SWP_NOZORDER)

btnSalvar.Enabled = True
btnIniciar.Enabled = False
Else
[ô]
[ô] Erro de conexão fecha a janela de dispostivos
DestroyWindow(hHwnd)

btnSalvar.Enabled = False
Ligaweb()
End If
End Sub

Private Sub btnIniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIniciar.Click
Ligaweb()
End Sub
Private Sub Ligaweb()
iDevice = lstDispositivos.SelectedIndex
If iDevice <> -1 Then
abreJanelaVisualizacao()
Else
MsgBox([Ô]Selecione um dispositivo de video...[Ô])
End If

End Sub
Private Sub fechaJanelaVisualizacao()
[ô] Desconecta do dispositivo
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

[ô] fecha a chama a janela
DestroyWindow(hHwnd)
End Sub

Private Sub btnSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
VBSTUDENT 04/06/2013 20:54:13
#424396
Ô cara valeu mesmo hein! testei seu exemplo mas quando vou capturara imagem pela Cam, me surge uma tela acinzentada ao invés do que realmente é pra ser. Quer ver? pode testar aí! também tentei pelo exemplo do Macoratti http://www.macoratti.net/07/08/vbn_wbc.htm mas tá com este mesmo
problema, inclusive na Demo que vem com o exemplo do grande Macoratti.

O que pode ser este problema? Entretanto se aí no seu pc tambem estiver desse jeito, então deva ser algo do tipo [Ô]os codes da API do Windws pra esta função de webcam já eram[Ô] rsrs.

Sinceramente estou com sérios problemas.
Faça seu login para responder