Linux下获取相机照片的C程序

标签 c linux camera linux-device-driver

所以我想知道如何才能从 Logitech C270 相机获取照片?我在下面发布了我的代码,似乎相机指向的文件是空的,因为起始地址和结束地址是相同的。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 307200 // number of pixels (640x480 for my webcam)
int main() {
//create files
    FILE *camera, *grab;
    camera=fopen("/dev/v4l/by-id/usb-046d_0825_1155F490-video-index0", "rb");

//testing files

if(camera==NULL){fputs("\n Can't open camera!",stderr);}

if(feof(camera)){fputs("\n End of file reached!",stderr);}

if(ferror(camera)){fputs("\n The camera has an error!",stderr);}

//stats
rewind (camera); //set position indicator at begining of file
printf("%p is the pos of pointer start\n",&camera); //starting address 
fseek(camera,0,SEEK_END);//set position indicator to EndOfFile
printf("%p is the pos of pointer end\n",&camera); //ending address

//running
    grab=fopen("grab.raw", "wb");
    float data[SIZE];
    int n=2; //arbitrary non 0 num to test if fread = 0
    n=fread(data, sizeof(data[0]), SIZE, camera);
if(n=0){perror(ERRNO);}
    printf(" %d is the state of fread \n",n);  //fread's value
    fwrite(data, sizeof(data[0]), SIZE, grab);
    fclose(camera);
    fclose(grab); 
    return 0;
}

//testing 文件中没有任何文本出现,我只是得到 fread() 为 0 的事实,并且我得到的起始地址和结束地址是相同的。这是什么意思? 谢谢!

最佳答案

相机中的文件与计算机上的文件非常相似。

有一个顶级目录(通常)在其下面有两个目录。

所有视频/图片都位于最底部的目录中。

相机中的文件不是仅包含图像的原始二进制文件。

相反,它们是格式化的(图片通常是 .jpg 文件,电影通常是 mpg3 或 mpg4)

上层目录(通常)是单个条目,因此大小将为 0。

如果您使用文件浏览器访问相机,您将看到目录结构。

您的代码需要遵循相机文件结构。

当代码到达实际图像或电影时,它可以检查文件扩展名以确定下一步要做什么。

stat() 将返回有关文件的详细信息,包括其大小。

stat() 信息可以/应该用于为文件分配足够的内存。

然后可以将文件读入最终的内存分配中。

关于Linux下获取相机照片的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922386/

相关文章:

c - 在原始套接字上 Ping 数据包

c# - zlib 的 gzip 压缩和 .NET 的 GZipStream 使用的压缩有什么区别?

linux - 如何在 Linux 或 OS X 上为 Swift 包生成代码覆盖率?

java - 减慢相机对象的 setPreviewCallback 方法

c - 大型静态数组是否会破坏堆栈?

c - 警告 : incompatible pointer types

linux - 有没有办法以编程方式控制 Linux 上的硬盘驱动器事件灯?

python - subprocess.Popen 产生错误,而命令在命令行中运行顺利(错误 : find: paths must precedeexpression)

android - 两个带线程的 SurfaceView - 相机预览无法恢复

安卓2.2。人像相机预览