get live film from a camera by ported usb or other

Just starting out? Need help? Post your questions and find answers here.
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

get live film from a camera by ported usb or other

Post by +18 »

how can i connect my camera (any ... mobile camera...) to a pb app for showing on pc and save?

any start point for me please... :idea:
link, code, guide, WKey ...any
Thanks
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: get live film from a camera by ported usb or other

Post by IdeasVacuum »

If it's for a specific camera, check the CD for a driver (DLL). If you want to make a generic app for any camera, that's a bit more tricky. On Windows, if the cameras are compatible with Microsoft's HID (Human Interface Device) spec, then you can use MS drivers and API

http://www.purebasic.fr/english/viewtopic.php?p=298707
http://www.purebasic.fr/english/viewtopic.php?p=49139
http://www.purebasic.fr/english/viewtop ... 03&start=0
http://narod.yandex.ru/100.xhtml?pure-b ... --4.50.rar
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Re: get live film from a camera by ported usb or other

Post by +18 »

Thanks IdeasVacuum

other help is need too...if it is
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: get live film from a camera by ported usb or other

Post by AndyMK »

You could try this. Assuming you have a USB capture device connected with drivers installed or a webcam, the code below will capture the video and display it in realtime compressed with JPEG. You can adapt it to save to disk in your own motion JPEG format so that it does not eat up all your HDD. It's pretty fast and it does not use temporary files to grab images. The zip includes everything you need. Make sure you copy the lib to your pb userlibs folder. Have fun :)

Code: Select all

timeBeginPeriod_(1)
IncludeFile "escapi.pbi"
UseJPEGImageDecoder()

device = 0
Width = 320
Height = 240

dest = AllocateMemory(Width*Height)
destLen = MemorySize(dest)
count = setupESCAPI()

name$ = Space(1000)
getCaptureDeviceName(device, @name$, 1000)

scp.SimpleCapParams
scp\mWidth = Width
scp\mHeight = Height
scp\mTargetBuf = AllocateMemory(scp\mWidth*scp\mHeight*4)

If initCapture(device, @scp)
  
  image = CreateImage(#PB_Any, Width, Height)
  
  OpenWindow(0, 0, 0, Width, Height, name$, #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ImageGadget(0, 0, 0, Width, Height, ImageID(image))
  wxl_EncodePixelFormat(#PB_PixelFormat_32Bits_BGR)

  Repeat
    Event = WindowEvent()
    
    doCapture(device)
    While isCaptureDone(device) = 0
      Delay(1)
    Wend
        
    len = wxl_JPEGEncode(scp\mTargetBuf, Width, Height, 50, dest, destLen)
    
    CatchImage(image2, dest, len)
    SetGadgetState(0, ImageID(image2))
    
  Until Event = #PB_Event_CloseWindow
  
  deinitCapture(device)
Else
  Debug "init capture failed!"
EndIf
timeEndPeriod_(1)
End
Download:
http://dl.dropbox.com/u/7526263/Appz/PB ... apture.zip
Last edited by AndyMK on Mon Sep 20, 2010 4:15 pm, edited 1 time in total.
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Re: get live film from a camera by ported usb or other

Post by +18 »

thanks AndyMK
i 'll try work it
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: get live film from a camera by ported usb or other

Post by ar-s »

AndyMK wrote:You could try this. Assuming you have a USB capture device connected with drivers installed or a webcam, the code below will capture the video and display it in realtime compressed with JPEG. You can adapt it to save to disk in your own motion JPEG format so that it does not eat up all your HDD. It's pretty fast and it does not use temporary files to grab images. The zip includes everything you need. Make sure you copy the lib to your pb userlibs folder. Have fun :)

Code: Select all

timeBeginPeriod_(1)
IncludeFile "escapi.pbi"
UseJPEGImageDecoder()

device = 0
Width = 320
Height = 240

dest = AllocateMemory(Width*Height)
destLen = MemorySize(dest)
count = setupESCAPI()

name$ = Space(1000)
getCaptureDeviceName(device, @name$, 1000)

scp.SimpleCapParams
scp\mWidth = Width
scp\mHeight = Height
scp\mTargetBuf = AllocateMemory(scp\mWidth*scp\mHeight*4)

If initCapture(device, @scp)
  
  image = CreateImage(#PB_Any, Width, Height)
  
  OpenWindow(0, 0, 0, Width, Height, name$, #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ImageGadget(0, 0, 0, Width, Height, ImageID(image))
  wxl_EncodePixelFormat(#PB_PixelFormat_32Bits_BGR)

  Repeat
    Event = WindowEvent()
    
    doCapture(device)
    While isCaptureDone(device) = 0
      Delay(1)
    Wend
        
    len = wxl_JPEGEncode(scp\mTargetBuf, Width, Height, 50, dest, destLen)
    
    CatchImage(image2, dest, len)
    SetGadgetState(0, ImageID(image2))
    
  Until Event = #PB_Event_CloseWindow
  
  deinitCapture(device)
Else
  Debug "init capture failed!"
EndIf
timeEndPeriod_(1)
End
Download:
http://dl.dropbox.com/u/7526263/Appz/PB ... apture.zip
wxl_EncodePixelFormat() ??
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: get live film from a camera by ported usb or other

Post by Lord »

Put Wxl_Lib_Win32 in PureLibraries\userlib and
restart PureBasic.
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: get live film from a camera by ported usb or other

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: get live film from a camera by ported usb or other

Post by chris319 »

This is a very handy program, but is there any way to solve the problem it has with the stack overflowing?
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: get live film from a camera by ported usb or other

Post by morosh »

I tried to compile demo.pb, I get error at line 41:
SetGadgetState(0, ImageID(image2))
the specified #image is not initialised

:(
PureBasic: Surprisingly simple, diabolically powerful
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: get live film from a camera by ported usb or other

Post by chris319 »

Do you get this error on the first iteration or after it has run for a while?

The program as posted has built-in memory and image leaks. Perhaps that provides a clue as to why the stack overflows on line 38. Here is a version which fixes the leaks but not the overflow:

Code: Select all

timeBeginPeriod_(1)
IncludeFile "escapi.pbi"
UseJPEGImageDecoder()

device = 0
Width = 320
Height = 240

dest = AllocateMemory(Width*Height)
destLen = MemorySize(dest)
count = setupESCAPI()

name$ = Space(1000)
getCaptureDeviceName(device, @name$, 1000)

scp.SimpleCapParams
scp\mWidth = Width
scp\mHeight = Height
scp\mTargetBuf = AllocateMemory(scp\mWidth * scp\mHeight * 4)

If initCapture(device, @scp)
  
  image = CreateImage(#PB_Any, Width, Height)
  
  OpenWindow(0, 0, 0, Width, Height, name$, #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ImageGadget(0, 0, 0, Width, Height, ImageID(image))
  wxl_EncodePixelFormat(#PB_PixelFormat_32Bits_BGR)
  
  Repeat
    Event = WindowEvent()
    
    doCapture(device)
    While isCaptureDone(device) = 0
      Delay(1)
    Wend
    
    Quality = 20
    len = wxl_JPEGEncode(scp\mTargetBuf, Width, Height, Quality, dest, destLen)
    
    CatchImage(image2, dest, len)
    SetGadgetState(0, ImageID(image2))
    
  Until Event = #PB_Event_CloseWindow
  
  deinitCapture(device)
Else
  Debug "Init capture failed."
EndIf
timeEndPeriod_(1)

If dest <> 0
  FreeMemory(dest)
EndIf

If scp\mTargetBuf <> 0
  FreeMemory(scp\mTargetBuf)
EndIf

If image <> 0
  FreeImage(image)
EndIf

If image2 <> 0
  FreeImage(image2)
EndIf

End
Last edited by chris319 on Thu Sep 13, 2012 10:13 pm, edited 1 time in total.
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: get live film from a camera by ported usb or other

Post by morosh »

I still have the same error at the same line, may be there is something wrong in my computer :(
PureBasic: Surprisingly simple, diabolically powerful
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: get live film from a camera by ported usb or other

Post by chris319 »

This webcam program is pretty handy, but the version that uses the JPEG encoder crashes after a while due to a stack overflow presumably caused by the JPEG encoder (I don't know why it is necessary to encode to JPEG in the first place). I'll leave profiling to others, but I can't imagine a JPEG encoder being less computationally intensive with all the computations it has to perform than straightforward pixel addressing. This version bypasses the JPEG encoder and hasn't crashed after an extended period of use. It also has more efficient pixel addressing and properly releases the resources it allocates. Always, ALWAYS release the resources your program allocates. Failure to do so is a bug (memory leak) regardless of whether or not it affects the operation of your program.

Code: Select all

IncludeFile "escapi.pbi"

;#WIDTH = 1280/2 ;640
;#HEIGHT = 720/2 ;360

#WIDTH = 640 ;1280
#HEIGHT = 360 ;#WIDTH * 0.5625 ;16:9 ASPECT RATIO -- YOU MAY ADJUST TO SUIT
;#WIDTH = 1280
;#HEIGHT = 720

#WIDTHM1 = #WIDTH - 1
#HEIGHTM1 = #HEIGHT - 1
pixCount = (#WIDTH * #HEIGHT) - 2

    device = 0

    count = setupESCAPI()
;    Debug "Init: " + Str(count)

    If count = 0
      MessageRequester("Error", "Unable to initialize ESCAPI.")
      End
    EndIf

    name$ = Space(255)
    getCaptureDeviceName(device, @name$, 255)
;    Debug "Name: " + name$

    bufSize = #WIDTH * #HEIGHT * 4
    scp.SimpleCapParams
    scp\mWidth = #WIDTH
    scp\mHeight = #HEIGHT
    scp\mTargetBuf = AllocateMemory(bufSize)
    *buf = scp\mTargetBuf

    If initCapture(device, @scp)
;      Debug "Cap init successful."
     
      image = CreateImage(0, #WIDTH, #HEIGHT, 24)
      OpenWindow(0, 50, 50, #WIDTH, #HEIGHT, name$, #PB_Window_SystemMenu)
      ImageGadget(0, 0, 0, #WIDTH, #HEIGHT, ImageID(0))
      offset = 0
      Quit = #False

      Repeat
     
        doCapture(device)
        While isCaptureDone(device) = #False
          If WaitWindowEvent(1) = #PB_Event_CloseWindow
            Quit = #True
            Break
          EndIf       
        Wend

offset = 0       
StartDrawing(ImageOutput(0))   
*writeBuffer = DrawingBuffer()
pitch = DrawingBufferPitch()
StopDrawing()

;NEEDS PIXEL-BY-PIXEL READING AND WRITING
hm1 = *writebuffer + (#HEIGHTM1 * pitch)
For y = 0 To #HEIGHTM1
  For x = 0 To #WIDTHM1
    PokeL(hm1 + x*3, PeekL(*buf + offset))
    offset + 4
  Next
hm1 - pitch
Next
          
SetGadgetState(0, ImageID(0))

Until Quit = #True

      deinitCapture(device)
      FreeImage(0)
      FreeMemory(scp\mTargetBuf)
      CloseWindow(0)
    Else
      Debug "Init capture failed."
    EndIf

    End
Last edited by chris319 on Mon Oct 22, 2012 3:38 am, edited 1 time in total.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: get live film from a camera by ported usb or other

Post by chris319 »

10/9/12: Source code updated with faster pixel plotting.
r7mk4
User
User
Posts: 13
Joined: Tue Aug 02, 2005 2:23 pm

Re: get live film from a camera by ported usb or other

Post by r7mk4 »

chris319 wrote:10/9/12: Source code updated with faster pixel plotting.
Thanks a lot!!! Great work. :!:
Post Reply