Reputation: 63
I'm writing a simple C code for taking capture from my webcam and save it as a jpeg file on Ubuntu. Here is the code used :
#include <stdio.h>
#include <stdlib.h>
#define SIZE 76800 // number of pixels (320x240 for my webcam)
int main() {
FILE *camera, *grab;
camera=fopen("/dev/video0", "rb");
grab=fopen("grab.jpeg", "wb");
float data[SIZE];
fread(data, sizeof(data[0]), SIZE, camera);
fwrite(data, sizeof(data[0]), SIZE, grab);
fclose(camera);
fclose(grab);
return 0;
}
The resulting file is not a jpeg, how can I modify my code to obtain jpeg or png file at the end ?
Upvotes: 2
Views: 9236
Reputation: 663
This won't work. Because camera is a complex device and you have to set resolution, color scheme and some other important data to start capture.
You should use something like libuvc.
https://ken.tossell.net/libuvc/doc/group__device.html
You can check here. In line 614 they are trying to find a device and use it for camera grabbing. I think it's a great example.
https://github.com/gadLinux/OpenHMD-RiftPlayground/blob/master/src/main.c
Basically you have to init context, find camera, and use it.
Upvotes: 5