Strange webcam problem in windows 7

Just starting out? Need help? Post your questions and find answers here.
Fredi
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Jul 23, 2008 10:45 pm

Strange webcam problem in windows 7

Post by Fredi »

Existing PB webcam source working true in windows XP but when i run theys in windows 7 always opened "Video Source" window and say "Select a Video Device", like when webcam device is in use. But when i open another programs that work with webcam, showed webcam true without any problem. Strange part is sometimes for first time PB source work true but when i run that source again show above window again and not capture webcam again until i restart window.

This is an easy example that have this problem:

Code: Select all


If OpenWindow(0, 0, 0, 340, 260, "Video capture", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  If OpenLibrary(0, "avicap32.dll") 
    *capAddress = GetFunction(0, "capCreateCaptureWindowA") 
    hWndC = CallFunctionFast(*capAddress, 0, #WS_CHILD | #WS_VISIBLE, 10, 10, 320, 240, WindowID(0),1) 
    SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0) 
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0) 
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 15, 0) 
  EndIf 
EndIf 

Repeat 
  Event = WaitWindowEvent() 
Until Event = #PB_Event_CloseWindow 

SendMessage_(hWndC, #WM_CAP_STOP, 0, 0) 
SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0) 
DestroyWindow_(hWndC) 
CloseLibrary(0) 

End
What is the problem?
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Strange webcam problem in windows 7

Post by jesperbrannmark »

I have been stuggeling with webcams for five years now.
Last week I came up with this
(because in about 30% of times its just a black screen and noone seem to be talking about it)

Code: Select all

If OpenWindow(0, 0, 0, 340, 260, "Video capture", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  OpenLibrary(0, "avicap32.dll") 
  *capAddress = GetFunction(0, "capCreateCaptureWindowA") 
  hWndC = CallFunctionFast(*capAddress, 0, #WS_CHILD | #WS_VISIBLE, 10, 10, 320, 240, WindowID(0),1) 
  SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0) 
  SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0) 
  SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 15, 0) 
  ;noch ein mal...
  SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0)  
  DestroyWindow_(hWndC) 
  CloseLibrary(0) 
  OpenLibrary(0, "avicap32.dll") 
  *capAddress = GetFunction(0, "capCreateCaptureWindowA") 
  hWndC = CallFunctionFast(*capAddress, 0, #WS_CHILD | #WS_VISIBLE, 10, 10, 320, 240, WindowID(0),1) 
  SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0) 
  SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0) 
  SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 15, 0) 

EndIf 

Repeat 
  Event = WaitWindowEvent() 
Until Event = #PB_Event_CloseWindow 

;SendMessage_(hWndC, #WM_CAP_STOP, 0, 0) 
SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0) 
DestroyWindow_(hWndC) 
CloseLibrary(0) 

End
It seem to be working much better if you say it twice... try it. You can also try change
SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
to
SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 1, 0)
Fredi
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Jul 23, 2008 10:45 pm

Re: Strange webcam problem in windows 7

Post by Fredi »

I really got confused...
jesperbrannmark when i called :

Code: Select all

SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
one time that show "Video Source" window without showing webcam, but strange part is when i called that line twice, like:

Code: Select all

SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
First open "Video Source" window and after close that window then (in next try to connect) show webcam true. This happen for 80% of time and for 20% it show "Video Source" twice and can't connect to webcam anyway.
But why must an API don't work for first time but work if called twice !!??
This mean i every time want access to webcam must close "Video Source" first then work with webcam... and this is really uncommon for my program users !
Maybe we miss something in PB webcam source? Wrong API? or is an Windows bug?

Look at the capCreateCaptureWindow info in MSDN :
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
at bottom of page in "Community Additions" users say capCreateCaptureWindow doesn't work true at windows 7
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Strange webcam problem in windows 7

Post by jesperbrannmark »

Thats the thing. AVICAP is an awful beast.
I have tried this from several different programming languages - AVICAP always make you wanna cry.

Might wanna try a different method; DirectShow or WMI ?
(or switch to mac... hehe.. just had to say that there..)
Fredi
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Jul 23, 2008 10:45 pm

Re: Strange webcam problem in windows 7

Post by Fredi »

Yes, after some googling many users have exacly this problem in windows 7, and not exist any solution for that.
Exist another method to access webcam in windows 7 ?
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Strange webcam problem in windows 7

Post by jesperbrannmark »

You will get the same problems under AVICAP with vista and xp as well.... AVICAP is no fun.
Yes there are alternatives. I will see what I can find, maybe someone can fill me in here. Also a search in the forum should reveal some userlibs and dll's...
Fredi
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Jul 23, 2008 10:45 pm

Re: Strange webcam problem in windows 7

Post by Fredi »

I will see what I can find
Thank you jesperbrannmark, i'm looking for an good solution.
I found microsoft answer about this problem:
Since, VFW is a very old technology, it’s good to consider moving to DirectShow
But question is this, how to access webcam via DirectShow in PureBasic?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Strange webcam problem in windows 7

Post by wilbert »

Fredi wrote:Exist another method to access webcam in windows 7 ?
I don't know if it can be used but I came across this information about Microsoft Media Foundation
Microsoft Media Foundation was introduced in Windows Vista as the replacement for DirectShow. Of course, DirectShow is still supported in Windows 7, but developers are encouraged to use Media Foundation in their new digital media applications.

The improvements to Media Foundation can be summarized as follows:
  • Better format support, including MPEG-4
  • Support for capture devices and hardware codecs
  • A simplified programming model
  • Improvements to the platform
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Strange webcam problem in windows 7

Post by jesperbrannmark »

check out
http://www.purebasic.fr/english/viewtop ... 12&t=48212
and
http://purebasic.fr/english/viewtopic.php?f=7&t=25412

or maybe twain driver sometimes import from webcam in highres also if you like:

Code: Select all

If OpenLibrary(1, "EZTW32.DLL")    
  If CallFunction(1,"TWAIN_SelectImageSource",WindowID(GetActiveWindow())) ; choix du scan boite de dialogue      
    If CallFunction(1,"TWAIN_OpenDefaultSource")
      If CallFunction(1,"TWAIN_SetCurrentUnits",0) ; 0= DPI
        If CallFunction(1,"TWAIN_SetCurrentPixelType",1) ; fonction ok Scan format 0 = B&W, 1 Grey, 2 RGB
          If CallFunction(1,"TWAIN_SetBitDepth",24) ;Bit Depth 1, 2, 4, 8, 24 but depends on Pixeltype
            If CallFunction(1,"TWAIN_SetCurrentResolution",72) ; en point par pouce ppp (DPI)
            EndIf
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  *tst=AllocateMemory(100)
  PokeS(*tst,GetTemporaryDirectory()+"test.bmp")
  If FileSize(GetTemporaryDirectory()+"test.bmp")>-1
    DeleteFile(GetTemporaryDirectory()+"test.bmp")
  EndIf
  If FileSize(GetTemporaryDirectory()+"test.jpg")>-1
    DeleteFile(GetTemporaryDirectory()+"test.jpg")
  EndIf    
  CallFunction(1,"TWAIN_AcquireToFilename",WindowID(GetActiveWindow()),*tst)
  CloseLibrary(1)
  If FileSize(GetTemporaryDirectory()+"test.bmp")>-1
    LoadImage(0,GetTemporaryDirectory()+"test.bmp")
    SaveImage(0,GetTemporaryDirectory()+"scan.jpg",#PB_ImagePlugin_JPEG,6)
    FreeImage(0)
  EndIf
Else
  MessageRequester("","You need to install TWAIN driver.")
EndIf
but then you need http://www.eztwain.com/eztwain1.htm
Fredi
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Jul 23, 2008 10:45 pm

Re: Strange webcam problem in windows 7

Post by Fredi »

@wilbert
I never hear about it but sounds good if i know how it work in PB

@jesperbrannmark
Thank you for links and code, now i must work on theys until find best superseded for window VFW, But it was good if PureBasic have internal library for webcam :?
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Strange webcam problem in windows 7

Post by xxfreshman »

Do the init 2 times,

SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 1, 0)
SendMessage_(hWndC, #WM_CAP_SET_SCALE, #True, 0)

SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 10, 0)
SendMessage_(hWndC, #WM_CAP_SET_SCALE, #False, 0)


works perfekly with my computers.
Post Reply