Page 1 of 5
get live film from a camera by ported usb or other
Posted: Fri Sep 17, 2010 4:09 pm
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...
link, code, guide, WKey ...any
Thanks
Re: get live film from a camera by ported usb or other
Posted: Sat Sep 18, 2010 8:06 am
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
Re: get live film from a camera by ported usb or other
Posted: Sat Sep 18, 2010 12:23 pm
by +18
Thanks IdeasVacuum
other help is need too...if it is
Re: get live film from a camera by ported usb or other
Posted: Sun Sep 19, 2010 11:39 am
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
Re: get live film from a camera by ported usb or other
Posted: Mon Sep 20, 2010 11:49 am
by +18
thanks AndyMK
i 'll try work it
Re: get live film from a camera by ported usb or other
Posted: Mon Jul 25, 2011 4:46 pm
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() ??
Re: get live film from a camera by ported usb or other
Posted: Tue Jul 26, 2011 9:45 am
by Lord
Put Wxl_Lib_Win32 in PureLibraries\userlib and
restart PureBasic.
Re: get live film from a camera by ported usb or other
Posted: Wed Jul 27, 2011 7:03 pm
by IdeasVacuum
Re: get live film from a camera by ported usb or other
Posted: Sat Sep 08, 2012 1:35 am
by chris319
This is a very handy program, but is there any way to solve the problem it has with the stack overflowing?
Re: get live film from a camera by ported usb or other
Posted: Sat Sep 08, 2012 6:18 pm
by morosh
I tried to compile demo.pb, I get error at line 41:
SetGadgetState(0, ImageID(image2))
the specified #image is not initialised

Re: get live film from a camera by ported usb or other
Posted: Sun Sep 09, 2012 8:38 am
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
Re: get live film from a camera by ported usb or other
Posted: Sun Sep 09, 2012 3:37 pm
by morosh
I still have the same error at the same line, may be there is something wrong in my computer

Re: get live film from a camera by ported usb or other
Posted: Thu Sep 27, 2012 11:20 pm
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
Re: get live film from a camera by ported usb or other
Posted: Tue Oct 09, 2012 9:55 am
by chris319
10/9/12: Source code updated with faster pixel plotting.
Re: get live film from a camera by ported usb or other
Posted: Sun Oct 21, 2012 11:57 am
by r7mk4
chris319 wrote:10/9/12: Source code updated with faster pixel plotting.
Thanks a lot!!! Great work.
