Results 1 to 6 of 6

Thread: Problem with starting webcam

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Problem with starting webcam

    Hello experts
    I found this code that can launch the webcam and capture a picture.
    It is working fine when I execute the code for the first time.
    However to start the webcam again, I was obliged to restart windows.
    This is the code:
    Code:
    Public Const WS_CHILD As Long = &H40000000
    Public Const WS_VISIBLE As Long = &H10000000
    Public Const WM_USER As Long = &H400
    Public Const WM_CAP_START As Long = WM_USER
    Public Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
    Public Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
    Public Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
    Public Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
    Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
    Public Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25
    Code:
    Public Declare Function capCreateCaptureWindow _
        Lib "avicap32.dll" Alias "capCreateCaptureWindowA" _
             (ByVal lpszWindowName As String, ByVal dwStyle As Long _
            , ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long _
            , ByVal nHeight As Long, ByVal hwndParent As Long _
            , ByVal nID As Long) As Long
    
    Public Declare Function SendMessage Lib "user32" _
        Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long _
            , ByVal wParam As Long, ByRef lParam As Any) As Long
    Code:
    Private Sub Cmd3_Click()
    Dim temp As Long
    temp = SendMessage(hCap, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
    End Sub
    Code:
    Private Sub Cmd1_Click()
    hCap = capCreateCaptureWindow("Take a Camera Shot", WS_CHILD Or WS_VISIBLE, 0, 0, PicWebCam.Width, PicWebCam.Height, PicWebCam.hWnd, 0)
        If hCap <> 0 Then
            Call SendMessage(hCap, WM_CAP_DRIVER_CONNECT, 0, 0)
            Call SendMessage(hCap, WM_CAP_SET_PREVIEWRATE, 66, 0&)
            Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
        End If
    End Sub
    Code:
    Private Sub Cmd2_Click()
    Dim temp As Long
    temp = SendMessage(hCap, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
    End Sub
    This is what I get when I want to start the webcam for a second time

    Name:  111.jpg
Views: 619
Size:  24.3 KB

    Thank you

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

    Re: Problem with starting webcam

    i have tested your code here it would restart the webcam OK, i have tried it on windows 10 and XP

    just had to dimension hcap as module variable
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Re: Problem with starting webcam

    just had to dimension hcap as module variable
    I did so but still sometimes it works and most of the time I have to restart windows.

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

    Re: Problem with starting webcam

    i tried it multiple times

    you can download my sample from http://www.vbforums.com/showthread.p...ot-from-webcam
    see if it works any better or not

    did you try updating your webcam drivers?
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Re: Problem with starting webcam

    thanks sir for your interest
    I tried your sample and it works fine for the first time but when I wanted to restart again I got the message No webcam found.
    For updating the webcam drivers, I'll try that .

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Re: Problem with starting webcam

    I updated the driver but still the same problem.
    thanks

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