ARM-based network Image Transmission Technology

Source: Internet
Author: User

 

This article introduces the ARM-based network image transmission technology. There are two threads: one is to complete the acquisition of USB camera images, and the other is to achieve remote transmission through the network. The image collection thread calls the image acquisition program to complete the collection task. network transmission is socket programming.

 

/*************************************** ****************
* 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_cmd2;
 
Typedef struct
{
Unsigned short ID;
Unsigned short datasize;
Char data [506];
Unsigned short verifycode;
} _ 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 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.