1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include "../headers/webcam.h"
int descrpFichier;
struct video_capability vidcap;
struct video_window fenetre;
struct video_capture capture;
struct video_mbuf mbuf;
struct video_mmap mapbuf;
struct video_buffer buffer;
unsigned char *ptr;
unsigned char *posImg;
/**Ouverture de la camera**/
void
openCam (char *deviceName)
{
descrpFichier = open (deviceName, O_RDWR);
if (descrpFichier < 0)
{
printf ("\aAucun peripherique detecte\n");
printf ("Verifiez les droits d'acces a /dev/video0\n");
exit (-1);
}
}
/**Fermeture de la camera**/
void
closeCam (void)
{
printf ("Le programme a quitte correctement\n");
close (descrpFichier);
munmap (ptr, WIDTH*HEIGHT*3);
}
/**Informations sur les capacites du peripherique**/
void
printCap (void)
{
if (ioctl (descrpFichier, VIDIOCGCAP, &vidcap) < 0)
exit (-1);
if (ioctl (descrpFichier, VIDIOCGWIN, &fenetre) < 0)
exit (-1);
printf ("Nom: %s\n", vidcap.name);
if (!(vidcap.type && VID_TYPE_CAPTURE))
{
printf ("Le peripherique detecte ne supporte pas la capture\n");
exit (-1);
}
printf ("Type: Camera\n");
}
/**Definition de la fenetre de capture**/
void
setWin (void)
{
fenetre.x = 0;
fenetre.y = 0;
fenetre.width = WIDTH;
fenetre.height = HEIGHT;
fenetre.clipcount = 0;
fenetre.chromakey = 0;
fenetre.clipcount = 0;
if (ioctl (descrpFichier, VIDIOCSWIN, &fenetre) < 0)
{
perror ("VIDIOCSWIN");
printf("Verifiez que la webcam supporte bien l'acquisition d'une image de %ix%i pixels.\n",WIDTH,HEIGHT);
exit (-1);
}
}
/**Definition des proprietes de l'image**/
void
setImage (int hue, int colour, int contrast, int brightness, int whiteness,
int depth)
{
struct video_picture immage;
/*on recupere les valeurs actuelle*/
if (ioctl (descrpFichier, VIDIOCGPICT, &immage) < 0)
{
perror ("VIDIOCGPICT");
exit (-1);
}
/*on les modifie pour leur donner les nouvelles valeur*/
immage.hue = hue * 65536 / 100;
immage.colour = colour * 65536 / 100;
immage.contrast = contrast * 65536 / 100;
immage.brightness = brightness * 65536 / 100;
immage.whiteness = whiteness * 65536 / 100;
immage.depth = depth;
immage.palette = PALETTE;
/*on les attribut au peripherique*/
if (ioctl (descrpFichier, VIDIOCSPICT, &immage) < 0)
{
perror ("VIDIOCSPICT");
exit (-1);
}
}
/**Cette fonction de capture prend en param un pointeur sur une structure RGB24
et la modifie pour quelle contienne les composantes rgb de l'image prise.**/
void captureImage (RGB *rgb24)
{
int i, j;
int y = 0, x = 0;
if (ioctl (descrpFichier, VIDIOCGMBUF, &mbuf) < 0)
{
perror ("VIDIOCGMBUF");
exit (-1);
}
ptr =
(unsigned char *) mmap (0, WIDTH*HEIGHT*3, PROT_READ | PROT_WRITE, MAP_SHARED,
descrpFichier, 0);
if (ptr == ((unsigned char *) -1))
{
perror ("mmap");
exit (-1);
}
mapbuf.frame = 0;
mapbuf.height = HEIGHT;
mapbuf.width = WIDTH;
mapbuf.format = PALETTE;
/* on demande une capture : */
if (ioctl (descrpFichier, VIDIOCMCAPTURE, &mapbuf))
{
perror ("VIDIOCMCAPTURE");
printf ("L'application doit quitter\n\a");
closeCam ();
exit (-1);
}
i = -1;
/* Wait frame to be completed */
if (ioctl(descrpFichier, VIDIOCSYNC, &mapbuf.frame) < 0) {
perror ("VIDIOCSYNC");
exit (1);
}
/*posImg est le pointeur vers le debut de l'image */
posImg = ptr ;
/*on remplit la structure RGB24 avec les donnes rgb : */
/*on inverse lordre des pixels pour rétablir le sens de images*/
i = 0;
j= 3*WIDTH*HEIGHT-1-2;
for (y = 0; y < HEIGHT; y++)
{
for (x = 0; x < WIDTH; x++)
{
/*On remplit le tableau rgb2 avec les composantes */
rgb24->rgb2[i] = posImg[j];
i++; j++;
rgb24->rgb2[i] = posImg[j];
i++; j++;
rgb24->rgb2[i] = posImg[j];
i++;
j-=5;
}
}
rgb24->width = WIDTH;
rgb24->height = HEIGHT;
} |
Partager