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

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

📁 Linux的图片采集与传输,图片采集与传输a
💻 TXT
📖 第 1 页 / 共 2 页
字号:
      *(loca_ptr + x) = *img_ptr++;
    }
  }
}
/*
void rgb_to_framebuffer( fb_v41 *vd,          // 
                         int width,int height,     // 图像大小
                         int xoffset,int yoffset,      // 图像在Framebuffer偏移位置
                         unsigned short int *img_ptr )  // 图像数据指针
{
  int x,y;
  int location;
  // Figure out where in memory to put the pixel

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

*/


/*********************************************************************************************************
** Function name: open_framebuffer
** Descriptions: 该函数用于初始化FrameBuffer设备,在该函数中打开FrameBuffer设备,并将设备影射到内存
** Input: *ptr,打开Framebuffer设备路径指针
**        *vd ,参数指针
** Output : 返回非0值表示出错
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int open_framebuffer(char *ptr,fb_v41 *vd)
{
  int fbfd,screensize;
     // Open the file for reading and writing
    fbfd = open( ptr, O_RDWR);
    if (fbfd < 0) 
    {
    printf("Error: cannot open framebuffer device.%x\n",fbfd);
    return ERR_FRAME_BUFFER;
    }
    printf("The framebuffer device was opened successfully.\n");
  
  vd->fbfd = fbfd; // 保存打开FrameBuffer设备的句柄
 
    // Get fixed screen information 获取FrameBuffer固定不变的信息
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &vd->finfo)) 
    {
    printf("Error reading fixed information.\n");
    return ERR_FRAME_BUFFER;
    }

    // Get variable screen information 获取FrameBuffer屏幕可变的信息
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vd->vinfo)) 
    {
    printf("Error reading variable information.\n");
    return ERR_FRAME_BUFFER;
    }

    printf("%dx%d, %dbpp, xoffset=%d ,yoffset=%d \n", vd->vinfo.xres, 
       vd->vinfo.yres, vd->vinfo.bits_per_pixel,vd->vinfo.xoffset,vd->vinfo.yoffset );

    // Figure out the size of the screen in bytes
    screensize = vd->vinfo.xres * vd->vinfo.yres * vd->vinfo.bits_per_pixel / 8;

    // Map the device to memory
    vd->fbp = (char *)mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fbfd,0); // 影射Framebuffer设备到内存
    if ((int)vd->fbp == -1) 
    {
    printf("Error: failed to map framebuffer device to memory.\n");
    return ERR_FRAME_BUFFER;
    }
    printf("The framebuffer device was mapped to memory successfully.\n");
  return  0;
}

/*********************************************************************************************************
** Function name: open_video
** Descriptions: 通过该函数初始化视频设备
** Input: *fileptr,打开的文件名指针
**     *vd,参数指针
**     dep,像素深度
**     pal,调色板
**     width,宽度
**     height,高度
** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int open_video( char *fileptr,fb_v41 *vd ,int dep,int pal,int width,int height)
{
  // 打开视频设备

   if ((vd->fd = open(fileptr, O_RDWR)) < 0) 
  {
      perror("v4l_open:");
      return ERR_VIDEO_OPEN;
   }
    printf("=============Open Video Success=======================");
   // 获取设备
   if (ioctl(vd->fd, VIDIOCGCAP, &(vd->capability)) < 0) 
   {
      perror("v4l_get_capability:");
      return ERR_VIDEO_GCAP;
   }
   

   printf("=============Get Device Success=======================");
  // 获取图象  
   if (ioctl(vd->fd, VIDIOCGPICT, &(vd->picture)) < 0) 
   {
      perror("v4l_get_picture");
      return ERR_VIDEO_GPIC;
   }
      printf("=============Get Picture Success=======================");
   // 设置图象
   vd->picture.palette = pal;  // 调色板
   vd->picture.depth = dep;   // 像素深度
//printf("=====Capture depth:%d,Palette:%d================\n",vpic.depth,vpic.palette);
   vd->mmap.format =pal;
   if (ioctl(vd->fd, VIDIOCSPICT, &(vd->picture)) < 0) 
   {
      perror("v4l_set_palette");
      return ERR_VIDEO_SPIC;
   }
   // 
   vd->mmap.width = width;   // width;
   vd->mmap.height = height;  // height;
   vd->mmap.format = vd->picture.palette; 

   vd->frame_current = 0;
   vd->frame_using[0] = 0;
   vd->frame_using[1] = 0;
   
   // 获取缓冲影射信息
   if (ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf)) < 0) 
   {
      perror("v4l_get_mbuf");
      return -1;
   }
   
   // 建立设备内存影射
   vd->map = mmap(0, vd->mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, vd->fd, 0);
   if ( vd->map < 0) 
   {
      perror("v4l_mmap_init:mmap");
      return -1;
   }
       printf("The video device was opened successfully.\n");
  // return get_first_frame(vd);
  return 0;
}

 


/**************************************************************************************************************
**
**
***************************************************************************************************************/
int main( void )
{
  fb_v41 vd;
  int ret,i;
  unsigned short  *imageptr;
  unsigned short  tempbuf[640*480];
  ret = open_framebuffer(FB_FILE,&vd);  // 打开FrameBuffer设备
  if( 0!= ret )              // 打开FrameBuffer设备
  {
    goto err;
  } 
  for(i=0;i<640*480;i++)
   tempbuf[i] = 0xffff;
  rgb_to_framebuffer(&vd,640,480,0,0,tempbuf); // 填充FrameBuffer颜色至整个屏幕
  

  ret = open_video( V4L_FILE, &vd ,
                    16,             // 像素深度
           VIDEO_PALETTE_RGB565,    // 设置调包板
           320,240 );
  if( 0!= ret )   // 打开视频设备失败
  {
    goto err;
  }
  
 printf(vd.capability.name);printf(", Type:%d\n",vd.capability.type);
    printf("Maxwidth:%d,Maxheight:%d\n",vd.capability.maxwidth ,vd.capability.maxheight);
    printf("Minwidth:%d,Minheight:%d\n",vd.capability.minwidth,vd.capability.minheight);
    printf("Channels:%d,Audios:%d\n",vd.capability.channels,vd.capability.audios);
    printf("------Pic Size:%d-------\n",vd.mbuf.size);
  
  while(1)
  {
    imageptr = (unsigned short *) get_frame_address( &vd ); // 
    rgb_to_framebuffer(&vd,vd.mmap.width,vd.mmap.height,
                       160,120,imageptr);   // 
    if(get_next_frame( &vd ) !=0 ) 
    { // 获取图像数据出错
     goto err;
    }

  }
  
 //  exit(0);
err:
 if(vd.fbfd)
  close(vd.fbfd);    // 关闭FrameBuffer设备
 
 if(vd.fd)
  close(vd.fd);
 exit(0);
 return 0;
}
 

⌨️ 快捷键说明

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