⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 在linux下利用video4linux接口采集单幅图像
💻 C
字号:
#include <stdio.h>#include <fcntl.h>#include <unistd.h>		/* read */#include <errno.h>		/* errno */#include <stdlib.h>#include <termios.h>#include <math.h>#include <sys/fcntl.h>#include <sys/time.h>#include <sys/signal.h>#include <sys/types.h>#include <sys/shm.h>#include <sys/mman.h>#include <sys/resource.h>#include <string.h>#include <malloc.h>#include "v4l.h"#define norm1 VIDEO_MODE_PAL#define DEFAULT_PALETTE VIDEO_PALETTE_RGB24int screen_width=320;int screen_height=240;v4l_device vd;//char v4l_norms[8];int device_init(char *dev, int channel,int norm){   int i;   if (dev == NULL) {      dev = "/dev/video0"; //set to default device   }   if (v4l_open(dev, &vd)) {      return -1;   } else {      v4l_grab_init(&vd, screen_width, screen_height); //wake up drivers!      v4l_close(&vd);   }   if (v4l_open(dev, &vd)) return -1;   if (v4l_get_channels(&vd)) return -1;   if (v4l_set_norm(&vd, norm)) return -1;   if (v4l_mmap_init(&vd)) return -1;   if (v4l_switch_channel(&vd, channel)) return -1;   printf("%s: initialization OK... %s\n"            "%d channels\n"            "%d audios\n\n", dev, vd.capability.name, vd.capability.channels, vd.capability.audios);   for (i = 0; i < vd.capability.channels; i++) {      //printf("Channel %d: %s (pp)\n", i, vd.channel[i].name,v4l_norms[vd.channel[i].norm].name);      printf("Channel %d: %s (pp)\n", i, vd.channel[i].name);   }   printf("v4l: mmap's address = %p\n", vd.map);   printf("v4l: mmap's buffer size = 0x%x\n", vd.mbuf.size);   printf("v4l: mmap's frames = %d (%d max)\n", vd.mbuf.frames, VIDEO_MAX_FRAME);   for (i = 0; i < vd.mbuf.frames; i++) {      printf("v4l: frames %d's offset = 0x%x\n", i, vd.mbuf.offsets[i]);   }   //printf("v4l: channel switch to %d (%s)\n", channel, vd.channel[channel].name);   // start initialize grab   if (v4l_get_picture(&vd)) return -1;   if (v4l_set_palette(&vd, DEFAULT_PALETTE)) return -1;   if (v4l_grab_init(&vd, screen_width, screen_height)) return -1;   if (v4l_grab_sync(&vd)) return -1;   return 0;}int write_ppm24(char *filename, unsigned char* img, int screen_width, int screen_height) { int fp,j,k; unsigned char header[16];unsigned char *p=(unsigned char*)img;unsigned char rgb[3];if ((fp = open (filename, O_WRONLY | O_CREAT, 0644)) < 0) {    printf ("ppm open failed: %d\n", errno);    exit (1);//fp = fopen(filename,"w"); //if(fp == NULL) return -1; }sprintf(header,"P6\n%d %d\n255\n",screen_width, screen_height);//fprintf(fp,"P6\n%d %d\n255\n",screen_width, screen_height); write (fp, header, strlen(header));//fwrite(img, screen_height, 3*screen_width, fp); //p = img;   for (j=0; j<screen_height; j++) {    for (k=0; k<screen_width; k++) {      rgb[2] = *p++;		      rgb[1] = *p++;		      rgb[0] = *p++;		     // p++;			      write(fp, rgb, 3);    } }close(fp); return 0; } int main(){     unsigned char *image;	int wid;	int heigh;	wid=screen_width;	heigh=screen_height;   if (device_init("/dev/video0",0,norm1) == -1) {      perror("device_init: failed...");      exit(1);   }    else {      printf("OK!\n");   }    //v4l_grab_frame(vd,2);  /*if (device_grab_frame(&vd)==-1)   {	   perror(" frame failed");   }    //Ok, grab a frame.   if(v4l_grab_sync(&vd)==-1)   {	   perror("device grab failed");   }   if (device_next_frame(&vd)==-1)   {	   perror(" frame failed");   }    //Ok, grab a frame.   if(v4l_grab_sync(&vd)==-1)   {	   perror("device grab failed");   }   //Wait until captured.*/  image = device_get_address(&vd);    if (write_ppm24("rgb24.ppm",image,wid,heigh)==-1){	   perror("read file failed");	   exit(1);   }   printf("%d\n ",image);  // free(image);   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -