Page 1 of 3 123 LastLast
Results 1 to 40 of 93

Thread: [Vb.Net] WebCam Class (ICam)

  1. #1

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    [Vb.Net] WebCam Class (ICam)

    Ok I Wrote this class to get to grips with the .NET enviroment so the coding may not be the best but it works. I've also attatached a project which uses the class its all simple enough

    The class will make it easy for your app to view a webcamera set FPS etc.

    Ok the class,

    First 3 variables which can be changed within the class are,

    Code:
        Private CamFrameRate As Integer = 15
        Private OutputHeight As Integer = 240
        Private OutputWidth As Integer = 360

    CamFrameRate
    - Starting frame rate how much of a gap there is between frames 15ms (About 65 FPS) NOTE - You can changes the FPS through a sub this is just the initial frame rate.

    OutputHeight/OutputWidth
    - Fairly self explanitory, just sets the dimensions of output.

    VB Code:
    1. Public iRunning As Boolean

    This can be called at anytime and will return if the camera is running or not

    VB Code:
    1. Messagebox.show Mycam.Irunning

    How To Use The Class

    VB Code:
    1. Private myCam As iCam
    2.  
    3. Set myCam = New iCam

    From here you can call a range of functions using the syntax,

    VB Code:
    1. mycam.[Function Name]

    Functions

    initCam(ByVal parentH As Integer)
    - This is where it all starts you must call this to set the camera up first
    ParentH is where we want to prievew, so if we have a pictureBox on our form, named picoutput.

    VB Code:
    1. myCam.initCam(Me.picOutput.Handle.ToInt32)

    resetCam()
    - If you need to reset the camera call this function, you most proberly wont but its here incase you do

    VB Code:
    1. MyCam.ResetCam()

    setFrameRate(ByVal iRate As Long)
    - Here you can set the frame rate by passing FPS it will then be converted into how much time between frames.

    VB Code:
    1. myCam.setFrameRate(25)

    closeCam()

    - Allways call when closing the application, just clears things up.

    VB Code:
    1. myCam.CloseCam()

    copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF)

    - This sub returns an image of the current frame. You need to pass to it the source picture box (Where the camera image is) and then a rectangle specifying size dimensions.

    VB Code:
    1. Me.picStill.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
    2.                             Me.picOutput.Width, Me.picOutput.Height))

    FPS()
    - This sub returns the current FPS
    VB Code:
    1. MessageBox.Show MyCam.Fps()


    As I say code may not be great but it works good, and i just hope sompeople may find it useful. I'd appriciate people not replying here but Pm'ing me with any problems since i'd like to keep this thread as clean as possible!

    Pino
    Attached Files Attached Files
    Last edited by Hack; Jan 13th, 2006 at 10:00 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: [Vb.Net] WebCam Class (ICam)

    Is there a motion detection function included?..

  3. #3

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    I'd appriciate people not replying here but Pm'ing me with any problems since i'd like to keep this thread as clean as possible!
    No motion detection, but it can copy a frame so you just need to compare 2 frames. Please Pm me with any furthor problems

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [Vb.Net] WebCam Class (ICam)

    OK, this is my conversion, it's in 2005. The only difference I think is the Running property as 2003 doesn't support Public and Friend property with the same name.
    VB Code:
    1. Imports System.Windows.Forms
    2. Imports System.Drawing
    3.  
    4. Public Class WebCamera
    5.  
    6. #Region "Api/constants"
    7.  
    8.     Private Const WS_CHILD As Integer = &H40000000
    9.     Private Const WS_VISIBLE As Integer = &H10000000
    10.     Private Const SWP_NOMOVE As Short = &H2S
    11.     Private Const SWP_NOZORDER As Short = &H4S
    12.     Private Const WM_USER As Short = &H400S
    13.     Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_USER + 10
    14.     Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_USER + 11
    15.     Private Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_USER + 45
    16.     Private Const WM_CAP_SET_PREVIEW As Integer = WM_USER + 50
    17.     Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
    18.     Private Const WM_CAP_GET_FRAME As Long = 1084
    19.     Private Const WM_CAP_COPY As Long = 1054
    20.     Private Const WM_CAP_START As Long = WM_USER
    21.     Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
    22.     Private Const WM_CAP_SEQUENCE As Long = (WM_CAP_START + 62)
    23.     Private Const WM_CAP_SET_SEQUENCE_SETUP As Long = (WM_CAP_START + 64)
    24.     Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
    25.  
    26.     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
    27.     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
    28.     Private 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
    29.  
    30. #End Region
    31.  
    32.     Public Event ImageChanged()
    33.  
    34.     Private _Device As String
    35.     Private _hWnd As Integer
    36.     Private lwndC As Integer
    37.  
    38.     Public _Running As Boolean
    39.  
    40.     Private _FramesPerSecond As Integer = 10
    41.     Private OutputHeight As Integer = 240
    42.     Private OutputWidth As Integer = 360
    43.  
    44.     Private _LoggingFrameSpan As Integer = 0
    45.     Private _LoggingFilename As String = String.Empty
    46.     Private _LoggingCount As Integer = 0
    47.  
    48.     Private WithEvents _Picture As PictureBox
    49.  
    50.     Public Property Running() As Boolean
    51.         Get
    52.             Return _Running
    53.         End Get
    54.         Friend Set(ByVal value As Boolean)
    55.             _Running = value
    56.         End Set
    57.     End Property
    58.  
    59.     Public Property FramesPerSecond() As Integer
    60.         Get
    61.             Return _FramesPerSecond
    62.         End Get
    63.         Set(ByVal value As Integer)
    64.             _FramesPerSecond = value
    65.             ResetCamera()
    66.         End Set
    67.     End Property
    68.  
    69.     Public ReadOnly Property CurrentImage() As Image
    70.         Get
    71.             Dim CurrentPic As Image = Nothing
    72.             If Not (_Picture Is Nothing) Then
    73.                 Return _Picture.Image
    74.             End If
    75.             Return CurrentPic
    76.         End Get
    77.     End Property
    78.  
    79.     Public Sub StartFeed()
    80.         If Me.Running Then
    81.             Throw New CameraAlreadyRunningException
    82.         Else
    83.             Try
    84.                 _Picture = New PictureBox
    85.                 _hWnd = capCreateCaptureWindowA(_Device, WS_VISIBLE Or WS_CHILD, 0, 0, OutputWidth, CShort(OutputHeight), _Picture.Handle.ToInt32, 0)
    86.                 SetupCamera()
    87.             Catch Ex As Exception
    88.                 _Picture = Nothing
    89.                 Throw Ex
    90.             End Try
    91.         End If
    92.     End Sub
    93.  
    94.     Public Sub EndFeed()
    95.         If Me.Running Then
    96.             SendMessage(_hWnd, WM_CAP_DRIVER_DISCONNECT, 0, CType(0, String))
    97.             _Picture = Nothing
    98.             Me.Running = False
    99.         Else
    100.             Throw New CameraNotRunningException
    101.         End If
    102.     End Sub
    103.  
    104.     Public Sub ResetCamera()
    105.         If _Running Then
    106.             EndFeed()
    107.             Application.DoEvents()
    108.             SetupCamera()
    109.         Else
    110.             Throw New CameraNotRunningException
    111.         End If
    112.     End Sub
    113.  
    114.     Private Sub SetupCamera()
    115.         If SendMessage(_hWnd, WM_CAP_DRIVER_CONNECT, CType(_Device, Short), CType(0, String)) = 1 Then
    116.  
    117.             Dim CameraFrameRate As Short = CType(1000 \ _FramesPerSecond, Short)
    118.  
    119.             SendMessage(_hWnd, WM_CAP_SET_PREVIEWRATE, CameraFrameRate, CType(0, String))
    120.             SendMessage(_hWnd, WM_CAP_SET_PREVIEW, 1, CType(0, String))
    121.             Me.Running = True
    122.         Else
    123.             Me.Running = False
    124.             Throw New CameraSetupFailedException
    125.         End If
    126.     End Sub
    127.  
    128.     Public Sub StartLogging(ByVal Folder As String, ByVal GroupFilename As String, ByVal FrameSpan As Integer)
    129.         _LoggingFrameSpan = FrameSpan
    130.  
    131.         Dim Filename As String = Folder
    132.  
    133.         If Not Filename.EndsWith("\") Then
    134.             Filename &= "\"
    135.         End If
    136.         Filename &= GroupFilename
    137.         _LoggingFilename = Filename
    138.     End Sub
    139.  
    140.     Public Sub StopLogging()
    141.         _LoggingFrameSpan = 0
    142.         _LoggingFilename = String.Empty
    143.         _LoggingCount = 0
    144.     End Sub
    145.  
    146.     Private Sub _Picture_BackgroundImageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Picture.BackgroundImageChanged
    147.         If _LoggingFrameSpan > 0 Then
    148.             Dim LogImage As Boolean = _LoggingCount = 0 OrElse (_LoggingCount Mod _LoggingFrameSpan) = 0
    149.             _LoggingCount += 1
    150.             If LogImage Then
    151.                 LogCurrentImage()
    152.             End If
    153.         End If
    154.         RaiseEvent ImageChanged()
    155.     End Sub
    156.  
    157.     Private Sub LogCurrentImage()
    158.         CurrentImage.Save(_LoggingFilename & _LoggingCount.ToString)
    159.     End Sub
    160.  
    161. End Class
    I also created some custom exceptions:
    VB Code:
    1. Public Class CameraNotRunningException
    2.     Inherits Exception
    3.  
    4.     Public Sub New()
    5.         MyBase.New("Camera is not currently running")
    6.     End Sub
    7. End Class
    8.  
    9. Public Class CameraSetupFailedException
    10.     Inherits Exception
    11.  
    12.     Public Sub New()
    13.         MyBase.New("Camera setup failed")
    14.     End Sub
    15. End Class
    16.  
    17. Public Class CameraAlreadyRunningException
    18.     Inherits Exception
    19.  
    20.     Public Sub New()
    21.         MyBase.New("Camera is already running")
    22.     End Sub
    23. End Class
    Now to use this code you need to do something like:
    VB Code:
    1. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    2.         _Camera = New WebCamLib.WebCamera
    3.         _Camera.StartFeed()
    4.     End Sub
    5.  
    6.     Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
    7.         If _Camera.Running Then
    8.             _Camera.EndFeed()
    9.         End If
    10.         _Camera = Nothing
    11.     End Sub
    12.  
    13.     Private Sub _Camera_ImageChanged() Handles _Camera.ImageChanged
    14.         Dim NewImage As Image = _Camera.CurrentImage
    15.  
    16.         'display image on form
    17.  
    18.     End Sub
    I haven't had time to really test this, it's merely an alpha release. As you can see you can start logging at any time by using the StartLogging sub.
    The logging I hope to seperate into a seperate class that deals soley with logging.

    ANyways, have a play, tell me where I have gone wrong.

    Woof

  5. #5
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [Vb.Net] WebCam Class (ICam)

    Hi,
    I changed WebCamViewer to get the real frame rate using the CallBack function and I get about 6 frames per second.
    Can you really have a 25 frames/s ?

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [Vb.Net] WebCam Class (ICam)

    What did u change? Pino's vb6 version, or my .NET conversion?

    Woof

  7. #7
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [Vb.Net] WebCam Class (ICam)

    I changed .Net code.

  8. #8

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    Woka!

    My Code was .net!

    My code was .Net 2003 wokas is .net 2005

  9. #9
    Member
    Join Date
    Jul 2006
    Location
    Taiwan
    Posts
    37

    Re: [Vb.Net] WebCam Class (ICam)

    are these code applicable to all webcam ?
    i'm using logitech quickcam express

    woka's code consist of WebCamLib ?
    what is it ? i got an error when debugging

  10. #10
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Re: [Vb.Net] WebCam Class (ICam)

    If another application is using the web cam and we start our application, a selection device window apears, what can I do to not see this window? (sorry for my english, is not perfect) I been waiting for a solution for this problem for like two weeks, can someone help me?

  11. #11
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [Vb.Net] WebCam Class (ICam)

    Hi,
    I get the solution, after a long an hard job !
    See VB.NET sources here : http://edid.free.fr/shared/webcam/index.php
    Solution is in the registry database.
    Last edited by edid; Jan 11th, 2007 at 06:57 AM.

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [Vb.Net] WebCam Class (ICam)

    Nice code...just looking at it now.
    Although he's used the VisualBasic namespace I believe, and the code in certain places is a little left to be desired Could really do with going through and tidying all the lose ends etc.

    I actually cannot get it to work either...but am still working on that.

    Woka

  13. #13
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [Vb.Net] WebCam Class (ICam)

    I'll be happy you give me advices.

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [Vb.Net] WebCam Class (ICam)

    Well remove the VisualBasic reference from the project.

    Then slowly convert the code to "proper" .NET, ie:

    VB Code:
    1. strText = Len(strUsername)
    would change to:
    VB Code:
    1. strText = strUsername.Length

    Others include:

    VB Code:
    1. Err.raise(-1, "Class Name", "Err Description")
    changes to:
    VB Code:
    1. Throw New Exception("Err Description")
    and
    VB Code:
    1. MsgBox("Hello!")
    to
    VB Code:
    1. MessageBox.Show("Hello!")

    The code itself isn't the neatest. Poor error handling, poor variable naming and the code construction isn't the best.

    An example of this would be:
    VB Code:
    1. Module MTools
    2.     Public Function MREG_GetRoot(ByVal psNom As String, Optional ByVal pbReadOnly As Boolean = False) As Microsoft.Win32.RegistryKey
    3.         Dim lRegRoot As Microsoft.Win32.RegistryKey
    4.         Dim lsRegRoot As String
    5.         lsRegRoot = UCase(psNom.Split("\")(0))
    6.         Select Case UCase(lsRegRoot)
    7.             Case "HKEY_CURRENT_USER"
    8.                 MREG_GetRoot = Microsoft.Win32.Registry.CurrentUser
    9.             Case "HKEY_LOCAL_MACHINE"
    10.                 MREG_GetRoot = Microsoft.Win32.Registry.LocalMachine
    11.             Case "HKEY_CLASSES_ROOT"
    12.                 MREG_GetRoot = Microsoft.Win32.Registry.ClassesRoot
    13.             Case "HKEY_CURRENT_CONFIG"
    14.                 MREG_GetRoot = Microsoft.Win32.Registry.CurrentConfig
    15.             Case "HKEY_USERS"
    16.                 MREG_GetRoot = Microsoft.Win32.Registry.Users
    17.             Case Else
    18.                 Err.Raise(-1, "MREG_GetRoot", "Unknown root : " & psNom)
    19.         End Select
    20.         If InStr(psNom, "\") > 0 Then
    21.             lsRegRoot = psNom.Substring(InStr(psNom, "\"))
    22.             If lsRegRoot <> "" Then
    23.                 MREG_GetRoot = MREG_GetRoot.OpenSubKey(lsRegRoot, Not pbReadOnly)
    24.             End If
    25.         End If
    26.     End Function
    27. End Module
    This should be changed to:
    VB Code:
    1. Public Class MTools
    2.     Public Shared Function MREG_GetRoot(ByVal psNom As String, Optional ByVal pbReadOnly As Boolean = False) As Microsoft.Win32.RegistryKey
    3.        
    4.     Dim lsRegRoot As String = (psNom.Split("\")(0)).ToUpper()
    5.  
    6.     Dim regKey As Microsoft.Win32.RegistryKey    
    7.  
    8.     Select Case lsRegRoot
    9.             Case "HKEY_CURRENT_USER"
    10.                 regKey = Microsoft.Win32.Registry.CurrentUser
    11.             Case "HKEY_LOCAL_MACHINE"
    12.                 regKey = Microsoft.Win32.Registry.LocalMachine
    13.             Case "HKEY_CLASSES_ROOT"
    14.                 regKey = Microsoft.Win32.Registry.ClassesRoot
    15.             Case "HKEY_CURRENT_CONFIG"
    16.                 regKey = Microsoft.Win32.Registry.CurrentConfig
    17.             Case "HKEY_USERS"
    18.                 regKey = Microsoft.Win32.Registry.Users
    19.             Case Else
    20.                 Throw New Exception("Unknown root : " & psNom)
    21.         End Select
    22.  
    23.         If psNom.Contains("\") Then
    24.             lsRegRoot = psNom.Substring(psNom.IndexOf("\"))
    25.             If lsRegRoot.Length > 0 Then
    26.                 regKey = regKey.OpenSubKey(lsRegRoot, Not pbReadOnly)
    27.             End If
    28.         End If
    29.  
    30.     return regKey
    31.        
    32.     End Function
    33. End Class
    Just daft things like that throughout the entire code, makes it hard to follow. Still can't get it working on my pc. I keep getting object not set errors.

    Will take a few hours to convert completely, and then a few more to iron out all the bugs.

    Hope that helps.

    WOka

  15. #15
    New Member
    Join Date
    Aug 2007
    Posts
    1

    Re: [Vb.Net] WebCam Class (ICam)

    I want to save each 10 Sekonds a new picture.
    In the following function i don´t know how I can pass the new picture form the cam to bmp.
    The bmp.Save function is running correctly, I already tested it.


    Private Sub Save()
    Me.Show()
    Dim myCam As New iCam
    Dim i As Integer = 0
    Dim numerOfPicture As String
    Dim bmp As Bitmap
    numerOfPicture = i.ToString
    bmp = '**********this line is the problem**********
    bmp.Save("C:\Pictures\Picture" + numerOfPicture + ".bmp")
    i = i + 1
    Me.Close()
    End Sub



    I tried to use the origin code by change it a little, but I got a big Problem.
    The following changes work correctly if I click the button every 10 Sekonds.
    Now the problem: If I let the Button click by software, in the pictureBox is no longer the picture but a part of the
    desktop which is seen right behind the pictureBox.

    Private myCam As iCam

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.picOutput.SizeMode = PictureBoxSizeMode.StretchImage
    myCam = New iCam
    myCam.initCam(Me.picOutput.Handle.ToInt32)
    End Sub

    Private Sub cmdViewStill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewStill.Click
    If myCam.iRunning Then
    Dim a As FrmImage
    a = New FrmImage

    a.picImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
    Me.picOutput.Width, Me.picOutput.Height))
    Me.Show()
    Else
    MessageBox.Show("Camera Is Not Running!")
    End If
    End Sub

    Now I want to save the picture:

    Private Sub Save()
    Me.Show()
    Dim myCam As New iCam
    Dim i As Integer = 0
    Dim numerOfPicture As String
    Dim bmp As Bitmap
    numerOfPicture = i.ToString
    bmp = CType(picImage.Image, Bitmap)
    bmp.Save("C:\Pictures\Picture" + numerOfPicture + ".bmp")
    i = i + 1
    Me.Close()
    End Sub

    and this works If I click the button who is starting Save(), but If I let the Button click by software,
    in the pictureBox is no longer the picture but a part of the
    desktop which is seen right behind the pictureBox.
    So I thought don´t use a button but a funtion wich starts without a button to take a picture every 10 Sekonds.
    -> The same result:In the pictureBox is the desktop which is seen right behind the pictureBox.
    -> And in the file it is also.

    Could somebody show me a line of code with wich I can save the taken picture of the cam directly in a bitmap.
    Then I could simply save the bitmap.

    That means to replace these 2 lines:

    a.picImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
    Me.picOutput.Width, Me.picOutput.Height))

    in something like:

    Dim bmp as Bitmap
    bmp = '*****here should the cam pass one picture to bmp so I can save bmp direktly*****

    I would be obliged if somebody could help me.

    I use WinXP and
    VisualBasic Express

  16. #16
    New Member
    Join Date
    Oct 2007
    Posts
    5

    Re: [Vb.Net] WebCam Class (ICam)

    I downloaded the original source for VS2003 Framework 1.1 and it appeared to work fine. I wanted to use the 2005 Framework 2.0 copy that Wokawidget posted though. I put it into VS2005 and it compiled without errors. I can start and stop the webcam (the green light goes on and off) however the ImageChanged event never seems to fire even when the cam is on (I put logging in that event in the class to verify it... I also kept tabs on the _Picture variable and it doesn't appear to have anything, like the system isn't returning it).

    I know the class is talking to the web cam somewhat because it will turn it on and off, but I can't see to pull the images from it. Thoughts?

    Here's the example code from my form that's trying to use the class:

    vb Code:
    1. Private WithEvents _Camera As WebCamera
    2.  
    3.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    4.         _Camera = New WebCamera
    5.         _Camera.StartFeed()
    6.     End Sub
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         _Camera = New WebCamera
    10.         _Camera.StartFeed()
    11.     End Sub
    12.  
    13.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    14.         If _Camera.Running Then
    15.             _Camera.EndFeed()
    16.         End If
    17.     End Sub
    18.  
    19.     Private Sub _Camera_ImageChanged() Handles _Camera.ImageChanged
    20.         Me.Text = "Changed at " & Now().ToString
    21.         Dim NewImage As Image = _Camera.CurrentImage
    22.         PictureBox1.Image = NewImage
    23.         NewImage.Dispose()
    24.         NewImage = Nothing
    25.     End Sub

  17. #17
    New Member
    Join Date
    Dec 2006
    Location
    Cornwall, England
    Posts
    1

    Re: [Vb.Net] WebCam Class (ICam)

    thanks for this code its ace.

    One little problem i have found, im using VS 2008 and have managed to get an application to work on my laptop which opens up a 348x270 capture window, enables to user to press "Take Photo" and save. Perfect yeah...

    Well now ive tried transferring the application it to another PC i only get half of the picture box showing (capture frame) and the other half is the form behind coming through, as if the window isnt redrawing corrrectly so you dont get a proper image being viewed let alone saved? Does that make sense?

    Any suggestions?
    Last edited by gonkowonko; Jan 22nd, 2008 at 11:13 AM.

  18. #18
    New Member
    Join Date
    Feb 2007
    Posts
    14

    Thumbs up Re: [Vb.Net] WebCam Class (ICam)

    Hello,

    This code is amazing

    I as wondering if there would be a way to add effects to the webcam stream. Like black and white, filters etc.

    Let me know.

    Thanks, Adam.

  19. #19

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    I'm not too sure if you can add any affects to the stream. I'll have a look into it if I get a few minutes!


  20. #20
    New Member
    Join Date
    Jan 2008
    Posts
    1

    Re: [Vb.Net] WebCam Class (ICam)

    Thanks for the great code.

    I am trying to modify the code so that I can capture the image not just with a button, but also when the user presses the button on the webcam.

    Any suggestions?

    Thanks,
    Didius

  21. #21

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    Didius,

    Thanks! As for your question, I'm not too sure that would involve looking at the hardware you are using.

    Thanks

  22. #22
    Member
    Join Date
    Nov 2007
    Posts
    58

    Re: [Vb.Net] WebCam Class (ICam)

    Ok ... i managed to capture images from webcam ... but I need to send those images to another computer ... like an livestream ... how can I do that ?

  23. #23
    New Member
    Join Date
    Apr 2008
    Posts
    1

    Re: [Vb.Net] WebCam Class (ICam)

    I took the time to register for the forum just so that I could properly thank pino for this code. it was precisely what I need for a project I'm working on. this was after trying several others that didn't work or didn't work well. thanks!

  24. #24

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    Quote Originally Posted by jaguar22
    I took the time to register for the forum just so that I could properly thank pino for this code. it was precisely what I need for a project I'm working on. this was after trying several others that didn't work or didn't work well. thanks!
    Thats really great to hear!

    Good luck with the project!

    Pino

  25. #25
    Member
    Join Date
    Nov 2007
    Posts
    58

    Re: [Vb.Net] WebCam Class (ICam)

    well jaguar ...

    I think too the pino's code is great ... but too advanced for what I need right now ...

    a great code that works can be found at http://www.webtropy.com/articles/art7-2.asp ... it's easy to use too

    pino thanks for the code

  26. #26

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [Vb.Net] WebCam Class (ICam)

    Quote Originally Posted by makko
    well jaguar ...

    I think too the pino's code is great ... but too advanced for what I need right now ...

    a great code that works can be found at http://www.webtropy.com/articles/art7-2.asp ... it's easy to use too

    pino thanks for the code
    Too advanced? The code above uses the exact same method. My class just provides a simple interface.

    Pino
    Last edited by Pino; Apr 7th, 2008 at 04:32 AM.

  27. #27
    New Member
    Join Date
    May 2008
    Location
    Kent, England
    Posts
    4

    Re: [Vb.Net] WebCam Class (ICam)

    great webcam code, by far the best way of doing it in VB, I've added motion detection to it, pic below, this is my first attempt at motion detection so the code maybe very grude.



    I will post the source later when I've cleaned it up a little.

    I thought i would add the motion detection with a visual twist, you can see what motion has been picked up by the green indicator. I also added track bars which control how sensitive it is, and also the object size it responds to. Might be handy for a security thing, you can set the size to only go off to people and not a cat for example.

    Kind Regards,
    OuTa-SyNc

  28. #28
    New Member
    Join Date
    May 2008
    Posts
    5

    Talking Re: [Vb.Net] WebCam Class (ICam)

    Quote Originally Posted by OuTa-SyNc
    great webcam code, by far the best way of doing it in VB, I've added motion detection to it, pic below, this is my first attempt at motion detection so the code maybe very grude.



    I will post the source later when I've cleaned it up a little.

    I thought i would add the motion detection with a visual twist, you can see what motion has been picked up by the green indicator. I also added track bars which control how sensitive it is, and also the object size it responds to. Might be handy for a security thing, you can set the size to only go off to people and not a cat for example.

    Kind Regards,
    OuTa-SyNc






    Sir i was Looking for this type of application can you please post a draft here or email me perhaps. I need this for my thesis im gonna add sms features to it and post it here. please please please .....

  29. #29
    New Member
    Join Date
    May 2008
    Location
    Kent, England
    Posts
    4

    Re: [Vb.Net] WebCam Class (ICam)

    here you go...full source iCam with Motion detection

    ignore the commented out lines, these are for a project I'm working on. Like I said the method I used may be grude but it works...and at the same speed as the webcam. It uses 2 timers, each is set with an interval the same as the frame rate (i.e. if the cam is set to 30 FPS then the timer interval would be 33) but I changed it to 30.

    If anyone can improve it please post back, as I would like an optimised version myself

  30. #30
    New Member
    Join Date
    May 2008
    Posts
    5

    Talking Re: [Vb.Net] WebCam Class (ICam)

    removed for privacy purposes
    Last edited by Mark Lloyd Duatin; Nov 9th, 2014 at 04:30 PM.

  31. #31
    New Member
    Join Date
    May 2008
    Location
    Kent, England
    Posts
    4

    Re: [Vb.Net] WebCam Class (ICam)

    does anyone know how I can hide the cam picturebox and display only the output produced from the motion detection?

    If I set the cam picturebox to visible = false nothing works because the camera output is not being produced, basically I want to get the frames from the camera, add my motion detection method to it and display the results in the output picturebox only. I've tried rendering over the image set in the cam picturebox but with no luck.

    Not sure if anyone has noticed but if you place anything in front of the picturebox with the cam running then it is treader as output produced from the cam???

    Thanks in advance

  32. #32
    New Member
    Join Date
    May 2008
    Posts
    5

    Re: [Vb.Net] WebCam Class (ICam)

    hey since you helped me haha im helping you too here i solved the problem and added a feature where you can select the device where the program will get the image.


    do re upload your webcam motion detection using my code here. i used copying from the clipboard and retrieving it..

    if anyone can optimize this please do so thnx
    Attached Files Attached Files

  33. #33
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Re: [Vb.Net] WebCam Class (ICam)

    Hello,
    a simply great class.
    How can I get a second camera? I'm testing it now with two cameras.

    Thanks.

  34. #34
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Lightbulb Re: [Vb.Net] WebCam Class (ICam)

    hi every body this class is a develop on icam
    and can capture image with no problem even when the window is minimized
    no problem .with time adding .auto capturing and ......
    Attached Files Attached Files

  35. #35
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: [Vb.Net] WebCam Class (ICam)

    Hi guys

    If I was to use this class without using a picturebox to show the webcam feed all the time but rather initiate the camera and then when buttons are clicked grab a picture then how would I go about doing that?

    I can only initiate the camera with a handle to a picturebox as of now

  36. #36
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: [Vb.Net] WebCam Class (ICam)

    Ok so I found a somewhat workable solution to my own problem.

    As wrote if I placed the picturebox outside the form the GrabIt picture is black also if I make the picturebox 0,0 in size the picture would be black.

    So what I did was to leave the picturebox at 1,1 in size and place it somewhere on the main form where it is unnoticed and I can now GrabIt and get perfect webcam shot when buttons are clicked.

  37. #37
    New Member
    Join Date
    Feb 2009
    Posts
    1

    Exclamation Re: [Vb.Net] WebCam Class (ICam)

    Quote Originally Posted by OuTa-SyNc
    here you go...full source iCam with Motion detection

    ignore the commented out lines, these are for a project I'm working on. Like I said the method I used may be grude but it works...and at the same speed as the webcam. It uses 2 timers, each is set with an interval the same as the frame rate (i.e. if the cam is set to 30 FPS then the timer interval would be 33) but I changed it to 30.

    If anyone can improve it please post back, as I would like an optimised version myself
    Can someone please re-attach the BBIC.zip? The website is no-longer.

    Thanks,

    D.M

  38. #38
    New Member
    Join Date
    May 2008
    Location
    Kent, England
    Posts
    4

    Re: [Vb.Net] WebCam Class (ICam)

    I didn't realise that I deleted from my server

    iCam with Motion detection

  39. #39
    New Member
    Join Date
    Mar 2009
    Posts
    2

    Re: [Vb.Net] WebCam Class (ICam)

    This is simpe webcam code. You create simple user interface with startrecord, stoprecord, and stopcam, one listbox contain webcam device.
    change name of button according to name in sourcecode and you can use this code to record webcam
    vb.net Code:
    1. Public Class Form1
    2.     Const WM_CAP_START = &H400S
    3.     Const WS_CHILD = &H40000000
    4.     Const WS_VISIBLE = &H10000000
    5.  
    6.     Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
    7.     Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
    8.     Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
    9.     Const WM_CAP_SEQUENCE = WM_CAP_START + 62
    10.     Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
    11.  
    12.     Const WM_CAP_SET_SCALE = WM_CAP_START + 53
    13.     Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
    14.     Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
    15.  
    16.     Const SWP_NOMOVE = &H2S
    17.     Const SWP_NOSIZE = 1
    18.     Const SWP_NOZORDER = &H4S
    19.     Const HWND_BOTTOM = 1
    20.     Dim count As Integer = 0
    21.     '--The capGetDriverDescription function retrieves the version
    22.     ' description of the capture driver--
    23.     Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
    24.        (ByVal wDriverIndex As Short, _
    25.         ByVal lpszName As String, ByVal cbName As Integer, _
    26.         ByVal lpszVer As String, _
    27.         ByVal cbVer As Integer) As Boolean
    28.  
    29.     '--The capCreateCaptureWindow function creates a capture window--
    30.     Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
    31.        (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
    32.         ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
    33.         ByVal nHeight As Short, ByVal hWnd As Integer, _
    34.         ByVal nID As Integer) As Integer
    35.  
    36.     '--This function sends the specified message to a window or windows--
    37.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    38.        (ByVal hwnd As Integer, ByVal Msg As Integer, _
    39.         ByVal wParam As Integer, _
    40.        <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
    41.  
    42.     '--Sets the position of the window relative to the screen buffer--
    43.     Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
    44.        (ByVal hwnd As Integer, _
    45.         ByVal hWndInsertAfter As Integer, ByVal x As Integer, _
    46.         ByVal y As Integer, _
    47.         ByVal cx As Integer, ByVal cy As Integer, _
    48.         ByVal wFlags As Integer) As Integer
    49.  
    50.     '--This function destroys the specified window--
    51.     Declare Function DestroyWindow Lib "user32" _
    52.        (ByVal hndw As Integer) As Boolean
    53.  
    54.     '---used to identify the video source---
    55.     Dim VideoSource As Integer
    56.     '---used as a window handle---
    57.     Dim hWnd As Integer
    58.  
    59.     Private Sub Form1_Load( _
    60.        ByVal sender As System.Object, _
    61.        ByVal e As System.EventArgs) Handles MyBase.Load
    62.         btnStartRecording.Enabled = True
    63.         btnStopRecording.Enabled = False
    64.         '---list all the video sources---
    65.         ListVideoSources()
    66.         VideoSource = 0
    67.         PreviewVideo(pbctrl)
    68.        
    69.     End Sub
    70.     Private Sub ListVideoSources()
    71.         Dim DriverName As String = Space(80)
    72.         Dim DriverVersion As String = Space(80)
    73.         For i As Integer = 0 To 9
    74.             If capGetDriverDescriptionA(i, DriverName, 80, _
    75.                DriverVersion, 80) Then
    76.                 lstVideoSources.Items.Add(DriverName.Trim)
    77.             End If
    78.         Next
    79.     End Sub
    80.     '---list all the video sources---
    81.     Private Sub lstVideoSources_SelectedIndexChanged( _
    82.        ByVal sender As System.Object, ByVal e As System.EventArgs) _
    83.        Handles lstVideoSources.SelectedIndexChanged
    84.         '---check which video source is selected---
    85.         VideoSource = lstVideoSources.SelectedIndex
    86.         '---preview the selected video source
    87.         PreviewVideo(pbCtrl)
    88.     End Sub
    89.     '---preview the selected video source---
    90.     Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
    91.         hWnd = capCreateCaptureWindowA(VideoSource, _
    92.             WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
    93.             0, pbCtrl.Handle.ToInt32, 0)
    94.  
    95.         If SendMessage( _
    96.            hWnd, WM_CAP_DRIVER_CONNECT, _
    97.            VideoSource, 0) Then
    98.  
    99.             '---set the preview scale---
    100.             SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
    101.             '---set the preview rate (ms)---
    102.             SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
    103.             '---start previewing the image---
    104.             SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
    105.             '---resize window to fit in PictureBox control---
    106.             SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
    107.                pbCtrl.Width, pbCtrl.Height, _
    108.                SWP_NOMOVE Or SWP_NOZORDER)
    109.         Else
    110.             '--error connecting to video source---
    111.             DestroyWindow(hWnd)
    112.         End If
    113.     End Sub
    114.     '---stop the preview window---
    115.     Private Sub btnStopCamera_Click( _
    116.        ByVal sender As System.Object, _
    117.        ByVal e As System.EventArgs) _
    118.        Handles btnStopCamera.Click
    119.         StopPreviewWindow()
    120.     End Sub
    121.     '--disconnect from video source---
    122.     Private Sub StopPreviewWindow()
    123.         SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
    124.         DestroyWindow(hWnd)
    125.     End Sub
    126.     '---Start recording the video---
    127.     Private Sub btnStartRecording_Click( _
    128.        ByVal sender As System.Object, _
    129.        ByVal e As System.EventArgs) _
    130.        Handles btnStartRecording.Click
    131.         btnStartRecording.Enabled = False
    132.         btnStopRecording.Enabled = True
    133.         '---start recording---
    134.         SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0)
    135.     End Sub
    136.     '---stop recording and save it on file---
    137.     Private Sub btnStopRecording_Click( _
    138.        ByVal sender As System.Object, _
    139.        ByVal e As System.EventArgs) _
    140.        Handles btnStopRecording.Click
    141.         btnStartRecording.Enabled = True
    142.         btnStopRecording.Enabled = False
    143.         '---save the recording to file---
    144.         SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\Users\Public\Videos\Sample Videos\recordedvideo.avi")
    145.  
    146.     End Sub
    End Class

  40. #40
    New Member
    Join Date
    Apr 2009
    Posts
    3

    Re: [Vb.Net] WebCam Class (ICam)

    very nice class...

    however i think this will be great if there is a record functionality.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width