Results 1 to 34 of 34

Thread: Video capture

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Thumbs down Video capture

    Hi
    i m using:
    DoEvents
    mCapHwnd = capCreateCaptureWindow("WebcamCapture", 0, 0, 0, 640, 480, Form1.hwnd, 0)
    temp = SendMessage(mCapHwnd, CONNECT, 0, 0)
    temp = SendMessage(mCapHwnd, GET_SEQUENCE, 0, 0)
    temp = SendMessage(mCapHwnd, RECORD, 20, "C:\myvideo1.avi")
    these API's to capture video from a webcam and the program records the video. The only problem i am facing is that when the application starts recording video it does not stop until the Esc ket is hit. I tried using a timer to call SendMessage(mCapHwnd, STOPP, 0, 0) API but the application does not run the timer until the recording has finished using Esc key.
    Can some one pls help me in what i m doing wrong. is there an API that can send a message to windows to record until (e.g. 60 sec) of video or is there a way i can use timer to stop the recording.
    Thanks
    Shoazib

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Video capture

    Quote Originally Posted by shoazib
    I tried using a timer to call SendMessage(mCapHwnd, STOPP, 0, 0) API but the application does not run the timer until the recording has finished using Esc key.
    You put the SendMessage call in a timer and it didn't run? When you are recording does the program freeze (become not responsive)? Could you use SendKeys to press the Esc button? Or do you need to press Esc when the app is in focus only?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Re: Video capture

    Quote Originally Posted by manavo11
    You put the SendMessage call in a timer and it didn't run? When you are recording does the program freeze (become not responsive)? Could you use SendKeys to press the Esc button? Or do you need to press Esc when the app is in focus only?
    Thanks for replying. When I am recording the program does not freeze, infact the mouse shows like it is loading something and the program does record too. I dont know how to use the sendkeys but I do have to press Esc key on the keyboard for the application to stop recording.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Video capture

    VB Code:
    1. SendKeys "{Esc}"

    This will simulate the press of the escape key on the keyboard.


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,287

    Re: Video capture

    To specify a duration for your capture send the WM_CAP_SET_SEQUENCE_SETUP message prior to recording. The lParam parameter specifies a CAPTUREPARMS structure, where you set the fLimitEnabled member to 1 (to indicate that you require a time limit). The wTimeLimit member specifies the amount of seconds to record, so set this value to 60.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Re: Video capture

    Thank you megatron for replying. You provided useful information. I tried doing the way you told. I get a type mismatch error on the last paramater (Capparms):
    SendMessage(mCapHwnd, WM_CAP_SET_SEQUENCE_SETUP, Len(Capparms), Capparms)
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Do you know how i should change the IParam
    Thanks

  7. #7
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,287

    Re: Video capture

    Pass "lParam" as Any. VB will take care of the dirty work for you.

    Your new definition should read:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Re: Video capture

    Thank you again for helping me out it worked. I need to implement motion detecion in my program. I found a code for it on the internet and by adding a picture box on it get frames and puts it on the clipboard. The problem with this is that it I have to have a picture box for motion detection and another window that I use through the capcreate function. I need to detect motion in the capreate function. so I added a function as advised by my friend and send the video handle and the Videohdr structure in it. This funcion has the same motion detection code I was usinf early for picture box we modified a little bit. The error i am getting is for this line
    c = lpVHdr.lpData((i * inten * Tppx) * (j * inten * Tppy)) "Expected array error"
    the original code for this line said:
    c = picSenseMotion.Point(i * inten * Tppx, j * inten * Tppy)
    I am attaching the whole function here to for the reference. Can someone pleas help me with this or if they have any other motion detection solution to this ?
    Public Function FrameCalBackFunc(ByVal hCapWnd As Long, ByRef lpVHdr As VIDEOHDR) As Long



    'Get the picture from camera.. the main part
    Dim temp As Long
    Dim temp1 As Long
    'temp = SendMessage(mCapHwnd, GET_FRAME, 0, 0)
    'Call capGrabFrame(hCapWnd)
    ' Call capGrabFrameNoStop(hCapWnd)
    'temp1 = SendMessage(mCapHwnd, Copy, 0, 0)

    ' Call capEditCopy(hCapWnd)
    ' picSenseMotion.Picture = Clipboard.GetData
    ' Clipboard.Clear

    Ri = 0 'right
    Wo = 0 'wrong

    ' LastTime = GetTickCount

    For i = 0 To 640 / inten - 1
    For j = 0 To 480 / inten - 1
    'get a point
    'c = picSenseMotion.Point(i * inten * Tppx, j * inten * Tppy)
    c = lpVHdr.lpData((i * inten * Tppx) * (j * inten * Tppy))


    'analyze it, Red, Green, Blue
    R = c Mod 256
    G = (c \ 256) Mod 256
    B = (c \ 256 \ 256) Mod 256

    'recall what the point was one step before this
    c2 = P(i, j)
    'analyze it
    R2 = c2 Mod 256
    G2 = (c2 \ 256) Mod 256
    B2 = (c2 \ 256 \ 256) Mod 256

    'main comparison part... if each R, G and B are somewhat same, then it pixel is same still
    'in a perfect camera and software tolerance should theoretically be 1 but this isnt true...
    If Abs(R - R2) < Tolerance And Abs(G - G2) < Tolerance And Abs(B - B2) < Tolerance Then
    'pixel remained same
    Ri = Ri + 1
    'Pon stores a boolean if the pixel changed or didnt, to be used to detect REAL movement
    POn(i, j) = True
    'WindowsMediaPlayer1.Visible = True


    Else
    'Pixel changed
    Wo = Wo + 1
    'make a red dor
    P(i, j) = lpVHdr.lpData
    '((i * inten * Tppx) * (j * inten * Tppy)) 'picSenseMotion.Point(i * inten * Tppx, j * inten * Tppy)
    'picSenseMotion.PSet (i * inten * Tppx, j * inten * Tppy), vbRed
    ' lpVHdr.lpData((i * inten * Tppx) * (j * inten * Tppy)) = vbRed
    'lpVHdr.lpData((i * inten * Tppx) * (j * inten * Tppy)) = vbRed
    POn(i, j) = False
    End If

    Next j

    Next i

    RealRi = 0

    For i = 1 To 640 / inten - 2
    For j = 1 To 480 / inten - 2
    If POn(i, j) = False Then
    'Real movement is simply occuring when all 4 pixels around one pixel changed
    'Simply put, If this pixel is changed and all around it changed too, then this is a real
    'movement
    If POn(i, j + 1) = False Then
    If POn(i, j - 1) = False Then
    If POn(i + 1, j) = False Then
    If POn(i - 1, j) = False Then
    RealRi = RealRi + 1
    'picSenseMotion.PSet (i * inten * Tppx, j * inten * Tppy), vbGreen
    'lpVHdr.lpData((i * inten * Tppx) * (j * inten * Tppy)) = vbGreen

    End If
    End If
    End If
    End If

    End If


    Next j
    Next i

    'state all statistics
    'Label1.Caption = Int(Wo / (Ri + Wo) * 100) & " % movement" & vbCrLf & "Real Movement: " & RealRi & vbCrLf _
    '& "Completed in: " & GetTickCount - LastTime
    Dim Movement As Long
    Movement = Int(Wo / (Ri + Wo) * 100)
    If Movement > fiMovement Then

    'mnuCapVid_Click
    'StopMotionDetection
    Else
    'tmrSenseMotion.Enabled = True
    End If

    End Function

  9. #9
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,287

    Re: Video capture

    All references to your picture box are commented out?

    Do you need the picturebox solely for analysis? Or to draw on as well (I noticed
    the PSet calls).

    I was going to suggest refreshing the picturebox frame-by-frame in a callback function or timer, but this will make drawing operations tricky.

    Likewise, if you hide the picturebox, you can still use it for image analysis, but you will not be able to draw on it.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Re: Video capture

    Thank you guys for your help, my motion detection is working now. I need to show time on the video that I am recording. I tried using GetDC it does show time but not on the video that is being recorded. It only show on the live video from the camera.
    Can anyone please help me with this.
    Bye

  11. #11
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Video capture

    Could you post your code for recording video? All the structures and stuff as well. I'm struggling with a similar app.

    Thank Pino

  12. #12
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,287

    Re: Video capture

    Quote Originally Posted by shoazib
    Thank you guys for your help, my motion detection is working now. I need to show time on the video that I am recording. I tried using GetDC it does show time but not on the video that is being recorded. It only show on the live video from the camera.
    Can anyone please help me with this.
    Bye
    There are 2 messages you need to look into: WM_CAP_SET_CALLBACK_FRAME and WM_CAP_SET_CALLBACK_VIDEOSTREAM. Both of these set up callback functions just prior to displaying a frame in the preview window (the former message) and writing a frame to disk (the latter message). So these callbacks are identical in functionality and structure, but differ in where they output to.

    The lpData member of the VIDEOHDR structure contains the bitmapped data bits of the frame. Now, you need to find a clever way to draw onto this. Right off the bat, I'm thinking a WM_CAP_GET_FORMAT or WM_CAP_GET_STATUS to get the dimensions and color depth (bits/pixel).

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    23

    Re: Video capture

    when i start my application using 2 webcams I get a dialog box asking to select video source. Is there a way I can get rid ot this ?

  14. #14
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,287

    Re: Video capture

    When does this happen? I.e. Which message is sent just before this dialog pops up?

    Are you sending the WM_CAP_DLG_VIDEOSOURCE message?

  15. #15
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Exclamation Re: Video capture

    when i start my application using 2 webcams I get a dialog box asking to select video source. Is there a way I can get rid ot this ?
    I know this thread hasn't been active in a while but I am currently working on a project displaying 2 webcams in separate capture windows at the same time. I get this same error as shoazib. I am using 2 logitech webcams and when I check the driver used using the capGetDriverDescriptionA function I found that both use the Microsoft WDM Image Capture Driver.

    The dialog box that pops up asks to select a device and shows both my webcams. This occurs after the first webcam has been connected. I think it selects the first one and then becomes unsure of what to do when asked to connect another device using the same driver. I would like to have both webcams connect without having the user go through this dialog box that automatically pops up. I want to specify the device, not have the user do it.

    I have done hours upon hours of research on the web, as well as playing around with code to try to figure this out but I have come up empty handed.

    I use capCreateCaptureWindowA to create a capture window and handle. Then use SendMessage(hwnd, WM_CAP_DRIVER_CONNECT, device_num, 0). device_num must be 0 so that it selects the WDM Image Capture driver. The dialog box shows up only after I try to connect to the driver again to get the 2nd webcam running.
    ex: SendMessage(hwnd2, WM_CAP_DRIVER_CONNECT, device_num, 0)

    There must be a way to talk to that driver to tell it which webcam I want to use? or a message that can be sent? please help

    I appreciate any info provided. Thanks.
    Last edited by pescar238; Nov 3rd, 2006 at 06:58 PM.

  16. #16
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Re: Video capture

    *bump*

    someone please help

  17. #17
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Video capture

    As far as I know only 1 video source can be connected with "WDM". Did you try to make "device_num=1" to get the other one. Take a look at my code it takes the video stream instead of the clipboard for accessing the data.
    Attached Files Attached Files

  18. #18
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Re: Video capture

    Thanks for the reply namrekka .

    To answer your question -- If "device_num" is set to 1, it won't connect to the camera at all. This is because the "device_num" variable actually represents which driver to connect to. Since I only have the WDM driver, it is set to 0.

    I have actually been able to connect with both of my webcams and record/take pictures from both at the same time. However, when I connect to them at the start of the program this annoying dialog box pops up asking me for the video source. What I want to be able to do is to connect to the device of my choice without having to go through this dialog box.

    One of the webcams gets automatically selected the first time I try to connect. On the second attempt to connect, so I can get the other webcam to display, is when the dialog box appears asking for the video source. I know this is because I am using the same driver, but how the heck can I specify the video source without a dialog box?

    Thanks for your input and the code example. The issue though is connecting to the driver and selecting the video source (and getting the right one ) so the user doesn't have to.
    Does your code take care of this, maybe I missed something?

    By the way, this is for live video in two separate capture windows using two separate webcams for the purpose of monitoring stuff.

    Anymore help and/or input is greatly appreciated!
    Last edited by pescar238; Nov 15th, 2006 at 11:22 AM.

  19. #19
    Junior Member gopi_vbboy's Avatar
    Join Date
    Nov 2006
    Location
    India
    Posts
    31

    Smile Re: Video capture

    videocapture interfaces in vb
    -----------------------

    Hey guys there is readymade with source code module at

    http://www.shrinkwrapvb.com/videocap.htm

    hope helps everyone
    -------------------

    gopi_vbboy
    rate me @ your cost
    Last edited by gopi_vbboy; Nov 15th, 2006 at 04:50 AM.

  20. #20
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Re: Video capture

    Hey guys there is readymade with source code module at

    http://www.shrinkwrapvb.com/videocap.htm

    hope helps everyone
    Thanks but this still doesn't take care of my problem with the driver and selecting of devices without a dialog box. I can capture video and do everything else without a problem.

  21. #21
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Video capture

    With "capGetDriverDescription" (0-9) you can walk trough a list of available video devices. It returns "True" if that index exist:
    http://msdn2.microsoft.com/en-gb/library/ms707201.aspx

    With that index you can open the selected video device:
    http://msdn2.microsoft.com/en-us/library/ms707174.aspx

    Did you try this?

  22. #22
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Re: Video capture

    I do use this and this is used this to find the available capture drivers, not devices. If you want you can try what I'm doing to see what I mean by the dialog box. Here is a simple example. 2 webcams should be connected via USB.

    Ex:

    hHwnd1 = capCreateCaptureWindowA(0, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, frmCarGUI.picCam1.Handle.ToInt32, 0)
    SendMessage(hHwnd1, WM_CAP_DRIVER_CONNECT, iDevice, 0)

    hHwnd2 = capCreateCaptureWindowA(0, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, frmCarGUI.picCam2.Handle.ToInt32, 0)
    SendMessage(hHwnd2, WM_CAP_DRIVER_CONNECT, iDevice, 0) <-- Here is where the dialog box appears.

    iDevice is a variable that come from locating drivers. Since I only have the WDM capture driver, iDevice = 0.

    If you give this code a try with the 2 webcams, let me know if you get the same problem as me. I'll try to put something simple together soon for a downloadable version of what I'm doing so I can post it.

  23. #23
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Video capture

    "iDevice" is the index of a driver found by "capGetDriverDescription". For the first driver it could be "0" for the second one this "Idevice" differs.
    Or do you have 2 the same devices shared with one driver? Try make "iDevice" = 1

  24. #24
    New Member
    Join Date
    Sep 2006
    Posts
    8

    Re: Video capture

    both devices are sharing the same driver. making iDevice = 1 doesn't do anything unless you have a different driver to work with. since I have only one driver then iDevice is 0. i tried making iDevice = 1 before and it doesn't connect with the device, mainly because making iDevice equal to 1 means I'd be trying to connect to a driver that isn't there.

  25. #25
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Video capture

    Ahhh.. I understand. You use 2 of the same devices. I tested with 2 different. Hmm..

  26. #26
    New Member
    Join Date
    Jul 2007
    Posts
    1

    Re: Video capture

    Hi friends,

    I´m using the codes you showed in this thread to capture a photo with a generic webcam my client has bought.

    The point is that I´m not able to define the size of the captured image. I´m not talking about fitting the image to the picturebox in wich I show it. Indeed it's done by the code I´ve got. What I need is to capture exactly what I see once the image saved is much bigger than the one who appears in the picture box.

    Can anyone help me? Thank you in advance. (Below, please find the code I´m using).


    To initialize the camera

    Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)

    On Error GoTo treatment:

    mHwnd = capCreateCaptureWindowA("Teste", WS_VISIBLE Or WS_CHILD, 0, 0, 100, 100, pbCtrl.hwnd, 0)

    If SendMessage(mHwnd, WM_CAP_DRIVER_CONNECT, 0, 0) Then
    SendMessage mHwnd, WM_CAP_SET_SCALE, False, 0
    '---set the preview rate (ms)---
    SendMessage mHwnd, WM_CAP_SET_PREVIEWRATE, 30, 0
    '---start previewing the image---
    SendMessage mHwnd, WM_CAP_SET_PREVIEW, True, 0
    '---resize window to fit in PictureBox control---
    SetWindowPos mHwnd, HWND_BOTTOM, 0, 0, pbCtrl.ScaleWidth, pbCtrl.ScaleHeight, SWP_NOMOVE Or SWP_NOZORDER

    Else
    '--error connecting to video source---
    MsgBox "Problema na conexão com a câmera. Contate o suporte!! - " & Err.Description
    DestroyWindow mHwnd
    End If

    Exit Sub

    treatment:

    MsgBox "Problema na conexão com a câmera. Contate suporte!!!" & Err.Description
    End Sub

    to capture the image

    Private Sub cmdCapturar_Click()

    SendMessage mHwnd, GET_FRAME, 0, 0
    SendMessage mHwnd, COPY, 0, 0
    Picture1.Picture = Clipboard.GetData
    Clipboard.Clear
    End Sub

  27. #27
    New Member
    Join Date
    Sep 2007
    Posts
    1

    Re: Video capture

    Looks like nobody solved the problem of pescar238...

    Does anyone know how to select the device (the camera) having only one available driver?

    Thanks in advance.

  28. #28
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: Video capture

    I have this problem as well. I have a tablet which has a front and rear camera. Even if I disable the front camera when I attemp to connect I get a dialog box showing only the rear camera. When I select this nothing happens. grrrrrrrrrrr.com

    Any help plz :-)

  29. #29
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Video capture

    Probably better to start a new thread than gravedig one this old.

    I would not be surprised if your "cameras" are really one camera device to the system. You'd have to use some method of switching imagers and it may be proprietary (requiring specialized device control operations) or only accessible through newer mechanisms like now-legacy WIA or the current WPD.

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

  30. #30
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: Video capture

    Thank you for your reply

  31. #31
    New Member
    Join Date
    Jan 2012
    Posts
    3

    Re: Video capture

    I have actually managed to do this by using the registry.
    It is a little ad-hoc and dodgy sometimes, but it is the best (and easiest) solution I have come up with.
    When you select a webcam in that dialogue a reference is kept in the registry. Notice that when you only use one webcam, the window will only show up once (when you connect the webcam for the first time) or never at all.
    Take a look in your registry at LocalMachine\\SYSTEM\\CurrentControlSet\\Control\\MediaResources\\msvideo\\MSVideo.VFWWDM.

    Code:
    //Get the ID for the webcam
    if (changeSource == false)
       DevicePath = LoadWebcamRegistry();
    else
       DevicePath = "";
    
    //In case of a new device (DevicePath == ""), ask user and save to registry
    //Clearing DevicePath causes DRIVER_CONNECT to show dialog and update registry
    SetWebcamRegistry(DevicePath);
    
    //Try to connect to driver
    if (window.SendMessage(WebcamLib.WM_CAP_DRIVER_CONNECT, IntPtr.Zero, IntPtr.Zero))
    {
       DevicePath = GetWebcamRegistry();
       SaveWebcamRegistry(DevicePath);
    }
    else
    {
       Disconnect();
    }
    The 'changeSource' is a parameter in the Connect function in my webcam class. When you want to change the source, you can simply put that variable on true and reconnect

    I'll leave editing the registry to you

  32. #32
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: Video capture

    Thanks Sade, I will take a look at that.

  33. #33
    New Member
    Join Date
    Jan 2012
    Posts
    3

    Re: Video capture

    Oh, in case you hadn't noticed...
    The sample I posted is C#, but I think you get the idea.
    If you get stuck, just let me know

  34. #34
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: Video capture

    Thanks mate; yes I did. I started out as a developer some 20 years ago using C so I should be able to manage.

    Thanks again.

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