Webcam on Linux!

Linux specific forum
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Webcam on Linux!

Post 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.
Anonymous

Post 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
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Well it definitely works here but yes using the V4L Api is the elegant way.
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post by Niffo »

Cpl.Bator wrote:your code don't work , ...
Does not work here too : black image (QuickCam VC)
Niffo
Ulix
User
User
Posts: 48
Joined: Wed Jan 23, 2008 12:45 pm
Location: France, Montpellier

Re: Webcam on Linux!

Post 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
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Webcam on Linux!

Post 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
https://www.doerpsoft.org

Boost. Work. Efficiency.
Post Reply