Username: Password: oppure
ProCam - iCam.vb

iCam.vb

Caricato da: Lollo 97
Scarica il programma completo

  1. Option Explicit On
  2. Option Strict On
  3. Public Class iCam      
  4.        
  5.         #Region "Api/constants"
  6.        
  7.         Private Const WS_CHILD As Integer = &H40000000
  8.         Private Const WS_VISIBLE As Integer = &H10000000
  9.         Private Const SWP_NOMOVE As Short = &H2S
  10.         Private Const SWP_NOZORDER As Short = &H4S
  11.         Private Const WM_USER As Short = &H400S
  12.         Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_USER + 10
  13.         Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_USER + 11
  14.         Private Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_USER + 45
  15.         Private Const WM_CAP_SET_PREVIEW As Integer = WM_USER + 50
  16.         Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
  17.         Private Const WM_CAP_GET_FRAME As Long = 1084
  18.         Private Const WM_CAP_COPY As Long = 1054
  19.         Private Const WM_CAP_START As Long = WM_USER
  20.         Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
  21.         Private Const WM_CAP_SEQUENCE As Long = (WM_CAP_START + 62)
  22.         Private Const WM_CAP_SET_SEQUENCE_SETUP As Long = (WM_CAP_START + 64)
  23.         Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
  24.         Private Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
  25.         Private Const WM_CAP_FILE_SAVEDIB = WM_USER + 25
  26.        
  27.         Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Short, ByVal lParam As String) As Integer
  28.         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 Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
  29.     Public 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
  30.         Private Declare Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean
  31.        
  32.         #End Region
  33.        
  34.         #Region "Variables"
  35.         Private iDevice As String
  36.         Private hHwnd As Integer
  37.         Private lwndC As Integer
  38.         Public StatusString As String
  39.         Public iRunning As Boolean = False
  40.         Private CamFrameRate As Integer = 15
  41.         Private OutputHeight As Integer = 760
  42.         Private OutputWidth As Integer = 1024
  43.         Public PathString As String
  44.         Private Temp As Image
  45.         #End region
  46.        
  47.         Public Sub resetCam()
  48.                 If iRunning Then
  49.                         CloseCam()
  50.                         Application.DoEvents()
  51.                         StatusString = "WebCam resettata"
  52.                 End If
  53.         End Sub
  54.        
  55.         Public Sub initCam(ByVal parentH As Integer,ByVal currentDevice As String)
  56.                 If iRunning = False Then
  57.                         currentDevice = iDevice
  58.                         hHwnd = capCreateCaptureWindowA(currentDevice, WS_VISIBLE Or WS_CHILD, 0, 0, OutputWidth, CShort(OutputHeight), parentH, 0)
  59.                         If setCam() = False Then
  60.                                 StatusString = "Errore nel set-up della WebCam"
  61.                         End If
  62.                 Else
  63.                         StatusString = "WebCam già avviata"
  64.                 End If
  65.         End Sub
  66.        
  67.         Public Sub setFrameRate(ByVal iRate As Long)
  68.                 Try
  69.                         CamFrameRate = CInt(1000 / iRate)
  70.                         resetCam()
  71.                 Catch
  72.                         StatusString = "Valore FPS errato!"
  73.                 End Try
  74.         End Sub
  75.        
  76.         Private Function setCam() As Boolean
  77.                 Try
  78.                         If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, CShort(iDevice), CType(0, String)) = 1 Then
  79.                                 SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, CShort(CamFrameRate), CType(0, String))
  80.                                 SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, CType(0, String))
  81.                                 Me.iRunning = True
  82.                                 Return True
  83.                         Else
  84.                                 Me.iRunning = False
  85.                                 Return False
  86.                         End If
  87.                 Catch
  88.                         StatusString = "Errore impostazioni WebCam"
  89.                         Return False
  90.                 End Try
  91.         End Function
  92.        
  93.         Public Sub CloseCam()
  94.                 Try
  95.                         If Me.iRunning Then
  96.                                 SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, CType(0, String))
  97.                                 Me.iRunning = False
  98.                                 StatusString = "WebCam chiusa"
  99.                         End If
  100.                 Catch
  101.                         StatusString = "Errore nella chiusura della WebCam"
  102.                 End Try
  103.         End Sub
  104.        
  105.         Public Function FPS() As Integer
  106.                 Try
  107.                         Return CInt(1000 / (CamFrameRate))
  108.                 Catch
  109.                         Return Nothing
  110.                 End Try
  111.         End Function
  112.        
  113.         Public Sub SaveFrame(ByVal FileName As String)
  114.                 Try
  115.                         SendMessage(hHwnd, WM_CAP_FILE_SAVEDIB, 0&, FileName)
  116.                         StatusString = "Frame salvato con successo in "&FileName
  117.                 Catch
  118.                         StatusString = "Errore nel salvataggio del File"
  119.                 End Try
  120.         End Sub
  121.        
  122.         Public Function ConvertBMP(ByVal BMPFullPath As String, _
  123.                 ByVal imgFormat As Imaging.ImageFormat) As Boolean
  124.                 Dim Ans As Boolean
  125.                 Dim sNewFile As String
  126.                 Dim objBmp As New Bitmap(BMPFullPath)
  127.                 Try
  128.                         sNewFile = IO.Path.GetDirectoryName(BMPFullPath)
  129.                         sNewFile &= IO.Path.GetFileNameWithoutExtension(BMPFullPath)
  130.                         sNewFile &= "." & imgFormat.ToString
  131.                         objBmp.Save(sNewFile, imgFormat)
  132.                         StatusString = "Conversione effettuata con successo in "&sNewFile
  133.                         PathString = IO.Path.GetDirectoryName(sNewFile)
  134.                         Ans = True
  135.                         Return Ans
  136.                 Catch
  137.                         StatusString = "Conversione fallita."
  138.                         Ans = False
  139.                         Return Ans
  140.                 End Try
  141.         End Function
  142.        
  143.         Public Sub StopVideo(ByVal FileName As String)
  144.                 Try
  145.                         If iRunning
  146.                                 SendMessage(hHWnd, WM_CAP_FILE_SAVEAS, 0,FileName)
  147.                                 StatusString = "Video fermato e salvato in "&FileName
  148.                         End If
  149.                 Catch
  150.                         StatusString = "Impossibile salvare il Video"
  151.                 End Try
  152.         End Sub
  153.        
  154.         Public Sub RecordVideo()
  155.                 Try
  156.                         If iRunning
  157.                                 SendMessage(hHWnd, WM_CAP_SEQUENCE, 0, Nothing)
  158.                                 StatusString = "Video in registrazione..."
  159.                         End If
  160.                 Catch
  161.                         StatusString = "Impossibile registrare il video!"
  162.                 End Try
  163.         End Sub
  164.        
  165. End Class