介绍基于ARM的网络图像传输技术 一段程序,两个线程:一个完成usb摄像头图像的采集,另一个借助网络实现远程传输。图像采集线程通过调用获取图像的程序完成采集任务;网络传输就是socket编程了。
/*******************************************************
* Filename: soket_image.c
* Description:Send image by gprs
* Author: ly44770
* History: 1.0
* Date: 07/06/27
*******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
//#include <sys/ipc.h>
#include <semaphore.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netdb.h>
//#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#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);