V4L2 webcam image grab

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I tried to write a simple program which reads a webcam out and saves the
picture in a file. The camera is a Philips SPC1030NC with the uvc driver.

But the program doesn't work. The pictures look like the image [1] and
I get this output from my program, which is attached:

       Width: 640 Height: 480 Pixelformat: 1196444237
libv4lconvert: Error decompressing JPEG: fill_nbits error: need 2 more bits
       Fertig!

What have I to change in my program?

Regards, Joern

[1] http://royalclan.de/files/forum/webcam_image.ppm
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
//#include <libv4l1.h>
#include <libv4l2.h>
//#include <linux/videodev.h>
#include <linux/videodev2.h>



int main()
{
	struct v4l2_format format;
	char *data;
	int fd;


	fd = v4l2_open("/dev/video0", O_RDWR);

	//Einstelllungen auslesen
	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	if(v4l2_ioctl(fd,VIDIOC_G_FMT, &format)<0){
		printf("Error getting Camera info");
	}

	printf("Width: %d Height: %d Pixelformat: %d\r\n", format.fmt.pix.width, format.fmt.pix.height, format.fmt.pix.pixelformat);

	format.fmt.pix.width=640;
	format.fmt.pix.height=480;
	format.fmt.pix.pixelformat=V4L2_PIX_FMT_RGB24;

	//Werte setzen
	if(v4l2_ioctl(fd, VIDIOC_S_FMT, &format)<0){
		printf("Error setting Camera settings");
	}

	//Speicher für Bild reservieren
	data = malloc((format.fmt.pix.sizeimage*3));
	if(data==NULL){
		printf("Error getting space for image");
		return 1;
	}

	//Bild einlesen
	if(v4l2_read(fd, data, format.fmt.pix.sizeimage*3) < 0){
		printf("v4l2_read() returned error \r\n");
	}

	//Ausgabedatei öffnen + Header schreiben
	FILE * img = fopen("webcam_image.ppm","w");
	fprintf(img, "P6\n%d %d\n255\n",format.fmt.pix.width, format.fmt.pix.height);

	fwrite(data, (format.fmt.pix.sizeimage*3),1, img);

	fclose(img);
	v4l2_close(fd);

	printf("Fertig!\r\n");

   return 0;
}

[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux