reading the camera device
ok, i have this little bit of code to read the camera device and return the buffer to another function for writing to a ppm image. the problem is that when it gets to the read function, it hangs and never resumes. obviously there is a problem somewhere in the way of length of reading the bytes in.
the program is attached.
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] matthewh.genestate.com
/*Spiros Ioannou 2/2000 */
/*[email protected]*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/types.h>
#include <errno.h>
/*#include <linux/videodev.h>*/
#include "../conf.h"
void save_ppm(char *filename, unsigned char *pic);
int main()
{
int dev; /* the device we will use */
int len;
char device[FILENAME_SIZE] = "/dev/video0";
unsigned char pic[MAX_RGBA_IMAGE_SIZE];char filename[FILENAME_SIZE] = "./test.ppm";
dev = open (device, O_RDWR);
if (dev < 0)
{
perror(device);
exit(1);
}
fprintf(stderr, "about to read\n");
len = read(dev, pic, MAX_RGBA_IMAGE_SIZE);
if (len == 0 || len == -1)
{
fprintf(stderr, "error reading camera device\n");
exit(0);
}
fprintf(stderr, "read\n");
/*while((len = read(dev, pic, MAX_RGBA_IMAGE_SIZE)) <= 0)
{
if(errno != 0 && errno != EINTR)
{
fprintf(stderr, "Error read image\n");
exit(0);
}
}*/
save_ppm(filename, pic);
exit(0);
}void save_ppm(char *filename, unsigned char *pic)
{
FILE *outfile;
unsigned char buff[3];
int i;
if (!(outfile=fopen(filename,"w"))) {
perror(filename);
exit(errno);
};
fprintf (outfile, "P6\n%d %d\n%d\n", 640, 480, 255);
for (i = 0; i < 640*480*3; i+=3) {
buff[0] = pic[i]; /* r */
buff[1] = pic[i+1]; /* g */
buff[2] = pic[i+2]; /* b */
fwrite (buff, 1, 3, outfile);
}
fclose(outfile);
}/************************************************
* this is the config file containing defaults *
* for the camera program. all defaults are *
* controlled with preprocessor macros *
************************************************/
#define FILENAME_SIZE 20 /* char size for holding filenames */
#define DEFAULT_NOISE_THREASHOLD 60 /* level of noise */
#define DEFAULT_PIXEL_THREASHOLD 1500 /* number of changed pixels */
#define X_AXIS_IMAGE_SIZE 640 /* work it out */
#define Y_AXIS_IMAGE_SIZE 480 /* ditto */
#define DAY_LIGHT_LEVEL 180 /* mark to differentiate between day and night */
#define TIMEOUT_LEN 1000 /* ??? - doesn't seem to be used anywhere else */
#define MAX_RGBA_IMAGE_SIZE ((640*480*4)+256) /* maximum size for capturing an image */