在Linux中读取USB网络摄像头的输出 [英] Reading output of a USB webcam in Linux

查看:1658
本文介绍了在Linux中读取USB网络摄像头的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C中的fread和fwrite。所以我在C中编写了这个小程序,从网络摄像头获取数据并将其转储到文件中。以下是来源:

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;
}

程序在编译时有效( gcc -o snap camera.c )。令我惊讶的是输出文件不是原始数据转储而是JPEG文件。在程序输出文件上输出linux上的文件命令显示它是一个JPEG图像数据:JFIF Standard 1.01。该文件在图像查看器上可见,虽然有点饱和。

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.

这是怎么发生的?我没有在源代码或程序中使用任何JPEG编码库。相机是否原生输出JPEG?网络摄像头是由Logitech制造的Sony Playstation 2 EyeToy。该系统是Debian Linux。

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.

推荐答案

Sony EyeToy 有一个OV7648传感器,配有非常流行的OV519桥接器。 OV519以JPEG格式输出帧 - 如果我从自己的相机中正确记住它是唯一支持的格式。

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.

这样的相机需要应用程序支持,或者在交付给用户空间之前将解压缩帧的特殊驱动程序。显然,在你的情况下,驱动程序以原始形式提供JPEG帧,这就是你在输出中获取JPEG数据的原因。

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,你真的应该看看在 Video4Linux2 API 上以正确的方式访问Linux上的视频设备 - 一个简单的 open()/ read( )/ close()通常不够......

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...

这篇关于在Linux中读取USB网络摄像头的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆