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

📄 cmdserver.c

📁 关于处理图像二值化的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************/
/*
 *	cmdserver.c -- Command Server for CANCam
 *
 *	(C) Copyright 2003, 2004, 2005 Feith Sensor to Image GmbH
 *
 * Version 1.0.1  15.05.2003
 *	Handshake function 63 ans 64
 *
 * Version 1.0.2  31.07.2003
 *	functions set_outport, get_outport, set_inport and get_inport insert.
 * functions calibrate_sensor, set_calibrate_param and get_calibrate_param insert.
 *
 * Version 1.0.3  08.08.2003
 *	insert www interface to get an image from the cgi.
 * insert function set integration time, get integration time
 *
 * Version 1.0.4 15.01.2004
 * improved communication
 * insert fuctions for windowing, subsampling, LUT, Snapshots
 * supports color sensor 
 *
 * Version 1.1.0  16.01.2004
 *	insert function set_gain, get_gain, set_row_delay, get_row_delay, enable_subsampling,
 * disable_subsampling, initialize_snapshot, disable_snapshot, set_snapshot_trigger_line,
 * get_sensor_type, set_lut, get_lut
 * insert sensor LM9648 (RGB)
 *
 * Version 1.1.1  02.03.2004
 * insert function set integration time get integration time
 * previous function set integration time get integration time renamed to set_itime and get_itime
 * grab_image_half replaced with grab_image, but fuctionality is identically
 * insert function grab_image_on_trigger
 * insert function grab_sync_image_on_trigger
 * insert function set_fdelay, get_fdelay
*
 * Version 1.1.2  02.12.2004
 * insert function config_io_ports set_io_port get_io_port
 *
 * Version 1.2.0  05.02.2005
 * supports hardware-version 1 and 2
 * supports only kernel 2.4
*/
/*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "cancam.h"
#include "jpeglib.h"


#ifndef EMBED
#include <sys/select.h>
#endif

extern unsigned char write_JPEG_file (char * filename, int quality, unsigned char *ppixel, int i_width, int i_height, J_COLOR_SPACE jcs);
extern unsigned char save_bmp(char *fname,unsigned short w,unsigned short h,unsigned char *ppixel);

#define CANCAMDRV_CMD_START_TIMER   2
#define CANCAMDRV_CMD_STOP_TIMER   3
#define DRIVERNAME_0 "/dev/cancamtimer"                // Device-Name


unsigned char cancam_cmd,cancam_len;
unsigned long cancam_dat[32];
unsigned char debug_info = 1;
static unsigned char *phelp, *temppix;
unsigned long ptr_adr;
const unsigned char BUFLEN = 128;
unsigned char buffer[128];
unsigned long internet_cnt;
static sigset_t sig_mask;
static unsigned char cancam_init = 0;
static unsigned char in_action = 0;

static int fd1;

char cmdserver_version[] ="Version 1.2.0  05.02.2005";

int creatpidfile(void)
{
	FILE	*f;
	pid_t	pid;
	char	*pidfile = "/var/run/cmdserver.pid";

	pid = getpid();
	if ((f = fopen(pidfile, "w")) == NULL) {
		printf("CMDSERVER: failed to open(%s), errno=%d\n",
			pidfile, errno);
		return(-1);
	}
	fprintf(f, "%d\n", pid);
	fclose(f);
	return(0);
}

void save_internet_image(void)
{
	unsigned char error;
	unsigned char *ppixel;
	unsigned long ptr_adr;
	int lw,lh;
	char l_str[100];
	unsigned short x1,y1,x2,y2;

	if(cancam_init == 0)
	{
		printf("init cancam\n");
	   error = init_cancam((int)getpid(),NULL, 0x46454954);

		if(error == 0)
		{
	      x1 = (1288/2) - (1280/2);
			x2 = (1288/2) + (1280/2) - 1;
			y1 = (1024/2) - (1024/2);
			y2 = (1024/2) + (1024/2) - 1;
			error = set_video_window(x1,y1,x2,y2);
		   usleep(100000);
		}
	}
	printf("\nSave internet image ...\n");

   error = grab_image(0);
	get_pixel_ptr(0,&ptr_adr);
	ppixel = (unsigned char *) ptr_adr;
	error = get_video_window(&x1,&y1,&x2,&y2);
	lw = x2 - x1 + 1;
	lh = y2 - y1 + 1;

	printf("\nw = %d h = %d\n",lw,lh);

	sprintf(l_str,"/home/httpd/image_s/%ld.jpg",internet_cnt);
	unlink(l_str);
	srand(time(NULL));
	internet_cnt = rand() + time(NULL);
   sprintf(l_str,"/home/httpd/image_s/%ld.jpg",internet_cnt);
	error = write_JPEG_file (l_str,75, ppixel,lw,lh,JCS_GRAYSCALE);
	if(error)
	{
		printf("\nError %d save image.\n",error);
		sleep(3);

	}
	unlink("/home/httpd/image_s/status.log");

	if(cancam_init == 0)
	{
		printf("close cancam\n");
	   error = close_cancam();
	}
	printf("\nSave internet image ready\n");
}


unsigned long GetTickCount(void)
{
	unsigned long s_t,u_t,ms;
	struct timeval tv;

	gettimeofday(&tv,NULL);
	s_t = tv.tv_sec * 1000;
	u_t = tv.tv_usec / 1000;
	ms = s_t + u_t;
	return abs(ms);
}


void sig_usr2(int signr)
{
	if(in_action == 0)
	{
		save_internet_image();
	}
}

void print_cmd(unsigned char *buf)
{
	unsigned char *bptr,i;

	bptr = buf;

	cancam_cmd = *bptr++;
	cancam_len = *bptr++;

	if(cancam_len > 32)
		return;

	if(debug_info)
	{
		printf("\nCommando = %d\n",cancam_cmd);
		printf("Param length = %d\n",cancam_len);
	}
   for (i = 0; i < cancam_len; i++)
   {
		unsigned char h1,h2,h3,h4;

		h4 = *bptr++;
		h3 = *bptr++;
		h2 = *bptr++;
		h1 = *bptr++;

		cancam_dat[i] = (h1 & 0xff) + ((h2 & 0xff) << 8) + ((h3 & 0xff) << 16) + ((h4 & 0xff) << 24);
		if(debug_info)
			printf("Param %d = %08lX\n",i+1,cancam_dat[i]);
	}
}

unsigned char make_send_param(unsigned char *buf)
{
	unsigned char *bptr,i,cnt;

	cnt = 0;

	bptr = buf;
	*bptr++ = cancam_len;
	cnt++;
   for (i = 0; i < cancam_len; i++)
   {
		unsigned char h1,h2,h3,h4;

		*bptr++ = h1  = ((cancam_dat[i] >> 24) & 0xff);
		*bptr++ = h2 = ((cancam_dat[i] >> 16) & 0xff);
		*bptr++ = h3 = ((cancam_dat[i] >> 8) & 0xff);
		*bptr++ = h4 = (cancam_dat[i] & 0xff);
		cnt += 4;
		if(debug_info)
			printf("Receive Param %d = %08lX\n",i+1,cancam_dat[i]);
	}
	return(cnt);
}

void usage(void)
{
	printf("usage: cmdserver [-h] [port]\n");
	printf("\th -> Show this message\n");
	printf("\tdefault port = 4711\n");
	exit(0);
}


int main(int argc, char *argv[])
{
  int len,i1;
  static int server_sockfd;
  int server_len;
  int keep_running = 1;
  struct sockaddr_in server_address;
  struct sigaction sa;
  unsigned char *ptr,i,help, mode, vsub, hsub, vavg, havg, trigger, shutter, reset, level;
  int k,isize=0;
  unsigned short x1,y1,x2,y2,port;
  unsigned char error,error_init,error_grab = 0;
  unsigned short itime, delay,shelp;
  int readsocks;
  int lw,lh,j,ii;

  int cli_length;
  struct sockaddr_in cli_addr;
  struct hostent *gethostbyaddr();
  unsigned char init_flag = 0;
	fd_set socks;
  struct timeval timeout;  /* Timeout for select */
	unsigned char help1,help2, gain0, gain1, gain2, gain3;
	char l_str[50];
	unsigned long lhelp, exptime;
  int lstart, lsize=0;
  static unsigned char exit_app = 0;
  
  HW_CONFIG hardware_info;


	switch (getopt(argc, argv, "h"))
	{
		case 'h':
			usage();
			break;
	}

	if(argc == 2)
	{
		port = atoi(&argv[1][0]);
		if(port == 0)
			port = 4711;
	}
	else
		port = 4711;

	printf("init cancam\n");
	get_hardwareinfo(&hardware_info);
	
   error_init = init_cancam((int)getpid(),NULL, 0x46454954);

	if(error_init == 0)
	{
      x1 = (1288/2) - (1024/2);
		x2 = (1288/2) + (1024/2) - 1;
		y1 = (1024/2) - (1024/2);
		y2 = (1024/2) + (1024/2) - 1;
		error = set_video_window(x1,y1,x2,y2);
		init_flag = 1;
		cancam_init = 1;
		
		set_memory_size(9*1024*1024);


	}
	else
	{
		printf("error %d init cancam\n",error_init);
		exit(0);
	}
	printf("\nCommand Server %s\n",cmdserver_version);
   printf("Port: %d\n",port);

  server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = INADDR_ANY;//htonl(INADDR_ANY);
  server_address.sin_port = htons(port);
  server_len = sizeof(server_address);
  bind(server_sockfd, (struct sockaddr *)&server_address, server_len);


  len = sizeof(k);
  k = 0x2000;
  setsockopt(server_sockfd,SOL_SOCKET,SO_SNDBUF,&k,len);

	creatpidfile();

   sigemptyset(&sig_mask);
   sigaddset(&sig_mask, SIGUSR2);

	sa.sa_handler = sig_usr2;
   memset(&sa.sa_mask, 0, sizeof(sa.sa_mask));
	sa.sa_mask = sig_mask;
	sa.sa_flags = SA_RESTART;
	sa.sa_restorer = 0;
	sigaction(SIGUSR2, &sa, NULL);

// **************************************************
  while(keep_running)
  {
	if(debug_info)
		 printf("\nCommand Server waiting\n");

	  cli_length = sizeof(cli_addr);
    len = recvfrom(server_sockfd, buffer, BUFLEN-1, 0,
		 (struct sockaddr *) &cli_addr, &cli_length);

	if(debug_info)
		 printf("[Message from %s]\n",inet_ntoa(cli_addr.sin_addr));

	 if(len)
	 {
	 	print_cmd(buffer);

		in_action = 1;
	 	switch(cancam_cmd)
	 	{
			case 0:	// echo test
						len = make_send_param(buffer);
					   cli_length = sizeof(cli_addr);
 				      if(sendto(server_sockfd, &buffer[0], BUFLEN-1, 0,(struct sockaddr *) &cli_addr, cli_length) == -1)
							printf("error sendto\n");
						break;

			case 1:	// get invert muster
						ptr = (unsigned char *)cancam_dat;
						for (i = 0; i < (cancam_len * 4); i++)
						{
							help = *ptr;

⌨️ 快捷键说明

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