usb camera

Just starting out? Need help? Post your questions and find answers here.
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

usb camera

Post by ludoke »

I will use an usb webcam,i try severall examples ,but where I find "escapi.pbi".
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: usb camera

Post by infratec »

Hi,

Code: Select all

;
; Extremely Simple Capture API
; http://sol.gfxile.net/escapi/
;
; http://sol.gfxile.net/zip/escapi3.zip
;

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Enumeration CAPTURE_PROPETIES
  #CAPTURE_BRIGHTNESS
  #CAPTURE_CONTRAST
  #CAPTURE_HUE
  #CAPTURE_SATURATION
  #CAPTURE_SHARPNESS
  #CAPTURE_GAMMA
  #CAPTURE_COLORENABLE
  #CAPTURE_WHITEBALANCE
  #CAPTURE_BACKLIGHTCOMPENSATION
  #CAPTURE_GAIN
  #CAPTURE_PAN
  #CAPTURE_TILT
  #CAPTURE_ROLL
  #CAPTURE_ZOOM
  #CAPTURE_EXPOSURE
  #CAPTURE_IRIS
  #CAPTURE_FOCUS
  #CAPTURE_PROP_MAX
EndEnumeration


Structure escapi_SimpleCapParams
  *mTargetBuf  ; TargetBuffer. Must be at least mWidth * mHeight * SizeOf(int) of size!
  mWidth.l     ; Buffer width
  mHeight.l    ; Buffer height
EndStructure



; Return the number of capture devices found
PrototypeC.l countCaptureDevicesProc()

; initCapture tries To open the video capture device.
; Returns 0 on failure, 1 on success.
; Note: Capture parameter values must Not change While capture device
;       is in use (i.e. between initCapture And deinitCapture).
;       Do *Not* free the target buffer, Or change its pointer!
; 
PrototypeC.l initCaptureProc(deviceno.l, *aParams.escapi_SimpleCapParams)

; deinitCapture closes the video capture device.
PrototypeC deinitCaptureProc(deviceno.l)

; doCapture requests video frame To be captured.
PrototypeC doCaptureProc(deviceno.l)

; isCaptureDone returns 1 when the requested frame has been captured.
PrototypeC.l isCaptureDoneProc(deviceno.l)

; Get the user-friendly name of a capture device.
PrototypeC getCaptureDeviceNameProc(deviceno.l, *namebuffer, bufferlength.l)

; Returns the ESCAPI DLL version. 0x200 For 2.0
PrototypeC.l ESCAPIDLLVersionProc()

;Gets value (0..1) of a camera property (see CAPTURE_PROPERTIES, above)
PrototypeC.f getCapturePropertyValueProc(deviceno.l, prop.l)

; Gets whether the property is set to automatic (see CAPTURE_PROPERTIES, above)
PrototypeC.l getCapturePropertyAutoProc(deviceno.l, prop.l)

; Set camera property To a value (0..1) And whether it should be set To auto.
PrototypeC.l setCapturePropertyProc(deviceno.l, prop.l, value.f, autoval.l)

; Return line number of error, Or 0 If no catastrophic error has occurred.
PrototypeC.l getCaptureErrorLineProc(deviceno.l)

; Return HRESULT of the catastrophic error, Or 0 If none.
PrototypeC.l getCaptureErrorCodeProc(deviceno.l)

; marked as "internal" in the example
PrototypeC initCOMProc()


Global escapi_dll.i


Global escapi_countCaptureDevices.countCaptureDevicesProc
Global escapi_initCapture.initCaptureProc
Global escapi_deinitCapture.deinitCaptureProc
Global escapi_doCapture.doCaptureProc
Global escapi_isCaptureDone.isCaptureDoneProc
Global escapi_getCaptureDeviceName.getCaptureDeviceNameProc
Global escapi_ESCAPIVersion.ESCAPIDLLVersionProc
Global escapi_getCapturePropertyValue.getCapturePropertyValueProc
Global escapi_getCapturePropertyAuto.getCapturePropertyAutoProc
Global escapi_setCaptureProperty.setCapturePropertyProc
Global escapi_getCaptureErrorLine.getCaptureErrorLineProc
Global escapi_getCaptureErrorCode.getCaptureErrorCodeProc

Global escapi_initCOM.initCOMProc


Procedure.i escapi_setup()
  
  If IsLibrary(escapi_dll)
    ProcedureReturn escapi_countCaptureDevices()
  EndIf
  
  ; load library
  escapi_dll = OpenLibrary(#PB_Any, "escapi.dll")
  If escapi_dll = 0
    ProcedureReturn #False
  EndIf
  
  ; Fetch function entry points
  escapi_countCaptureDevices     = GetFunction(escapi_dll, "countCaptureDevices")
  escapi_initCapture             = GetFunction(escapi_dll, "initCapture")
  escapi_deinitCapture           = GetFunction(escapi_dll, "deinitCapture")
  escapi_doCapture               = GetFunction(escapi_dll, "doCapture")
  escapi_isCaptureDone           = GetFunction(escapi_dll, "isCaptureDone")
  escapi_getCaptureDeviceName    = GetFunction(escapi_dll, "getCaptureDeviceName")
  escapi_ESCAPIVersion           = GetFunction(escapi_dll, "ESCAPIDLLVersion")
  escapi_getCapturePropertyValue = GetFunction(escapi_dll, "getCapturePropertyValue")
  escapi_getCapturePropertyAuto  = GetFunction(escapi_dll, "getCapturePropertyAuto")
  escapi_setCaptureProperty      = GetFunction(escapi_dll, "setCaptureProperty")
  escapi_getCaptureErrorLine     = GetFunction(escapi_dll, "getCaptureErrorLine")
  escapi_getCaptureErrorCode     = GetFunction(escapi_dll, "getCaptureErrorCode")
  
  escapi_initCOM.initCOMProc     = GetFunction(escapi_dll, "initCOM")
  
  If escapi_countCaptureDevices = 0 Or
     escapi_initCapture = 0 Or
     escapi_deinitCapture = 0 Or
     escapi_doCapture = 0 Or
     escapi_isCaptureDone = 0 Or
     escapi_initCOM = 0 Or
     escapi_getCaptureDeviceName = 0 Or
     escapi_ESCAPIVersion = 0 Or
     escapi_getCapturePropertyValue = 0 Or
     escapi_getCapturePropertyAuto = 0 Or
     escapi_setCaptureProperty = 0 Or
     escapi_getCaptureErrorLine = 0 Or
     escapi_getCaptureErrorCode = 0 Or
     escapi_initCOM = 0
    
    CloseLibrary(escapi_dll)
    
    ProcedureReturn #False
  EndIf
  
  ; Verify DLL version
  If escapi_ESCAPIVersion() < $200
    
    CloseLibrary(escapi_dll)
    
    ProcedureReturn #False
  EndIf
  
  ; Initialize COM...
  ;escapi_initCOM()
  
  ; returns number of devices found
  ProcedureReturn escapi_countCaptureDevices()
  
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
  
  Define Count.i, i.i, *Buffer, scp.escapi_SimpleCapParams, image.i, Exit.i, DeviceName$, Event.i
  Define StartTime.i, EndTime.i, device.i, mHeight.i, mWidth.i, x.i, y.i, Offset.i, PixelRGB.i, PixelBGR.i
  
  
  Count = escapi_setup()
  If Count
    
    *Buffer = AllocateMemory(1024)
    If *Buffer
      
      device = 0
      
      For i = 0 To Count - 1
        escapi_getCaptureDeviceName(i, *Buffer, 1024)
        If i = device
          DeviceName$ = PeekS(*Buffer, -1, #PB_Ascii)
        EndIf
        ;Debug PeekS(*Buffer, -1, #PB_Ascii)
      Next i
      FreeMemory(*Buffer)
      
      scp\mWidth = 640
      scp\mHeight = 480
      scp\mTargetBuf = AllocateMemory(scp\mWidth * scp\mHeight * 4)
      If scp\mTargetBuf
        
        If escapi_initCapture(device, @scp)
          
          image = CreateImage(#PB_Any, 640, 480)
          
          OpenWindow(0, 0, 0, 640, 480, DeviceName$, #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_Invisible)
          
          ImageGadget(0, 0, 0, 640, 480, ImageID(image))
          
          CreateStatusBar(0, WindowID(0))
          AddStatusBarField(20)
          
          ResizeWindow(0, #PB_Ignore, #PB_Ignore, #PB_Ignore, WindowHeight(0) + StatusBarHeight(0))
          
          HideWindow(0, #False)
          
          Repeat
            
            Event = WindowEvent()
            
            escapi_doCapture(device)
            While escapi_isCaptureDone(device) = 0
              If WaitWindowEvent(1) = #PB_Event_CloseWindow
                Exit = #True
                Break
              EndIf
            Wend
            
            If StartDrawing(ImageOutput(image))
              StartTime = ElapsedMilliseconds()
              mHeight = scp\mHeight - 1
              mWidth = scp\mWidth - 1
              For y = 0 To mHeight
                Offset = y * scp\mWidth
                For x = 0 To mWidth
                  PixelRGB = PeekL(scp\mTargetBuf + (Offset + x) << 2)
                  PixelBGR = (PixelRGB << 16) | (PixelRGB & $00FF00) | ((PixelRGB >> 16) & $FF)
                  Plot(x, y, PixelBGR)
                Next
              Next
              EndTime = ElapsedMilliseconds()
              
              StopDrawing()
              SetGadgetState(0, ImageID(image))
              
              StatusBarText(0, 0, Str(EndTime - StartTime))
            EndIf
            
            If Event = #PB_Event_CloseWindow
              Exit = #True
            EndIf
            
          Until Exit
          
          escapi_deinitCapture(device)
          
        Else
          MessageRequester("escapi", "Was not able to init the device")
        EndIf
        
        
        FreeMemory(scp\mTargetBuf)
      EndIf
      
    EndIf
  Else
    MessageRequester("escapi", "Was not able to load the escapi.dll")
  EndIf
  
CompilerEndIf
You need escapi 3 dll with a size of 94208 bytes. (follow the 2nd link in the code)
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: usb camera

Post by TI-994A »

ludoke wrote:I will use an usb webcam...
Please give this one a try as well. Original code by @Sparkie from twelve years ago!

Code: Select all

cwFlags = #WS_CHILD | #WS_VISIBLE
wFlags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
If OpenWindow(0, 0, 0, 600, 400, "Webcam Example", wFlags)
  If OpenLibrary(0, "avicap32.dll")
    *capAddress = GetFunction(0, "capCreateCaptureWindowA")
    If *capAddress 
      hWnd = CallFunctionFast(*capAddress, 0, cwFlags, 
                              0, 0, 600, 400, WindowID(0), 0)
      If hWnd
        If SendMessage_(hWnd, #WM_CAP_DRIVER_CONNECT, 0, 0)
          SendMessage_(hWnd, #WM_CAP_SET_SCALE, #True, 0)    
          SendMessage_(hWnd, #WM_CAP_SET_PREVIEWRATE, 15, 0)          
          SendMessage_(hWnd, #WM_CAP_SET_PREVIEW, #True, 0)          
          While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend 
          error$ = "Webcam terminated."
        Else
          error$ = "Unable to connect to webcam."
        EndIf
      Else
        error$ = "Unable to create capture window."
      EndIf
    Else      
      error$ = "Unable to locate capture function."
    EndIf        
    SendMessage_(hWnd, #WM_CAP_STOP, 0, 0)
    SendMessage_(hWnd, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
    DestroyWindow_(hWnd)
    CloseLibrary(0)    
  Else
    error$ = "Unable to open AVI library."
  EndIf
  MessageRequester("Webcam", error$)
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: usb camera

Post by AAT »

ludoke wrote:I will use an usb webcam ...
Read a topic: PureBasic Interface to OpenCV
viewtopic.php?f=12&t=57457
You will find many examples.
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: usb camera

Post by ludoke »

;I try everthing but I don't know how to do it,the examples works fine but
HOW simply open and close webcam with a call to a procedure
;When press button F1 open the webcam ,button F2 close webcam
;

Code: Select all

Enumeration  
  #main_win: #font
  #F1 :#F2   :#button1 ;functietoets met add
#open_camera:#close_camera
EndEnumeration 
;----------------------------------
Declare open_cam()
Declare close_cam()
Declare sneltoets()
;-------------------------------
cwFlags = #WS_CHILD | #WS_VISIBLE
wFlags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
If OpenWindow(#main_win, 0, 0, 1800, 1000, "Webcam Example", wFlags)
  sneltoets()
  ButtonGadget(#F1,100,20,300,40,   "F1 StartCam: ")
  ButtonGadget(#F2,100,60,300,40,   "F2 EndCam")

 Repeat
    Ev = WaitWindowEvent()
    Select ev   ;
     Case   #PB_Event_Gadget        ;  voor muis      
         
        Select EventGadget() 
               
          Case #F1     ;main
            open_cam()
          Case #F2
            close_cam()
        EndSelect
        
      Case    #PB_Event_Menu   ;voor toetsen
        Select EventMenu()            
          Case #F1
             Debug "F1 event"
               open_cam()
          Case #F2
            close_cam() 
        EndSelect
        
EndSelect
Until ev=#PB_Event_CloseWindow
EndIf
;-----------------------------------------------------
Procedure open_cam()
  If OpenLibrary(#main_win, "avicap32.dll")
    *capAddress = GetFunction(#main_win, "capCreateCaptureWindowA")
    If *capAddress
      hWnd = CallFunctionFast(*capAddress, 0, cwFlags,
                              100, 200, 800, 600, WindowID(#main_win), 0)
      If hWnd
        If SendMessage_(hWnd, #WM_CAP_DRIVER_CONNECT, 0, 0)
          SendMessage_(hWnd, #WM_CAP_SET_SCALE, #True, 0)   
          SendMessage_(hWnd, #WM_CAP_SET_PREVIEWRATE, 15, 0)         
          SendMessage_(hWnd, #WM_CAP_SET_PREVIEW, #True, 0)         
          While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
        ;  error$ = "Webcam terminated."
        Else
          error$ = "Unable to connect to webcam."
        EndIf
      Else
        error$ = "Unable to create capture window."
      EndIf
    Else     
      error$ = "Unable to locate capture function."
    EndIf       
    SendMessage_(hWnd, #WM_CAP_STOP, 0, 0)
    SendMessage_(hWnd, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
    DestroyWindow_(hWnd)
    CloseLibrary(#main_win)   
  Else
    error$ = "Unable to open AVI library."
  EndIf
  MessageRequester("Webcam", error$)
EndProcedure
;-------------------------------------------------------------------
Procedure close_cam()
  
EndProcedure
;--------------------------------------------------------------------
Procedure sneltoets()  ;sneltoetsen voor keybord events
   AddKeyboardShortcut(#main_win,  #PB_Shortcut_F1,#F1)
   AddKeyboardShortcut(#main_win,  #PB_Shortcut_F2,#F2)
 EndProcedure
 ;----------------------------------------------------------
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: usb camera

Post by AAT »

Code: Select all

Enumeration 
  #main_win
  #font
  #F1 
  #F2   
  #button1
  #open_camera 
  #close_camera
EndEnumeration

Global *capAddress, hWnd, WinID, cwFlags, LibID

Declare open_cam()
Declare close_cam()
Declare sneltoets()

cwFlags = #WS_CHILD | #WS_VISIBLE
wFlags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
If OpenWindow(#main_win, 0, 0, 1000, 800, "Webcam Example", wFlags)
  sneltoets()
  ButtonGadget(#F1,800,20,100,40,   "F1 StartCam: ")
  ButtonGadget(#F2,800,60,100,40,   "F2 EndCam")  
  WinID = WindowID(#main_win)
    
 Repeat
    Ev = WaitWindowEvent()
    Select ev   ;
     Case   #PB_Event_Gadget        ;  voor muis              
        Select EventGadget()               
          Case #F1     ;main
            open_cam()
          Case #F2
            close_cam()
        EndSelect
       
      Case    #PB_Event_Menu   ;voor toetsen
        Select EventMenu()           
          Case #F1
             Debug "F1 event"
               open_cam()
          Case #F2
            close_cam()
        EndSelect       
EndSelect
Until ev=#PB_Event_CloseWindow
EndIf

Procedure open_cam()
  LibID = OpenLibrary(#PB_Any , "avicap32.dll") 
  If LibID
    *capAddress = GetFunction(LibID, "capCreateCaptureWindowA")      
    If *capAddress
      hWnd = CallFunctionFast(*capAddress, 0, cwFlags, 0, 0, 640, 480, WinID,0) 
      SendMessage_(hWnd, #WM_CAP_DRIVER_CONNECT, 0, 0) 
      SendMessage_(hWnd, #WM_CAP_SET_PREVIEW, #True, 0) 
      SendMessage_(hWnd, #WM_CAP_SET_PREVIEWRATE, 25, 0) 
      SendMessage_(HWnd, #WM_CAP_SET_SCALE, #True, 0)   
    EndIf      
  EndIf 
EndProcedure

Procedure close_cam()
  If *capAddress
    SendMessage_(hWnd, #WM_CAP_STOP, 0, 0)
    SendMessage_(hWnd, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
    DestroyWindow_(hWnd)
    CloseLibrary(LibID)     
  EndIf  
EndProcedure

Procedure sneltoets()  ;sneltoetsen voor keybord events
   AddKeyboardShortcut(#main_win,  #PB_Shortcut_F1,#F1)
   AddKeyboardShortcut(#main_win,  #PB_Shortcut_F2,#F2)
 EndProcedure
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: usb camera

Post by TI-994A »

ludoke wrote:...HOW simply open and close webcam with a call to a procedure
When press button F1 open the webcam button F2 close webcam...
F1 starts the webcam - Ctrl+F1 selects a different webcam - F2 ends the webcam

Code: Select all

Enumeration 
  #appWindow  
  #intructions
  #camWindow
  #capLibrary
  #startCam
  #changeCam
  #endCam
EndEnumeration

Procedure startCam(winID, hCamWnd, reselect = #False)
  Static initDone
  If Not hCamWnd
    If OpenLibrary(#capLibrary, "avicap32.dll")
      *capAddress = GetFunction(#capLibrary, "capCreateCaptureWindowA")
      If *capAddress 
        hCamWnd = CallFunctionFast(*capAddress, 0, #WS_CHILD | #WS_VISIBLE,
                                   0, 0, 600, 400, winID, 0)
        If hCamWnd          
          If SendMessage_(hCamWnd, #WM_CAP_DRIVER_CONNECT, 0, 0)
            If initDone 
              If reselect          
                SendMessage_(hCamWnd, #WM_CAP_DLG_VIDEOSOURCE, 0, 0)
              EndIf              
            Else
              initDone = #True
            EndIf       
            SendMessage_(hCamWnd, #WM_CAP_SET_SCALE, #True, 0)    
            SendMessage_(hCamWnd, #WM_CAP_SET_PREVIEWRATE, 15, 0)          
            SendMessage_(hCamWnd, #WM_CAP_SET_PREVIEW, #True, 0)
            success = #True
          EndIf
        EndIf
      EndIf            
    EndIf    
    If Not success
      MessageRequester("Webcam", "Error initialising webcam.")
    EndIf  
  EndIf  
  ProcedureReturn hCamWnd
EndProcedure

Procedure endCam(hCamWnd, verbose = #False)
  If hCamWnd
    SendMessage_(hCamWnd, #WM_CAP_STOP, 0, 0)
    SendMessage_(hCamWnd, #WM_CAP_ABORT, 0, 0)
    SendMessage_(hCamWnd, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
    DestroyWindow_(hCamWnd)
    CloseLibrary(#capLibrary)
    If verbose
      MessageRequester("Webcam", "Webcam terminated.")
    EndIf 
  EndIf
  ProcedureReturn 0
EndProcedure

ins.s = "F1 to start / Ctrl+F1 to select different camera / F2 to end"
wFlags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
If OpenWindow(#appWindow, 0, 0, 600, 450, "Webcam Example", wFlags)
  TextGadget(#intructions, 0, 420, 600, 30, ins, #PB_Text_Center)
  AddKeyboardShortcut(#appWindow, #PB_Shortcut_F1, #startCam)
  AddKeyboardShortcut(#appWindow, #PB_Shortcut_F2, #endCam)    
  AddKeyboardShortcut(#appWindow, #PB_Shortcut_Control | 
                                  #PB_Shortcut_F1 , #changeCam)  
  Repeat
    event = WaitWindowEvent() 
    Select event
      Case #PB_Event_CloseWindow
        endCam(hCamWnd)
        appQuit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case #startCam
            hCamWnd = startCam(WindowID(#appWindow), hCamWnd)            
          Case #changeCam
            hCamWnd = endCam(hCamWnd)
            hCamWnd = startCam(WindowID(#appWindow), hCamWnd, #True)
          Case #endCam
            hCamWnd = endCam(hCamWnd, #True)
        EndSelect
    EndSelect
  Until appQuit
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: usb camera

Post by ludoke »

thanks ,I try to understand the code.
Post Reply