Reading output of a USB webcam in Linux

会有一股神秘感。 提交于 2019-12-03 07:30:43

问题


I was experimenting with a little bit with fread and fwrite in C. So i wrote this little program in C to get data from a webcam and dump it into a file. The following is the source:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 307200 // number of pixels (640x480 for my webcam)
int main() {
    FILE *camera, *grab;
    camera=fopen("/dev/video0", "rb");
    grab=fopen("grab.raw", "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 program works when compiled (gcc -o snap camera.c). What took me by surprise was that the output file was not a raw data dump but a JPEG file. Output of the file command on linux on the programs output file showed it was a JPEG image data: JFIF Standard 1.01. The file was viewable on an image viewer, although a little saturated.

How or why does this happen? I did not use any JPEG encoding libraries in the source or the program. Does the camera output JPEG natively? The webcam is a Sony Playstation 2 EyeToy which was manufactured by Logitech. The system is Debian Linux.


回答1:


The Sony EyeToy has an OV7648 sensor with the quite popular OV519 bridge. The OV519 outputs frames in JPEG format - and if I remember correctly from my own cameras that's the only format that it supports.

Cameras like this require either application support, or a special driver that will decompress the frames before delivery to userspace. Apparently in your case the driver delivers the JPEG frames in their original form, which is why you are getting JPEG data in the output.

BTW, you should really have a look at the Video4Linux2 API for the proper way to access video devices on Linux - a simple open()/read()/close() is generally not enough...



来源:https://stackoverflow.com/questions/11830517/reading-output-of-a-usb-webcam-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!