Page 1 of 1

Webcam on Linux!

Posted: Sun Jul 15, 2007 1:31 am
by Nik
Well it's 2:23 am here and I got bored, so I tried how webcams work on Linux.
I attached my webcam to the USB port and found it working with XawTV which told me that the resolution of it is 320*240 at RGB24.
I also knew that somehow Video4Linux uses a Video device called /dev/video0 for my Webcam. With this information I started hacking, and believe it or not I had a blurred Image after 3 minutes, some tinkering brought me a clear image and after another 4 minutes I got moving images working.
Here is my rough, non optimized testprogram:

Code: Select all

OpenFile(1,"/dev/video0")
If OpenWindow(0, 100, 100, 300, 300, "PureBasic - Image")


CreateImage(0, 320, 240)
  CreateGadgetList(WindowID(0))
    ImageGadget(0, 10, 10, 200, 200, ImageID(0))

  Repeat 
    Gosub Draw
    SetGadgetState(0, ImageID(0))
    EventID = WindowEvent()
    Delay(1)
  Until EventID = #PB_Event_CloseWindow  
  
EndIf

End  

Draw:
  If StartDrawing(ImageOutput(0))
   For x=1 To 240
    For y=1 To 320
     Plot(y,x,RGB(ReadByte(1),ReadByte(1),ReadByte(1)))
    Next
  Next
StopDrawing() 
EndIf  
Return
To make image processing faster it would be most easy to read one frame at a time into memory.

Posted: Mon Jul 23, 2007 11:09 am
by Anonymous
your code don't work , if you want use webcam on linux & pb , see this api :

http://v4l2spec.bytesex.org/spec/book1.htm

Posted: Mon Jul 23, 2007 11:31 am
by Nik
Well it definitely works here but yes using the V4L Api is the elegant way.

Posted: Tue Dec 11, 2007 7:03 pm
by Niffo
Cpl.Bator wrote:your code don't work , ...
Does not work here too : black image (QuickCam VC)

Re: Webcam on Linux!

Posted: Sun Dec 12, 2010 7:23 pm
by Ulix
Hello everybody!

(With text translated by Google. Thanks to him! :oops: )
 
Sorry to dig this!

Actually this code does not work! Picture to black.

Nobody has proposed a working code for capturing a picture or video from a Webcam on Linux, using the functions of the lib. V4L2.

If anyone has some code, it could help me!

I have found on: http://v4l2spec.bytesex.org/spec/book1.htm any sort of explanation!
But I am unable to translate in PureBasic, statements like this little piece of code that follows:

Code: Select all

 Information about the current video input
struct v4l2_input input;
int index;

if (-1 == ioctl (fd, VIDIOC_G_INPUT, &index)) {
        perror ("VIDIOC_G_INPUT");
        exit (EXIT_FAILURE);
}

memset (&input, 0, sizeof (input));
input.index = index;

if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
        perror ("VIDIOC_ENUMINPUT");
        exit (EXIT_FAILURE);
}

printf ("Current input: %s\n", input.name);

How to access the functions of the lib. V4L2?
With: "OpenLibrary (# PB_Any," / usr/lib/libv4l2.so ")?

How to translate: ioctl?
Should we include all structures?
What are the constants?
Etc. ......... :evil:

A challenge too great for me!

In short, if anyone has some code, it could help me!

Thank you in advance

Re: Webcam on Linux!

Posted: Sun Sep 28, 2014 8:49 pm
by langinagel
Just a snapshot function, but running properly here:

Code: Select all

Procedure snapshot()
  Protected PRG.s
  Protected savfile.s
  Protected resolution.s
PRG.s = "-v -r "
savfile.s = "Bild.jpg"
resolution.s = Chr(34)+"640x480" + Chr(34)+" "
PRG = PRG + resolution + savfile
;Debug PRG
RunProgram("fswebcam", PRG ,"/home/thorsten/Bilder/webcam_pics")
EndProcedure

snapshot()
It uses fswebcam.

Greetings
LN