Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1083010
  • 博文数量: 135
  • 博客积分: 10182
  • 博客等级: 上将
  • 技术积分: 1565
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-07 16:05
文章分类

全部博文(135)

文章存档

2011年(5)

2010年(20)

2009年(3)

2008年(16)

2007年(91)

分类: LINUX

2007-07-02 16:39:50

   一段程序,两个线程:一个完成usb摄像头图像的采集,另一个借助网络实现远程传输。图像采集线程通过调用获取图像的程序完成采集任务;网络传输就是socket编程了。:-)。
/*******************************************************
 * Filename: soket_image.c
 * Description:Send image by gprs
 * Author: ly44770
 * History: 1.0
 * Date:    07/06/27
 *******************************************************/
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include

#include
#define TRUE 1
#define FALSE 0
#define SERVPORT 9009
#define SERVER_IP "192.168.1.202"
//#define SERVER_IP "192.168.0.21"
typedef char BOOL;
BOOL bDataOK;
#define SIZE_PACKET  512
#define STR_HEAD  "MY_IMG"
#define GH_DBNAME  "MY_IMG_ARM"
#define Group_Devide_Flag "|"
#define STR_TAIL  "AT91RM"
#define NAME_LEN  strlen(GH_DBNAME)
typedef struct
{
 unsigned short  ID;  // Command ID
 char   Parameter1; // Parameter1
 char   Parameter2; // Parameter2
 char   Parameter3; // Parameter3
 char   Parameter4; // Parameter4
} _comedia_CMD;
//_comedia_CMD comedia_CMD;
_comedia_CMD comedia_CMD2;
 
typedef struct
{
 unsigned short ID;
 unsigned short DataSize;
 char         data[506];
 unsigned short VerifyCode;
} _PKG;
_PKG PKG;
_PKG PKG2;
#pragma pack(1)
typedef struct
{
 char BufHead[6];
 char BufDevide1[1];
 char BufDbName[NAME_LEN];
 char BufDevide2[1];
 _PKG m_Pkg;
 char BufDevide3[1];
 char BufTail[4];
} GH_PKG ;
#pragma pack()
GH_PKG SendPkg;
//unsigned long ulImageBuffer;
//unsigned long ulIndex;
void InitPackage()
{
 memset(SendPkg.BufHead,'\0',6);
 memcpy(SendPkg.BufHead,STR_HEAD,strlen(STR_HEAD));
 memset(SendPkg.BufDevide1,'\0',1);
 memcpy(SendPkg.BufDevide1,Group_Devide_Flag,strlen(Group_Devide_Flag));
 memset(SendPkg.BufDbName,'\0',11);
 memcpy(SendPkg.BufDbName,GH_DBNAME,strlen(GH_DBNAME));
 memset(SendPkg.BufDevide2,'\0',1);
 memcpy(SendPkg.BufDevide2,Group_Devide_Flag,strlen(Group_Devide_Flag));
 memcpy(&SendPkg.m_Pkg,&PKG2,sizeof(_PKG));
 memset(SendPkg.BufDevide3,'\0',1);
 memcpy(SendPkg.BufDevide3,Group_Devide_Flag,strlen(Group_Devide_Flag));
 memset(SendPkg.BufTail,'\0',4);
 memcpy(SendPkg.BufTail,STR_TAIL,strlen(STR_TAIL));
}

 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t get_img,send_img;
void consumer(void *arg);
void productor(void *arg);

int main(int argc, char *argv[])
{
 int ret;
 pthread_t id1,id2;
 
 pthread_mutex_init(&mutex,NULL);
 if(ret!=0)
 {
  perror("mutex_init");
 }
 
 ret=sem_init(&get_img,0,0);
 if(ret!=0)
 {
  perror("get_img_sem_init");
 }

 ret=sem_init(&send_img,0,1);
 if(ret!=0)
 {
  perror("send_img_sem_init");
 }

 // InitPackage();
 while(1){
  ret=pthread_create(&id1,NULL,(void *)productor, NULL);
  if(ret!=0)
   perror("pthread cread1");

  ret=pthread_create(&id2,NULL,(void *)consumer, NULL);
  if(ret!=0)
   perror("pthread cread2");

  pthread_join(id1,NULL);
  pthread_join(id2,NULL);
 }
 exit(0);
}
void productor(void *arg)
{
 // int i,nwrite;

 while(1)
 {
  sem_wait(&send_img);
  if(pthread_mutex_lock(&mutex)!=0){
   perror("pthread_mutex_lock");
   exit(1);
  }
  printf("get image pthread start\n");
  if(fork() == 0)
  {
   if(execl("/home/a/vgrabbj","vgrabbj","-f ./1.jpg", "-d /dev/v4l/video0",NULL) < 0)
    perror("execl error!\n");
   printf("get image ok");
  }
  wait(0);
  if(pthread_mutex_unlock(&mutex)!=0){
   perror("pthread_mutex_unlock");
  }
  else
   printf("get image pthread stop\n");
  sem_post(&get_img);
  sleep(1);
  //pthread_exit(0);
 }
}
void consumer(void *arg)
{
 int i,img_fd;
 int sockfd,sendbytes;
 struct sockaddr_in serv_addr;
 void *img_addr = NULL;
 int  img_index;
 void *img_addr2 = NULL;
 int img_index2;
 struct stat sb;
 int Pkg_num,DataSize,LastDataSize;
 BOOL bDone;
 int iIndex,iTemp;
 // bDone = FALSE;
 // iIndex = 0;
 while(1)
 {
  sem_wait(&get_img);
  if(pthread_mutex_lock(&mutex)!=0){
   perror("pthread_mutex_lock");
  }
  else
  {
   printf("send image pthread start\n");
   img_fd = open("/home/work/mywork/image/1.jpg",O_RDONLY);
   if(img_fd < 0)
   { perror("open the image");
    exit(1);
   }
   fstat(img_fd,&sb);
   img_addr = mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,img_fd,0);
   if(img_addr == MAP_FAILED)
   { perror("map the image");
    exit(1);
   }
   LastDataSize = sb.st_size % 506;
   if(LastDataSize == 0)
    Pkg_num = sb.st_size / 506 ;
   else
    Pkg_num = sb.st_size / 506 + 1;
   //LastDataSize = sb.st_size % 506;
   DataSize = Pkg_num * 512;
   //DataSize = sb.st_size;
   img_addr2 = malloc(DataSize);
   if(img_addr2 == NULL)
    printf("malloc error \n");
   img_index = 0;
   img_index2 = 0;
   if(LastDataSize == 0)
   {for(i = 0; i < Pkg_num; i++)
    {
     PKG.ID  = i;
     PKG.DataSize = 506;
     memcpy(PKG.data,img_addr + img_index,506);
     memcpy(img_addr2 + img_index2,&PKG,sizeof(_PKG));
     img_index += 506;
     img_index2 += sizeof(_PKG);
    }
   }else{ 
    for(i = 0; i < (Pkg_num-1); i++)
    {
     PKG.ID  = i;
     PKG.DataSize = 506;
     memcpy(PKG.data,img_addr + img_index,506);
     memcpy(img_addr2 + img_index2,&PKG,sizeof(_PKG));
     img_index += 506;
     img_index2 += sizeof(_PKG);
    }
    //img_index += 506;
    //img_index2 += sizeof(_PKG);
    PKG.ID  = Pkg_num - 1;
    PKG.DataSize = LastDataSize;
    memcpy(PKG.data,img_addr + img_index,LastDataSize);
    memcpy(img_addr2 + img_index2,&PKG,sizeof(_PKG));
   }
#if 0
   fd2 = open("/home/work/mywork/image/2.jpg",O_CREAT|O_RDWR);
   if(fd2 < 0)
   { perror("open the image2");
    exit(1);
   }
   write(fd2,img_add,sb.st_size);
   memcpy(a,img_add,5);
   printf("%c\n",a[1]);
   printf("%x\n",b[0]);
   printf("%x\n",b[1]);
#endif
   munmap(img_addr,sb.st_size);
   close(img_fd);  
   if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
    perror("socket");
    exit(1);
   }
   serv_addr.sin_family=AF_INET;
   serv_addr.sin_port=htons(SERVPORT);
   serv_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
   bzero(&(serv_addr.sin_zero),8);
   if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1){
    perror("connect");
    exit(1);
   }

   comedia_CMD2.ID         = 0x0aaa;
   comedia_CMD2.Parameter1 = 0x05;
   comedia_CMD2.Parameter2 = ( char )( sb.st_size );
   comedia_CMD2.Parameter3 = ( char )( sb.st_size / 256 );
   comedia_CMD2.Parameter4 = ( char )( sb.st_size / 256 / 256 );

   printf("DataSize=%d\n",DataSize);
   printf("PKG.DataSize=%d\n",PKG.DataSize);
   printf("Pkg_num=%d\n",Pkg_num);
   printf("LastDataSize=%d\n",LastDataSize);
   printf("comedia_CMD2.ID=%d\n",comedia_CMD2.ID);
   printf("comedia_CMD2.Parameter1=%d\n",comedia_CMD2.Parameter1);
   printf("comedia_CMD2.Parameter2=%d\n",comedia_CMD2.Parameter2);
   printf("comedia_CMD2.Parameter3=%d\n",comedia_CMD2.Parameter3);
   printf("comedia_CMD2.Parameter4=%d\n",comedia_CMD2.Parameter4);

   InitPackage();
   if((sendbytes=send(sockfd,&comedia_CMD2,sizeof(_comedia_CMD),0))==-1){
    perror("send");
    exit(1);
   }
   printf("send %d bytes\n",sendbytes);
   //   static BOOL bDone;
   //   static int iIndex,iTemp;
   bDone = FALSE;
   iIndex = 0;
   while(!bDone)
   {
    printf("start to receive\n");
    if((iTemp=recv(sockfd,&comedia_CMD2,sizeof(_comedia_CMD),0))==-1)
    {
     bDone = TRUE;
     perror("recv");
     printf("\ntcp read error!\n");
     exit(1);
    }
    printf("recv %d bytes\n",iTemp);
    iIndex += iTemp;
    if( iIndex >= 6 )
    {
     iIndex = 0;
     if( comedia_CMD2.ID == 0x0eaa )
     {
      if( comedia_CMD2.Parameter3 == (char)0xf0
        && comedia_CMD2.Parameter4 == (char)0xf0 )
      {
       //bDataOK = FALSE;
       printf( "\nsend Image id = %d.\n", PKG2.ID );
       bDone = TRUE;
      }
      else
      {
       if( TRUE )
       {
        printf( "\nsend Image id = %d.\n", comedia_CMD2.Parameter3 );
        memcpy(&PKG2,img_addr2 + comedia_CMD2.Parameter3 * 512, sizeof( _PKG ) );
        memcpy(&SendPkg.m_Pkg,&PKG2,sizeof(_PKG));
        printf("\nGH_PKG size= %d\n",sizeof(GH_PKG));
        printf("\n_PKG size= %d\n",sizeof(_PKG));
        if((iTemp=send(sockfd,&SendPkg,sizeof(GH_PKG),0))==-1){
         printf("\ntcp write error!\n");
         perror("send");
         exit(1);
        }
       }
      }
     }
    }
   }
   close(sockfd);
   free(img_addr2);
   img_addr2 = NULL;
   if(pthread_mutex_unlock(&mutex)!=0){
    perror("pthread_mutex_unlock");
   }
   else
    printf("send image pthread stop\n");
   sem_post(&send_img);
   sleep(1);
   //pthread_exit(0);
  }
 }
}
阅读(3486) | 评论(3) | 转发(3) |
给主人留下些什么吧!~~