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

📄 图像采集与传输代码3.txt

📁 Linux的图片采集与传输,图片采集与传输a
💻 TXT
📖 第 1 页 / 共 2 页
字号:
*****************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/videodev.h>
#include <sys/ioctl.h>

#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>

 

#define ERR_FRAME_BUFFER 1
#define ERR_VIDEO_OPEN  2
#define ERR_VIDEO_GCAP  3
#define ERR_VIDEO_GPIC  4
#define ERR_VIDEO_SPIC  5
#define ERR_SYNC     6
#define ERR_FRAME_USING   7
#define ERR_GET_FRAME     8

typedef struct _fb_v4l
{
 // FrameBuffer 信息
   int fbfd ;            // FrameBuffer设备句柄
    struct fb_var_screeninfo vinfo;  // FrameBuffer屏幕可变的信息
    struct fb_fix_screeninfo finfo;  // FrameBuffer固定不变的信息
    char *fbp;            // FrameBuffer 内存指针
  // video4linux信息  
   int fd;               // 
   struct video_capability  capability; // 
   struct video_buffer    buffer;   // 
   struct video_window    window;   // 
   struct video_channel   channel[8]; // 
   struct video_picture   picture;  // 
   struct video_tuner    tuner;   // 
   struct video_audio    audio[8];  // 
   struct video_mmap     mmap;    // 
   struct video_mbuf     mbuf;    // 
   unsigned char  *map;         
   int frame_current;//what 's the frame number being captured currently?
   int frame_using[VIDEO_MAX_FRAME];//帧的状态没有采集还是等待结束?
}fb_v41;

#define DEFAULT_PALETTE VIDEO_PALETTE_RGB565

#define FB_FILE "/dev/fb/0"
//V4L_FILE ''/dev/video0''
//Zhaoyang Modified 
//
#define V4L_FILE "/dev/video1"
/*
187 struct video_mmap
188 {
189         unsigned        int frame;              Frame (0 - n) for double buffer 
190         int             height,width;
191         unsigned        int format;              should be VIDEO_PALETTE_* 
192 };
193 


200 struct video_mbuf
201 {
202         int     size;            Total memory to map 
203         int     frames;         Frames 
204         int     offsets[VIDEO_MAX_FRAME]; //32
205 };

*/
/*********************************************************************************************************
** Function name: get_grab_frame
** Descriptions: 获取图像帧,该函数调用了VIDIOCMCAPTURE的ioctl,获取一帧图片
** Input: *vd,参数指针
**     frame,帧号
** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_grab_frame(fb_v41 *vd, int frame)
{
 //如果正在采集中
   if (vd->frame_using[frame]) {
      fprintf(stderr, "get_grab_frame: frame %d is already used.\n", frame);
      return ERR_FRAME_USING;
   }

   vd->mmap.frame = frame;
   /**
    Start Picture capture from this moment
   /**/
   if (ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) {
      perror("v4l_grab_frame");
      return ERR_GET_FRAME;
   }
   //置为采集忙状态
   vd->frame_using[frame] = 1;
   vd->frame_current = frame;
   return 0;
}

/*********************************************************************************************************
** Function name: get_next_frame
** Descriptions: 获取下一帧的图像
** Input: *vd ,参数指针
** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_first_frame(fb_v41 *vd)
{
  int ret;
  
 vd->frame_current = 0;
 ret = get_grab_frame( vd, 0 );
 if ( ret<0 )
   return ret;
 // 等待帧同步
 if (ioctl(vd->fd, VIDIOCSYNC, &(vd->frame_current)) < 0) 
 {   
 perror("v4l_grab_sync");
      return ERR_SYNC;
  }
 //采集完毕
  vd->frame_using[vd->frame_current] = 0 ;  
 return (0);
}

/*********************************************************************************************************
** Function name: get_next_frame
** Descriptions: 获取下一帧的图像
** Input: *vd ,参数指针
** Output : 返回0表示正常完成返回。
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_next_frame(fb_v41 *vd)
{
 int ret;
 vd->frame_current ^= 1;//两帧采集不是0就是1
 ret = get_grab_frame( vd,vd->frame_current);   // 获取图像数据
 if( ret < 0 )
  return ret;
  
 if (ioctl(vd->fd, VIDIOCSYNC, &(vd->frame_current)) < 0) // 等待帧同步
 {   perror("v4l_grab_sync");
      return ERR_SYNC;
   }
  vd->frame_using[vd->frame_current] = 0 ;//采集完毕置0
 return 0; 
}

 


/*********************************************************************************************************
** Function name: get_frame_address
** Descriptions: 获取帧地址.调用该函数可以获取当前帧的缓冲地址
** Input: *vd ,参数指针
** Output : 返回帧图像数据的指针地址.
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
unsigned char *get_frame_address(fb_v41 *vd)
{
 return (vd->map + vd->mbuf.offsets[vd->frame_current]);  // 从MAP内存中找到当前帧的起始指针
}

 

/*********************************************************************************************************
** Function name: rgb_to_framebuffer
** Descriptions: 写图像数据到Framebuffer,使用该函数前必须成功执行open_framebuffer函数.
** Input: *vd ,参数指针
**    width,图像的宽度vd->mmap.width
**        height,图像高度
**        xoffset,图在Framebuffer X轴偏移量vd->vinfo.xoffset
**        yoffset,图在Framebuffer Y轴偏移量
**        *img_ptr,即将写进FrameBuffer缓冲区指针
** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**
**                    vd->finfo.line_length
**         -------------------------------
**         |                   yoffset                     |
**         | xoffset         *                             |
**         |                                                  |
**         |                                                  |
**          -------------------------------
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void rgb_to_framebuffer( fb_v41 *vd,          // 
                         int width,int height,     // 图像大小
                         int xoffset,int yoffset,      // 图像在Framebuffer偏移位置
                         unsigned short  *img_ptr )  // 图像数据指针
{
  int x,y;
  int location;
  unsigned short *loca_ptr;
  // Figure out where in memory to put the pixel

    for ( y = 0; y < height; y++ )    // 纵扫描
  {
    location = xoffset * 2 +
            (y + yoffset) * vd->finfo.line_length; 
    loca_ptr = (unsigned short *) (vd->fbp + location);         
     for ( x = 0; x < width; x++ )   // 行扫描  
    {

⌨️ 快捷键说明

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