Results 1 to 34 of 34

Thread: [RESOLVED] webcam can't continuous capture image after time selection change

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Resolved [RESOLVED] webcam can't continuous capture image after time selection change

    hi all,

    i have download a webcam program and modify with it. i want the webcam to continuous capture image in the time period i set in the combo box. my combo box contain 10, 20 and 30 seconds interval. at 1st, i select 10 seconds and click start. the webcam works well and can continuous capture image every 10 seconds. however, when i change to 20 seconds, the webcam no more capture any image. even i stop and start again the webcam also no function anymore. below is my coding, help plz...


    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Const WM_NCLBUTTONDOWN = &HA1
    Const HTCAPTION = 2
    
        Private m_TimeToCapture_milliseconds As Integer
        
        Private m_Width As Long
        Private m_Height As Long
        
        Private mCapHwnd As Long
       
        Private bStopped As Boolean
    
    Private Sub Combo1_Change()
    
    If Combo1.Text = 10 Then
            Call Timer2_Timer
            Exit Sub
        ElseIf Combo1.Text = 20 Then
            Call Timer3_Timer
            Exit Sub
        ElseIf Combo1.Text = 30 Then
            Call Timer4_Timer
            Exit Sub
        End If
        
    
    End Sub
    
    Private Sub Form_Load()
    On Error Resume Next
    Label1.BackStyle = 0 ' CheckBox
    Label4.BackStyle = 0 ' Form Caption
    Label5.BackStyle = 0 ' Form Close
    
        m_TimeToCapture_milliseconds = 100
        m_Width = 352
        m_Height = 288
        bStopped = True
        mCapHwnd = 0
        
    End Sub
    
    Public Sub Start()
        On Error Resume Next
        If mCapHwnd <> 0 Then Exit Sub
        FrameNum = 0
        
        Timer1.Interval = m_TimeToCapture_milliseconds
    
        ' for safety, call stop, just in case we are already running
        Me.Timer1.Enabled = False
    
        ' setup a capture window
        mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, Me.hwnd, 0)
        DoEvents
        
        ' connect to the capture device
        Call SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0)
        DoEvents
        
        Call SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0)
    
        ' set the timer information
        bStopped = False
        Me.Timer1.Enabled = True
            
    
    End Sub
        
    Public Sub StopWork()
        On Error Resume Next
        ' stop the timer
        bStopped = True
        Timer1.Enabled = False
    
        ' disconnect from the video source
        DoEvents
    
        Call SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0)
        mCapHwnd = 0
    
    End Sub
    
    
    Private Sub Label1_Click()
    On Error Resume Next
        Image2.Visible = Not Image2.Visible
        
        If Image2.Visible = True Then
            Image1.Width = 352
            Image1.Height = 288
            Image1.Stretch = True
        Else
            Image1.Stretch = False
        End If
        
    End Sub
    
    
    
    Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        On Error Resume Next
        Dim lngReturnValue As Long
        If Button = 1 Then
            'Release capture
            Call ReleaseCapture
            'Send a 'left mouse button down on caption'-message to our form
            lngReturnValue = SendMessage(Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
        End If
    End Sub
    
    Private Sub Label5_Click()
        ' From Close
        Call lvButtons_H6_Click
    End Sub
    
    Private Sub lvButtons_H1_Click()
    On Error Resume Next
      If mCapHwnd = 0 Then Exit Sub
    
        Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
        DoEvents
        
    End Sub
    
    Private Sub lvButtons_H2_Click()
    On Error Resume Next
        
        If mCapHwnd = 0 Then Exit Sub
    
        Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
        DoEvents
    
    End Sub
    
    Private Sub lvButtons_H3_Click()
      Start
      lvButtons_H1.Enabled = True
      lvButtons_H2.Enabled = True
      lvButtons_H4.Enabled = True
      lvButtons_H3.Enabled = False
      
    End Sub
    
    Private Sub lvButtons_H4_Click()
        StopWork
        lvButtons_H1.Enabled = False
        lvButtons_H2.Enabled = False
        lvButtons_H4.Enabled = False
        lvButtons_H3.Enabled = True
    End Sub
    
    Private Sub lvButtons_H5_Click()
    On Error Resume Next
    DoEvents
    If Dir(App.Path & "\myPic", vbDirectory) = "" Then MkDir (App.Path & "\myPic")
    File1.Path = App.Path & "\myPic"
    'File1.Pattern = "*.bmp"
    File1.Pattern = "*.jpg"
    File1.Refresh
    
    Dim Maxnum As Integer, ii As Integer
    For ii = 0 To File1.ListCount - 1
        If Left(File1.List(ii), 1) = "p" Then
            If CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4)) > Maxnum Then
                Maxnum = CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4))
            End If
        End If
    Next
    
        'SavePicture Image1.Picture, App.Path & "\myPic\p" & Maxnum + 1 & ".bmp"
        
        Picture1.Picture = Image1.Picture
        SAVEJPEG App.Path & "\myPic\p" & Maxnum + 1 & ".jpg", 100, Me.Picture1
      DoEvents
    End Sub
    
    Private Sub lvButtons_H6_Click()
     Timer1.Enabled = False
        If mCapHwnd <> 0 Then StopWork
        Unload Me
        End
    
    End Sub
    
    Private Sub Timer1_Timer()
    On Error Resume Next
    
        ' pause the timer
        Timer1.Enabled = False
    
        ' get the next frame;
        Call SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)
    
        ' copy the frame to the clipboard
        Call SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)
    
        ' For some reason, the API is not resizing the video
        ' feed to the width and height provided when the video
        ' feed was started, so we must resize the image here
        ' Image1.Stretch = True
                
        ' get from the clipboard
        Image1.Picture = Clipboard.GetData
             
             
        ' restart the timer
        DoEvents
        If Not bStopped Then
            Timer1.Enabled = True
        End If
    
    End Sub
    
    Private Sub Timer2_Timer()
    If Combo1.Text = 10 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
              Timer2.Enabled = True
              Call lvButtons_H5_Click
         Else
              Timer2.Enabled = False
         End If
    End Sub
    
    Private Sub Timer3_Timer()
    If Combo1.Text = 20 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
              Timer3.Enabled = True
              Call lvButtons_H5_Click
         Else
              Timer3.Enabled = False
         End If
    End Sub
    
    Private Sub Timer4_Timer()
    If Combo1.Text = 30 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
              Timer4.Enabled = True
              Call lvButtons_H5_Click
         Else
              Timer4.Enabled = False
         End If
    End Sub
    Last edited by si_the_geek; Dec 19th, 2008 at 10:33 AM. Reason: corrected Code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: webcam can't continuous capture image after time selection change

    I strongly recommend reading the article What is wrong with using "On Error Resume Next"? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

    There is a good chance that you are getting an error (ie: VB is trying to tell you what is wrong), but you are ignoring it.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    si_the_geek,

    thank for the advise. actually i have try many try and error case in this coding. however, i cant find out what is wrong. maybe i miss some coding? i not sure. so i post here for advance help...

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: webcam can't continuous capture image after time selection change

    It isn't too clear what you mean.. have you removed all of the "On Error Resume Next"? If not, you should - there is no need for it at all in your code.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    i have remove all "on error resume next". still have same problem. help...

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: webcam can't continuous capture image after time selection change

    Were there any errors raised? Have you tried running it with full compile?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    dee-u, what error do you mean? error during compile?

    FYI, i can compile perfectly and made an exe file of it. however, same problem still occur when i change the time selection during exe is running.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    only timer 1 does anything with the webcam, 2 3 & 4 do not

    there is no need for multiple timers, just change the interval to suit, also it is pretty hard to enable a timer from within its own event

    if you want someone to help you with this zip and post the whole project or a sample version
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    here i attach my webcam exe file for your testing and help.

    one more help, how to remove the arab word at the top of the frame?
    Attached Files Attached Files

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    anyone help on this matter?

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    the arab word is part of the picture on the from, as the picture is within the form code you can not remove it easily, you could make a new picture or cover over the word by changing the backstyle of label4, so it does not show through
    anyway you should design your own form, not just use someone elses

    i wrote the above about 5 hours ago, got distracted

    here is a solution to capturing the images to file based on the value in the combo box, only uses one timer, i increased the timer interval as it does not really work any faster even with shorter interval
    vb Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Private Declare Sub ReleaseCapture Lib "user32" ()
    3. Const WM_NCLBUTTONDOWN = &HA1
    4. Const HTCAPTION = 2
    5.  
    6.     Private m_TimeToCapture_milliseconds As Integer
    7.    
    8.     Private m_Width As Long
    9.     Private m_Height As Long
    10.    
    11.     Private mCapHwnd As Long
    12.    
    13.     Private bStopped As Boolean
    14.  
    15. Private Sub Combo1_Change()
    16. ''Timer1.Interval = Combo1.Text * 100
    17.  
    18. ''If Combo1.Text = 10 Then
    19. ''        Call Timer2_Timer
    20. ''        Exit Sub
    21. ''    ElseIf Combo1.Text = 20 Then
    22. ''        Call Timer3_Timer
    23. ''        Exit Sub
    24. ''    ElseIf Combo1.Text = 30 Then
    25. ''        Call Timer4_Timer
    26. ''        Exit Sub
    27. ''    End If
    28.    
    29.  
    30. End Sub
    31.  
    32. Private Sub Command1_Click()
    33. Shell "WebCam.exe", vbNormalFocus 'run my clone
    34. Unload Me 'let me go away
    35. End Sub
    36.  
    37. Private Sub Form_Load()
    38.  
    39. Label1.BackStyle = 0 ' CheckBox
    40. Label4.BackStyle = 0 ' Form Caption
    41. Label5.BackStyle = 0 ' Form Close
    42.  
    43.     m_TimeToCapture_milliseconds = 500
    44.     m_Width = 352
    45.     m_Height = 288
    46.     bStopped = True
    47.     mCapHwnd = 0
    48.  
    49. Combo1.ListIndex = 0
    50.  
    51. End Sub
    52.  
    53. Public Sub Start()
    54.  
    55.     If mCapHwnd <> 0 Then Exit Sub
    56.     FrameNum = 0
    57.    
    58.     Timer1.Interval = m_TimeToCapture_milliseconds
    59.  
    60.     ' for safety, call stop, just in case we are already running
    61.     Me.Timer1.Enabled = False
    62.  
    63.     ' setup a capture window
    64.     mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, Me.hwnd, 0)
    65.     DoEvents
    66.    
    67.     ' connect to the capture device
    68.     Call SendMessage(mCapHwnd, WM_CAP_CONNECT, 1, 0)
    69.     '******** change 1 back to 0
    70.     DoEvents
    71.    
    72.     Call SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0)
    73.  
    74.     ' set the timer information
    75.     bStopped = False
    76.     Me.Timer1.Enabled = True
    77.        
    78.  
    79. End Sub
    80.    
    81. Public Sub StopWork()
    82.  
    83.     ' stop the timer
    84.     bStopped = True
    85.     Timer1.Enabled = False
    86.  
    87.     ' disconnect from the video source
    88.     DoEvents
    89.  
    90.     Call SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0)
    91.     mCapHwnd = 0
    92.  
    93. End Sub
    94.  
    95.  
    96.  
    97.  
    98. Private Sub Label1_Click()
    99.  
    100.     Image2.Visible = Not Image2.Visible
    101.    
    102.     If Image2.Visible = True Then
    103.         Image1.Width = 352
    104.         Image1.Height = 288
    105.         Image1.Stretch = True
    106.     Else
    107.         Image1.Stretch = False
    108.     End If
    109.    
    110. End Sub
    111.  
    112.  
    113.  
    114. Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    115.  
    116.     Dim lngReturnValue As Long
    117.     If Button = 1 Then
    118.         'Release capture
    119.         Call ReleaseCapture
    120.         'Send a 'left mouse button down on caption'-message to our form
    121.         lngReturnValue = SendMessage(Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    122.     End If
    123. End Sub
    124.  
    125. Private Sub Label5_Click()
    126.     ' From Close
    127.     Call lvButtons_H6_Click
    128. End Sub
    129.  
    130. Private Sub lvButtons_H1_Click()
    131.  
    132.   If mCapHwnd = 0 Then Exit Sub
    133.  
    134.     Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
    135.     DoEvents
    136.    
    137. End Sub
    138.  
    139. Private Sub lvButtons_H2_Click()
    140.  
    141.    
    142.     If mCapHwnd = 0 Then Exit Sub
    143.  
    144.     Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
    145.     DoEvents
    146.  
    147. End Sub
    148.  
    149. Private Sub lvButtons_H3_Click()
    150.   Start
    151.   lvButtons_H1.Enabled = True
    152.   lvButtons_H2.Enabled = True
    153.   lvButtons_H4.Enabled = True
    154.   lvButtons_H3.Enabled = False
    155.  
    156. End Sub
    157.  
    158. Private Sub lvButtons_H4_Click()
    159.     StopWork
    160.     lvButtons_H1.Enabled = False
    161.     lvButtons_H2.Enabled = False
    162.     lvButtons_H4.Enabled = False
    163.     lvButtons_H3.Enabled = True
    164. End Sub
    165.  
    166. Private Sub lvButtons_H5_Click()
    167. savepic
    168. End Sub
    169.  
    170. Private Sub savepic()
    171.  
    172. DoEvents
    173. If Dir(App.Path & "\myPic", vbDirectory) = "" Then MkDir (App.Path & "\myPic")
    174. File1.Path = App.Path & "\myPic"
    175. 'File1.Pattern = "*.bmp"
    176. File1.Pattern = "*.jpg"
    177. File1.Refresh
    178.  
    179. Dim Maxnum As Integer, ii As Integer
    180. For ii = 0 To File1.ListCount - 1
    181.     If Left(File1.List(ii), 1) = "p" Then
    182.         If CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4)) > Maxnum Then
    183.             Maxnum = CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4))
    184.         End If
    185.     End If
    186. Next
    187.  
    188.     'SavePicture Image1.Picture, App.Path & "\myPic\p" & Maxnum + 1 & ".bmp"
    189.    
    190.     Picture1.Picture = Image1.Picture
    191.     SAVEJPEG App.Path & "\myPic\p" & Maxnum + 1 & ".jpg", 100, Me.Picture1
    192.   DoEvents
    193. End Sub
    194.  
    195. Private Sub lvButtons_H6_Click()
    196.  Timer1.Enabled = False
    197.     If mCapHwnd <> 0 Then StopWork
    198.     Unload Me
    199.     End
    200.  
    201. End Sub
    202.  
    203. Private Sub Timer1_Timer()
    204. Static cnt As Integer
    205.     cnt = cnt + 1
    206.     ' pause the timer
    207.     Timer1.Enabled = False
    208.  
    209.     ' get the next frame;
    210.     Call SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)
    211.  
    212.     ' copy the frame to the clipboard
    213.     Call SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)
    214.  
    215.     ' For some reason, the API is not resizing the video
    216.     ' feed to the width and height provided when the video
    217.     ' feed was started, so the image was resize here
    218.     ' Image1.Stretch = True
    219.            
    220.     ' get from the clipboard
    221.     Image1.Picture = Clipboard.GetData
    222.          
    223.          
    224.     ' restart the timer
    225.     DoEvents
    226.     If cnt > Combo1.Text And Not Combo1.Text = 0 Then
    227. '    Debug.Print Timer
    228.         savepic
    229.         cnt = 0
    230.     End If
    231.     If Not bStopped Then
    232.         Timer1.Enabled = True
    233.     End If
    234.  
    235. End Sub
    236.  
    237. '''Private Sub Timer2_Timer()
    238. '''If Combo1.Text = 10 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
    239. '''          Timer2.Enabled = True
    240. '''          Call lvButtons_H5_Click
    241. '''     Else
    242. '''          Timer2.Enabled = False
    243. '''     End If
    244. '''End Sub
    245. '''
    246. '''Private Sub Timer3_Timer()
    247. '''If Combo1.Text = 20 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
    248. '''          Timer3.Enabled = True
    249. '''          Call lvButtons_H5_Click
    250. '''     Else
    251. '''          Timer3.Enabled = False
    252. '''     End If
    253. '''End Sub
    254. '''
    255. '''Private Sub Timer4_Timer()
    256. '''If Combo1.Text = 30 And lvButtons_H3.Enabled = False And lvButtons_H4.Enabled = True Then
    257. '''          Timer4.Enabled = True
    258. '''          Call lvButtons_H5_Click
    259. '''     Else
    260. '''          Timer4.Enabled = False
    261. '''     End If
    262. '''End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    westconn, thanks a lot. yeah. it work. however, for the timing period, when i select 10 seconds, but it capture image after 5 seconds. same situation happen for other selection. time capture image reduced half. i hv try double the timer interval for timer 2, timer 3, & timer 4 but it doesnt work.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    don't change timer interval, change
    this is about the most accurate i could get, tested with 10 and 20 second
    vb Code:
    1. Private Sub Timer1_Timer()
    2. Static lasttime As Single, nexttime As Single
    3.     cnt = cnt + 1
    4.     ' pause the timer
    5. '    Timer1.Enabled = False
    6.  
    7.     ' get the next frame;
    8.     Call SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)
    9.  
    10.     ' copy the frame to the clipboard
    11.     Call SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)
    12.  
    13.     ' For some reason, the API is not resizing the video
    14.     ' feed to the width and height provided when the video
    15.     ' feed was started, so the image was resize here
    16.     ' Image1.Stretch = True
    17.            
    18.     ' get from the clipboard
    19.     Image1.Picture = Clipboard.GetData
    20.          
    21.          
    22.     ' restart the timer
    23. '    DoEvents
    24.     If nexttime <= Timer Then
    25.     lasttime = Timer
    26.     nexttime = lasttime + Combo1.Text - 1 'allow processing time in timer event
    27.     'Debug.Print lasttime, nexttime
    28.     savepic
    29.     End If
    30.     If Not bStopped Then
    31. '        Timer1.Enabled = True
    32.     End If
    33.  
    34. End Sub
    i am not using any of the timers except timer 1, you may have noticed i commented out all there code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    westconn, nearly 1second is missed. however, when i change the code
    nexttime = lasttime + Combo1.Text - 1 'allow processing time in timer event
    to
    nexttime = lasttime + Combo1.Text 'allow processing time in timer event
    i can get accurate time. so what is the -1 important in the coding? however, i hv found another problem. when the time in combo box is 0 second and i click start, the camera will continue capturing image. how to modify it so that at 0 second and start button is click, no capturing operation is performed?

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    If nexttime <= Timer and not combo1.text = 0 Then
    should stop it capturing files when set to 0, but the image will still update on the form display

    the timer function was not accurate on my computer always seemed to be 1 second slow, with the -1 made it nearly correct, feel free to adjust whichever value suits you best
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    If nexttime <= Timer and not combo1.text = 0
    can stop it capturing files when set to 0, but when set to 10, 20 or 30 it continuously capture image without depend on the time. (nearly 10++ image captured in 1 second)
    Last edited by mic_k86; Jan 4th, 2009 at 01:33 AM.

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    i just tested the program again, the images saved very close to the correct timing p2 to p8, 5 seconds under 1 minute

    if you still have a problem post your form (or the code) again
    Attached Images Attached Images  
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Const WM_NCLBUTTONDOWN = &HA1
    Const HTCAPTION = 2
    
        Private m_TimeToCapture_milliseconds As Integer
        
        Private m_Width As Long
        Private m_Height As Long
        
        Private mCapHwnd As Long
       
        Private bStopped As Boolean
    
    
    
    Private Sub Command1_Click()
    Shell "WebCam.exe", vbNormalFocus 'run my clone
    Unload Me 'let me go away
    End Sub
    
    Private Sub Form_Load()
    
    Label1.BackStyle = 0 ' CheckBox
    Label4.BackStyle = 0 ' Form Caption
    Label5.BackStyle = 0 ' Form Close
    
        m_TimeToCapture_milliseconds = 500
        m_Width = 352
        m_Height = 288
        bStopped = True
        mCapHwnd = 0
    
    Combo1.ListIndex = 0
    
    End Sub
    
    Public Sub Start()
    
        If mCapHwnd <> 0 Then Exit Sub
        FrameNum = 0
        
        Timer1.Interval = m_TimeToCapture_milliseconds
    
        ' for safety, call stop, just in case we are already running
        Me.Timer1.Enabled = False
    
        ' setup a capture window
        mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, Me.hwnd, 0)
        DoEvents
        
        ' connect to the capture device
        Call SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0)
        DoEvents
        
        Call SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0)
    
        ' set the timer information
        bStopped = False
        Me.Timer1.Enabled = True
            
    
    End Sub
        
    Public Sub StopWork()
    
        ' stop the timer
        bStopped = True
        Timer1.Enabled = False
    
        ' disconnect from the video source
        DoEvents
    
        Call SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0)
        mCapHwnd = 0
    
    End Sub
    
    Private Sub Label1_Click()
    
        Image2.Visible = Not Image2.Visible
        
        If Image2.Visible = True Then
            Image1.Width = 352
            Image1.Height = 288
            Image1.Stretch = True
        Else
            Image1.Stretch = False
        End If
        
    End Sub
    
    Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
        Dim lngReturnValue As Long
        If Button = 1 Then
            'Release capture
            Call ReleaseCapture
            'Send a 'left mouse button down on caption'-message to our form
            lngReturnValue = SendMessage(Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
        End If
    End Sub
    
    Private Sub Label5_Click()
        ' From Close
        Call lvButtons_H6_Click
    End Sub
    
    Private Sub lvButtons_H1_Click()
    
      If mCapHwnd = 0 Then Exit Sub
    
        Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
        DoEvents
        
    End Sub
    
    Private Sub lvButtons_H2_Click()
    
        
        If mCapHwnd = 0 Then Exit Sub
    
        Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
        DoEvents
    
    End Sub
    
    Private Sub lvButtons_H3_Click()
      Start
      lvButtons_H1.Enabled = True
      lvButtons_H2.Enabled = True
      lvButtons_H4.Enabled = True
      lvButtons_H3.Enabled = False
      
    End Sub
    
    Private Sub lvButtons_H4_Click()
        StopWork
        lvButtons_H1.Enabled = False
        lvButtons_H2.Enabled = False
        lvButtons_H4.Enabled = False
        lvButtons_H3.Enabled = True
    End Sub
    
    Private Sub lvButtons_H5_Click()
    savepic
    End Sub
    
    Private Sub savepic()
    
    DoEvents
    If Dir(App.Path & "\myPic", vbDirectory) = "" Then MkDir (App.Path & "\myPic")
    File1.Path = App.Path & "\myPic"
    'File1.Pattern = "*.bmp"
    File1.Pattern = "*.jpg"
    File1.Refresh
    
    Dim Maxnum As Integer, ii As Integer
    For ii = 0 To File1.ListCount - 1
        If Left(File1.List(ii), 1) = "p" Then
            If CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4)) > Maxnum Then
                Maxnum = CInt(Mid(File1.List(ii), 2, Len(File1.List(ii)) - 4))
            End If
        End If
    Next
    
        'SavePicture Image1.Picture, App.Path & "\myPic\p" & Maxnum + 1 & ".bmp"
        
        Picture1.Picture = Image1.Picture
        SAVEJPEG App.Path & "\myPic\p" & Maxnum + 1 & ".jpg", 100, Me.Picture1
      DoEvents
    End Sub
    
    Private Sub lvButtons_H6_Click()
     Timer1.Enabled = False
        If mCapHwnd <> 0 Then StopWork
        Unload Me
        End
    
    End Sub
    
    Private Sub Timer1_Timer()
    
        Static cnt As Integer
        cnt = cnt + 1
    
        ' get the next frame;
        Call SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)
    
        ' copy the frame to the clipboard
        Call SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)
    
        ' For some reason, the API is not resizing the video
        ' feed to the width and height provided when the video
        ' feed was started, so the image was resize here
        ' Image1.Stretch = True
                
        ' get from the clipboard
        Image1.Picture = Clipboard.GetData
             
             
        ' restart the timer
        DoEvents
         If nexttime <= Timer And Not Combo1.Text = 0 Then
         lasttime = Timer
         nexttime = lasttime + Combo1.Text 'allow processing time in timer event
         'Debug.Print lasttime, nexttime
        savepic
            
        End If
    
    End Sub
    here is my current code

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    change the declared variables
    Private Sub Timer1_Timer()
    Static lasttime As Single, nexttime As Single


    other than that i cannot see any difference to what i am using
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    westconn, thanks a lot. ya, i miss to change that line of code. now i think the program is work smooth already. then i plan to made a command button so can direct access the myPic folder from vb. below is my code
    Code:
    Option Explicit
    
    Private Const CSIDL_DESKTOP = &H0
    Private Const NOERROR = 0
    
    Private Type SHTEMID
        cb As Long
        abID As Byte
    End Type
    Private Type ITEMIDLIST
        mkid As SHTEMID
    End Type
    
    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Dim strDesktop As String
    
    Private Sub Command2_Click()
    
    strDesktop = GetSpecialfolder(CSIDL_DESKTOP)
    ShellExecute Me.hwnd, "open", strDesktop & "\webcam\myPic", vbNullString, strDesktop & "\webcam\myPic", SW_SHOWNORMAL
    
    End Sub
    
    
    Private Sub Form_Load()
    Debug.Print "Desktop path: " & GetSpecialfolder(CSIDL_DESKTOP)
    End Sub
    
    Private Function GetSpecialfolder(CSIDL As Long) As String
    Dim r As Long
    Dim IDL As ITEMIDLIST
    Dim Path$
    
        'Get the special folder
        r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
            If r = NOERROR Then
                'Create a buffer
                Path$ = Space$(512)
                'Get the path from the IDList
                r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
                'Remove the unnecessary chr$(0)'s
                GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
                Exit Function
        End If
        GetSpecialfolder = ""
    End Function
    which is study from my post previously in this thread http://www.vbforums.com/showthread.php?t=550827 However, when i execute it, error variable not found and highlighted on cnt = cnt + 1. what wrong and how to correct it?

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    that line is not required it was a left over from my previous attempt
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    after i delete that line of code still got another error on this line of code
    FrameNum = 0
    at public sub start (). i try delete this code and it work fine. what is important of this code?

  23. #23
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: webcam can't continuous capture image after time selection change

    Did you ever declare FrameNum and used it anywhere? If not then just remove it.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  24. #24
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    it would appear to me that you added option explicit to the top of your form, after you had been testing the code already, any variables that were undeclared, would now have to be declared, preferably with the correct type, or an error will occur, even though no error was generated before adding option explicit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    so how to declare the FremeNum? or just delete it?

  26. #26
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: webcam can't continuous capture image after time selection change

    judging by the name framenum i guess as integer

    don't delete first, just comment out in case you still need it, or do a search to see where else it occurs
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    yea. i declare and no more error occur. is it need to declare it as public or private? i got try two types and all work fines.

  28. #28
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: webcam can't continuous capture image after time selection change

    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: webcam can't continuous capture image after time selection change

    thank everyone especially westconn1, i hv learn a lot about webcam program in vb.

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: [RESOLVED] webcam can't continuous capture image after time selection change

    hi all, i hv some problem here with my coding. this coding is work fine in my pc. but when i using monitoring software (tightVNC, which i installed my webcam my vb exe in one pc and monitor using another pc), i found following error
    Run-time error 521 - Cannot open clipboard"
    . i got try debug the coding by putting clipboard.clear but still have same problem. help...

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: [RESOLVED] webcam can't continuous capture image after time selection change

    any help?

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: [RESOLVED] webcam can't continuous capture image after time selection change

    i need help here. urgent. anyone help me modify the code? (without using of clipboard)

  33. #33
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] webcam can't continuous capture image after time selection change

    you can try to modify this code as it does not use the clipboard (or at least not in the same way), it captures a frame from the webcam to a picturebox
    vb Code:
    1. Option Explicit
    2.  
    3. Public Const WS_CHILD As Long = &H40000000
    4. Public Const WS_VISIBLE As Long = &H10000000
    5.  
    6. Public Type POINT
    7. x As Long
    8. y As Long
    9. End Type
    10.  
    11. Public Type CAPSTATUS
    12. uiImageWidth As Long
    13. uiImageHeight As Long
    14. fLiveWindow As Long
    15. fOverlayWindow As Long
    16. fScale As Long
    17. ptScroll As POINT
    18. fUsingDefaultPalette As Long
    19. fAudioHardware As Long
    20. fCapFileExists As Long
    21. dwCurrentVideoFrame As Long
    22. dwCurrentVideoFramesDropped As Long
    23. dwCurrentWaveSamples As Long
    24. dwCurrentTimeElapsedMS As Long
    25. hPalCurrent As Long
    26. fCapturingNow As Long
    27. dwReturn As Long
    28. wNumVideoAllocated As Long
    29. wNumAudioAllocated As Long
    30. End Type
    31.  
    32. Public Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" ( _
    33. ByVal lpszWindowName As String, _
    34. ByVal dwStyle As Long, _
    35. ByVal x As Long, _
    36. ByVal y As Long, _
    37. ByVal nWidth As Long, _
    38. ByVal nHeight As Long, _
    39. ByVal hWndParent As Long, _
    40. ByVal nID As Long) As Long
    41.  
    42. Public Declare Function GetDC Lib "user32.dll" ( _
    43. ByVal hWnd As Long) As Long
    44.  
    45. Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
    46. ByVal hdc As Long, _
    47. ByVal nWidth As Long, _
    48. ByVal nHeight As Long) As Long
    49.  
    50. Public Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _
    51. ByVal hdc As Long) As Long
    52.  
    53. Public Declare Function SelectObject Lib "gdi32.dll" ( _
    54. ByVal hdc As Long, _
    55. ByVal hObject As Long) As Long
    56.  
    57. Public Declare Function BitBlt Lib "gdi32.dll" ( _
    58. ByVal hDestDC As Long, _
    59. ByVal x As Long, _
    60. ByVal y As Long, _
    61. ByVal nWidth As Long, _
    62. ByVal nHeight As Long, _
    63. ByVal hSrcDC As Long, _
    64. ByVal xSrc As Long, _
    65. ByVal ySrc As Long, _
    66. ByVal dwRop As Long) As Long
    67.  
    68. Public Declare Function DeleteDC Lib "gdi32.dll" ( _
    69. ByVal hdc As Long) As Long
    70.  
    71.  
    72. Public hCapWin As Long
    73. Public w As Long
    74. Public h As Long
    75.  
    76. Public Sub CapFrame(i As Long)
    77. Dim hCapWinDC As Long
    78. Dim hBitmap As Long
    79. Dim hdc As Long
    80. Dim hOldBMP As Long
    81. Dim r As Long, j As Long
    82. Form2.Hide
    83. hCapWinDC = GetDC(hCapWin)
    84. hBitmap = CreateCompatibleBitmap(hCapWinDC, w, h)
    85. hdc = CreateCompatibleDC(hCapWinDC)
    86. hOldBMP = SelectObject(hdc, hBitmap)
    87.  
    88. r = BitBlt(hdc, 0, 0, w, h, hCapWinDC, 0, 0, vbSrcCopy)
    89. (Form2.Picture1(0).Width + 200), Form2.Picture1(0).Top + i Mod 3 * (Form2.Picture1(0).Height + 200), w * Screen.TwipsPerPixelX, h * Screen.TwipsPerPixelY
    90. Form1.Picture1.Move Form1.Picture1.Left, Form1.Picture1.Top, Form1.ScaleWidth, Form1.ScaleHeight
    91. r = BitBlt(Form1.Picture1.hdc, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, hdc, 0, 0, vbSrcCopy)
    92. Form1.Picture1.Refresh
    93. Form2.Picture1(i).Picture = Form1.Picture1.Image
    94. r = SelectObject(hdc, hOldBMP)
    95. r = DeleteDC(hdc)
    96. End Sub

    the whole project is in a thread by me in codebank vb6, i have this in a module, but you could change the declares and put in your form, in this case hcapwin was on form1 and the picture boxes on form 2
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  34. #34

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: [RESOLVED] webcam can't continuous capture image after time selection change

    westconn1, i hv download the vb in the codebank you upload few day ago. but until now i dont have idea how to change my coding part which using clipboard to your coding using DIB. seem need have a major change in the coding. can you help me? i upload here my latest coding which is still using the clipboard part. hope can help...
    Attached Files Attached Files

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