现在的位置: 首页 > 综合 > 正文

基于ARM的图像采集和数据传输系统

2018年03月16日 ⁄ 综合 ⁄ 共 11051字 ⁄ 字号 评论关闭

今天完成了前两个模块的整合调试,基本实现了功能!

把代码贴出来!

下一步考虑压缩为jpeg视频数据流,不保存文件直接传输。

视频采集头文件:

#ifndef _CAPTURE_H_
#define _CAPTURE_H_

#include <pthread.h>
#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>

#include <linux/types.h>
#include <jpeglib.h>
#include <setjmp.h>

#define bool int
#define true 1
#define false 0
//#include <sys/times.h>
int errno;
extern pthread_t hthread_capture ;//capture thread!

bool open_video() ;//open video
char *NextFrame() ;//capture one frame data
void close_video() ;//close video and free resource
bool capture_Run(pthread_mutex_t *capture_lock) ;//create capture thread
void *thread_capture(pthread_mutex_t *capture_lock) ;//capture thread function
bool encode_jpeg(char *lpbuf,int width ,int height,pthread_mutex_t *capture_lock) ;//jpeg encode
#endif
视频采集源文件:capture.c

#include "capture.h"
//struct video_capture v_capture ;

//定义视频扑捉相应的结构体
struct video_mbuf v_bufferproperty ;//此结构体利用mmap进行映射帧信息
//实际上是输入到摄像头存储器缓冲区的帧信息
struct video_mmap *v_memorymap ;//用于内存映射
struct video_picture v_imageproperty ;//采集图像的各种属性
struct video_capability v_capability ;//摄像头的基本信息
char *memorymap ;
int bufferIndex=0 ;
//struct v4l vd ;//vidoe4linux

//define the variable
char* deviceName = "/dev/v4l/video0";  
char *fname="temp.jpg" ;
int deviceHandle = 0;
int width = 320;  
int height = 240;  
int depth=16;  
int palette=VIDEO_PALETTE_RGB24;  
 
 
//conrresponding to thread
pthread_t hthread_capture ;//capture thread!

bool open_video()
{
 int ret =-1 ;
 //open device
 deviceHandle=open(deviceName,O_RDWR) ;//read and write mode
 if(deviceHandle==-1)
 {
  printf("Capture:Open failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  return false ;
 }
 
 //get the property of capture
 ret=ioctl(deviceHandle,VIDIOCGCAP,&v_capability) ;
 if(ret == -1)
 {
  printf("Capture:obtain capability failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件
  return false ;
 }
 if((v_capability.type&VID_TYPE_CAPTURE)==0)
 {
  printf("Capture:this device can't capture video to memory!/n") ;
  close(deviceHandle) ;//关闭设备文件
  return false ;
 }
 //print the information of capture
  printf(v_capability.name);
  printf(", Type:%d/n",v_capability.type);
  printf("Maxwidth:%d,Maxheight:%d/n",v_capability.maxwidth ,v_capability.maxheight);
  printf("Minwidth:%d,Minheight:%d/n",v_capability.minwidth,v_capability.minheight);
  printf("Channels:%d,Audios:%d/n",v_capability.channels,v_capability.audios) ;
 
 
 //get property of the picture that captured
 ret=ioctl(deviceHandle,VIDIOCGPICT,&v_imageproperty) ;
 if(ret==-1)
 {
  printf("Capture:obtain picture property failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件
  return false ;
 }
 
 v_imageproperty.palette=palette ;
 v_imageproperty.depth=depth ;
 //set property of the picture that capured
 ret = ioctl(deviceHandle,VIDIOCSPICT,&v_imageproperty) ;
 if(ret==-1)
 {
  printf("Capture:Set the property of image failed!/n") ;  
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件
  return false ;
 }
 //printf("test!/n") ;
 //get the property of  map buffer
 ret=ioctl(deviceHandle,VIDIOCGMBUF,&v_bufferproperty) ;
 if(ret==-1)
 {
  printf("Capture:Get the property of map buffer failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件  
  return false ;
 }
 
 //map
 memorymap=(char *)mmap(0,v_bufferproperty.size,PROT_READ | PROT_WRITE, MAP_SHARED, deviceHandle, 0); 
 if((int)memorymap==-1)
 {
  printf("Capture:Mmap failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件  
  return false ;  
 }
printf("memorymap=%d/n",(int)memorymap) ;
 //allocate video_map structures
 v_memorymap=(struct video_mmap*)(malloc(v_bufferproperty.frames*sizeof(struct video_mmap))) ;
 if(v_memorymap==NULL)
 {
  printf("Capture:malloc failed!/n")  ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(deviceHandle) ;//关闭设备文件  
  return false ;   
 }
 ret=0 ;
// printf("test!/n") ;
 while(ret<v_bufferproperty.frames)
 {
  v_memorymap[ret].frame=ret ;
  v_memorymap[ret].width=width ;
  v_memorymap[ret].height=height ;
  v_memorymap[ret].format=palette ;
  ++ret ;
 }

 ret=0 ;
printf("frame size:%d,frames :%d/n",v_bufferproperty.size,v_bufferproperty.frames) ;
 while(ret<(v_bufferproperty.frames-1))
 {

         if (ioctl (deviceHandle, VIDIOCMCAPTURE, &v_memorymap[bufferIndex]) == -1)  
         {       // capture request failed   
   printf("Capture:capture failed!/n") ;
   printf("Mesg:%s/n",sys_errlist[errno]) ;
   close(deviceHandle) ;//关闭设备文件
   exit(0) ;
         }    
  ++ret ; 
 }
 bufferIndex=v_bufferproperty.frames-1 ;

 return true ;
}
//注意bufferIndex标明当前是第几帧
char* NextFrame()  
{  
        // send a request to begin capturing to the currently indexed buffer   
        if (ioctl (deviceHandle, VIDIOCMCAPTURE, &v_memorymap[bufferIndex]) == -1)  
        {       // capture request failed   
     printf("Capture:capture failed!/n") ;
     printf("Mesg:%s/n",sys_errlist[errno]) ;
     close(deviceHandle) ;//关闭设备文件
     exit(0) ;
        }  
  
        // move bufferIndex to the next frame   
        ++ bufferIndex;  
        if (bufferIndex == v_bufferproperty.frames)  
        {       // bufferIndex is indexing beyond the last buffer   
                // set it to index the first buffer   
            bufferIndex = 0;  
        }  
  
        // wait for the currently indexed frame to complete capture   
        if (ioctl (deviceHandle, VIDIOCSYNC, &v_memorymap[bufferIndex]) == -1)  
        {       // sync request failed 
     printf("Capture:video sync failed!/n") ;
     printf("Mesg:%s/n",sys_errlist[errno]) ;
     close(deviceHandle) ;//关闭设备文件
     exit(0) ; 

        }  
  
        // return the address of the frame data for the current buffer index   
        return (memorymap + v_bufferproperty.offsets[bufferIndex]);  

//free resource
void close_video()
{
 //free the video_mmap structure
 free(v_memorymap) ;
 //unmap the capture memory
 munmap(memorymap,v_bufferproperty.size) ;
 //Close device file
 close(deviceHandle) ;
}
//create thread

bool capture_Run(pthread_mutex_t *capture_lock)
{
 int ret=-1 ;
 //注意在linux下使用线程,在编译链接时要加lpthread
 ret=pthread_create(&hthread_capture,PTHREAD_CREATE_JOINABLE,(void *)thread_capture,capture_lock)  ;
 if(ret==-1)
 {
  printf("Capture:create capture thread failed!/n") ;
  return false ;
 }
// printf("test!/n") ;
 return true ;
}
//thread function
void *thread_capture(pthread_mutex_t *capture_lock)
{
 char *temp=NULL ;
 int a=0 ;
 if(!open_video())
 {
  goto exit ;
 }
// bufferIndex=0 ;
// printf("test11!/n") ;
 while(1)
 {
  temp=NextFrame() ;
  printf("Frames:%d/n",a) ;
  if(!encode_jpeg(temp,width,height,capture_lock))
  {
   goto exit ;
  }
  ++a ;
  
 }
 close_video() ;
exit:
 printf("Capture:capture thread exited !/n") ;
}

bool encode_jpeg(char *lpbuf,int width ,int height,pthread_mutex_t *capture_lock)
{
 struct jpeg_compress_struct cinfo ;//
 struct jpeg_error_mgr jerr ;//
 JSAMPROW  row_pointer[1] ;
 int row_stride ;
 char *buffer=NULL ;
 int x ;
 FILE *fptr_jpg=NULL ;
 pthread_mutex_lock(capture_lock) ;//open lock
 fptr_jpg = fopen (fname,"wb");//注意这里为什么用fopen而不用open
 if(fptr_jpg==NULL)
 {
  printf("Encoder:open file failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;  
  return false ;
 }

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, fptr_jpg);

    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    jpeg_set_defaults(&cinfo);
   

    jpeg_set_quality(&cinfo, 80,true);
   
   
    jpeg_start_compress(&cinfo, TRUE);

    row_stride = width * 3;
 //       row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
  buffer=malloc(row_stride) ;
        row_pointer[0] = buffer;
    while (cinfo.next_scanline < height)
    {
   for (x = 0; x < row_stride; x+=3)
   {
    buffer[x]   = lpbuf[x];
    buffer[x+1] = lpbuf[x+1];
    buffer[x+2] = lpbuf[x+2];
   }
   jpeg_write_scanlines (&cinfo, row_pointer, 1);//critical
   lpbuf += row_stride;
    }

    jpeg_finish_compress(&cinfo);
    fclose(fptr_jpg);
    pthread_mutex_unlock(capture_lock) ;//release lock
    jpeg_destroy_compress(&cinfo);
    free(buffer) ;
    return true ;
 
}

//int main()
//{
// capture_Run() ;
// while(1) ;
// return 0;
//}

网络传输头文件:server_udpsocket.h

#ifndef _SERVER_UDPSOCKET_H_
#define _SERVER_UDPSOCKET_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <pthread.h>

#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/types.h>

 

#include <sys/stat.h>
#include <fcntl.h>

#define bool int
#define true 1
#define false 0

int errno ;
bool init_udpsocket() ;
bool udpsocket_Run(pthread_mutex_t *capture_lock) ;
void *thread_udpsocket(pthread_mutex_t *capture_lock) ;
extern pthread_t hthread_udpsocket ;
#endif
网络传输源文件:server_udpsocket.c

#include "server_udpsocket.h"

int hsockfd;                        // Socket file descriptor
int hnsockfd;                       // New Socket file descriptor
int localport=6767 ;          //Local socket port
struct sockaddr_in addr_local;     //local socket address
struct sockaddr_in addr_remote;  //remote socket address
int sin_size ;
//file operation
FILE *file=NULL ;
char tempbuf[65535] ;
char revbuf[15] ;
//thread
pthread_t hthread_udpsocket ;

bool init_udpsocket()
{
 int ret ;
 
 //create socket file description
 hsockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ;
 if(hsockfd==-1)
 {
  printf("UDPSocket:failed to obtain socket descripter/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;  
  return false ;
 } 
 
 //fill local address
 addr_local.sin_family = AF_INET;           // Protocol Family
  addr_local.sin_port = htons(localport);         // Port number
  addr_local.sin_addr.s_addr  = INADDR_ANY;  // AutoFill local address
  bzero(&(addr_local.sin_zero), 8);          // Flush the rest of struct

//bind port
 ret=bind(hsockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) ;
 if(ret==-1)
 {
  printf("UDPSocket:Bind port failed!/n") ;
  printf("Mesg:%s/n",sys_errlist[errno]) ;
  close(hsockfd) ;
  return false ;
 } 
 return true ;
}

bool udpsocket_Run(pthread_mutex_t *capture_lock)
{
 int ret =-1 ;
 ret=pthread_create(&hthread_udpsocket,PTHREAD_CREATE_JOINABLE,(void *)thread_udpsocket,capture_lock) ;
 return true ;
}

void *thread_udpsocket(pthread_mutex_t *capture_lock)
{
 unsigned int ret ;
 sin_size=sizeof(addr_remote) ;
 //initial udp socket
 if(!init_udpsocket())
 {
  goto exit ;
 }
printf("UDPSocket:udpsocket thread start!/n") ;
 while(1)
 {
  //wait client request
  ret=recvfrom(hsockfd, revbuf,15,0,(struct sockaddr *)&addr_remote, &sin_size) ;//reserve the interface to control capture
          //process ,for example control speed of sending printf("")
printf("hello world!/n") ;  
  if(ret==-1)
  {
   printf("Error:Receive Failed!/n") ;
   break ;
  }
    pthread_mutex_lock(capture_lock) ;
  //open file
  file=fopen("temp.jpg","r") ;
  if(file==NULL)
  {
   printf("UDPSocket:open file failed!/n") ;
   printf("Mesg:%s/n",sys_errlist[errno]) ;   
   goto exit ;
  }
  ret=fread(tempbuf,1,65535,file) ;
  if(ret==-1)
  {
   printf("UDPSocket:read file failed!/n") ;
   printf("Mesg:%s/n",sys_errlist[errno]) ;   
  }else
  {
   ret=sendto(hsockfd,tempbuf,ret,0,(struct sockaddr *)&addr_remote, sin_size) ;
   if(ret==-1)
   {
    printf("UDPSocket:send filed!/n") ;
    printf("Mesg:%s/n",sys_errlist[errno]) ;
   }
   printf("UDPSocket:Send picture!/n") ;
  }
  fclose(file) ;
  pthread_mutex_unlock(capture_lock) ;
 }
exit:
 close(hsockfd) ;
 printf("udpsocket thread exited!/n") ;
}

//int main()
//{
// udpsocket_Run() ;
// while(1) ;
// return 1 ;
//}

服务器端主程序:main.c

#include "server_udpsocket.h"
#include "capture.h"

pthread_mutex_t capture_lock = PTHREAD_MUTEX_INITIALIZER ;
//main
int main()
{
 if(!capture_Run(&capture_lock))//start capture
  return -1 ;

 if(!udpsocket_Run(&capture_lock)) //start server
  return -1 ;

 pthread_join(hthread_udpsocket,NULL) ;//
 pthread_join(hthread_capture,NULL) ;//
 return 0;
}

 

 

抱歉!评论已关闭.