📄 webcam.c
字号:
return (2); } else if (childpid > 0) exit(0); /* parent */ } /* read config */ if (config_file) { cfg_parse_file(config_file); } else { sprintf(filename, "%s/%s", getenv("HOME"), ".camErc"); memset(filename, '\0', sizeof(filename)); snprintf(filename, sizeof(filename)-1, "%s/%s", getenv("HOME"), ".camErc"); cfg_parse_file(filename); } if (NULL != (val = cfg_get_str("ftp", "host"))) ftp_host = val; if (NULL != (val = cfg_get_str("ftp", "user"))) ftp_user = val; if (NULL != (val = cfg_get_str("ftp", "pass"))) ftp_pass = val; if (NULL != (val = cfg_get_str("ftp", "dir"))) ftp_dir = val; if (NULL != (val = cfg_get_str("ftp", "file"))) ftp_file = val; if (NULL != (val = cfg_get_str("ftp", "tmp"))) ftp_tmp = val; if (-1 != (i = cfg_get_int("ftp", "passive"))) ftp_passive = i; if (-1 != (i = cfg_get_int("ftp", "debug"))) ftp_debug = i; if (-1 != (i = cfg_get_int("ftp", "do"))) ftp_do = i; if (-1 != (i = cfg_get_int("ftp", "upload_every"))) ftp_upload_every = i; if (-1 != (i = cfg_get_int("ftp", "keepalive"))) ftp_keepalive = i; if (-1 != (i = cfg_get_int("ftp", "timeout"))) ftp_timeout = i; if (NULL != (val = cfg_get_str("ftp", "interface"))) ftp_interface = val; if (-1 != (i = cfg_get_int("ftp", "delete_first"))) ftp_delete_first = i; if (NULL != (val = cfg_get_str("scp", "target"))) scp_target = val; if (-1 != (i = cfg_get_int("scp", "timeout"))) scp_timeout = i; if (NULL != (val = cfg_get_str("grab", "device"))) grab_device = val; if (NULL != (val = cfg_get_str("grab", "text"))) grab_text = val; if (NULL != (val = cfg_get_str("grab", "infofile"))) grab_infofile = val; if (NULL != (val = cfg_get_str("grab", "action_pre_shot"))) action_pre_shot = val; if (NULL != (val = cfg_get_str("grab", "action_post_shot"))) action_post_shot = val; if (NULL != (val = cfg_get_str("grab", "action_post_upload"))) action_post_upload = val; if (NULL != (val = cfg_get_str("grab", "archive"))) grab_archive = val; if (NULL != (val = cfg_get_str("grab", "blockfile"))) grab_blockfile = val; if (NULL != (val = cfg_get_str("grab", "uploadblockfile"))) upload_blockfile = val; if (NULL != (val = cfg_get_str("grab", "postprocess"))) grab_postprocess = val; if (NULL != (val = cfg_get_str("grab", "title_text"))) title_text = val; if (NULL != (val = cfg_get_str("grab", "archive_ext"))) archive_ext = val; if (NULL != (val = cfg_get_str("grab", "archive_thumbnails_dir"))) archive_thumbnails_dir = val; if (NULL != (val = cfg_get_str("grab", "logfile"))) logfile = val; if (NULL != (val = cfg_get_str("grab", "ttf_dir"))) ttf_dir = val; if (NULL != (val = cfg_get_str("grab", "title_font"))) title_font = val; if (NULL != (val = cfg_get_str("grab", "text_font"))) text_font = val; if (NULL != (val = cfg_get_str("grab", "temp_file"))) temp_file = val; if (NULL != (val = cfg_get_str("grab", "title_style"))) title_style_file = val; if (NULL != (val = cfg_get_str("grab", "text_style"))) text_style_file = val; if (NULL != (val = cfg_get_str("grab", "overlay_image"))) overlay_file = val; if (NULL != (val = cfg_get_str("grab", "watch_interface"))) watch_interface = val; if (NULL != (val = cfg_get_str("grab", "offline_image"))) offline_image = val; if (NULL != (val = cfg_get_str("grab", "pwc_wb_mode"))) pwc_wb_mode = val; if (-1 != (i = cfg_get_int("grab", "width"))) grab_width = i; if (-1 != (i = cfg_get_int("grab", "height"))) grab_height = i; if (-1 != (f = cfg_get_float("grab", "delay"))) grab_delay = f; if (-1 != (i = cfg_get_int("grab", "correct"))) delay_correct = 1; if (-1 != (i = cfg_get_int("grab", "percent"))) bw_percent = i; if (-1 != (i = cfg_get_int("grab", "quality"))) grab_quality = i; if (-1 != (i = cfg_get_int("grab", "input"))) grab_input = i; if (-1 != (i = cfg_get_int("grab", "norm"))) grab_norm = i; if (-1 != (i = cfg_get_int("grab", "text_r"))) text_r = i; if (-1 != (i = cfg_get_int("grab", "text_g"))) text_g = i; if (-1 != (i = cfg_get_int("grab", "text_b"))) text_b = i; if (-1 != (i = cfg_get_int("grab", "text_a"))) text_a = i; if (-1 != (i = cfg_get_int("grab", "title_r"))) title_r = i; if (-1 != (i = cfg_get_int("grab", "title_g"))) title_g = i; if (-1 != (i = cfg_get_int("grab", "title_b"))) title_b = i; if (-1 != (i = cfg_get_int("grab", "title_a"))) title_a = i; if (-1 != (i = cfg_get_int("grab", "bg_r"))) bg_r = i; if (-1 != (i = cfg_get_int("grab", "bg_g"))) bg_g = i; if (-1 != (i = cfg_get_int("grab", "bg_b"))) bg_b = i; if (-1 != (i = cfg_get_int("grab", "bg_a"))) bg_a = i; if (-1 != (i = cfg_get_int("grab", "close_dev"))) close_dev = i; if (-1 != (i = cfg_get_int("grab", "lag_reduce"))) lag_reduce = i; if (-1 != (i = cfg_get_int("grab", "overlay_x"))) overlay_x = i; if (-1 != (i = cfg_get_int("grab", "overlay_y"))) overlay_y = i; if (-1 != (i = cfg_get_int("grab", "colour"))) cam_colour = i; if (-1 != (i = cfg_get_int("grab", "brightness"))) cam_brightness = i; if (-1 != (i = cfg_get_int("grab", "contrast"))) cam_contrast = i; if (-1 != (i = cfg_get_int("grab", "hue"))) cam_hue = i; if (-1 != (i = cfg_get_int("grab", "whiteness"))) cam_whiteness = i; if (-1 != (i = cfg_get_int("grab", "framerate"))) cam_framerate = i; if (-1 != (i = cfg_get_int("grab", "crop"))) crop = i; if (-1 != (i = cfg_get_int("grab", "crop_width"))) crop_width = i; if (-1 != (i = cfg_get_int("grab", "crop_height"))) crop_height = i; if (-1 != (i = cfg_get_int("grab", "crop_x"))) crop_x = i; if (-1 != (i = cfg_get_int("grab", "crop_y"))) crop_y = i; if (-1 != (i = cfg_get_int("grab", "scale"))) scale = i; if (-1 != (i = cfg_get_int("grab", "scale_width"))) scale_width = i; if (-1 != (i = cfg_get_int("grab", "scale_height"))) scale_height = i; if (-1 != (i = cfg_get_int("grab", "archive_shot_every"))) archive_shot_every = i; if (-1 != (i = cfg_get_int("grab", "archive_subdirs"))) archive_subdirs = i; if (-1 != (i = cfg_get_int("grab", "archive_thumbnails_create"))) archive_thumbnails_create = i; if (-1 != (i = cfg_get_int("grab", "archive_thumbnails_width"))) archive_thumbnails_width = i; if (-1 != (i = cfg_get_int("grab", "archive_thumbnails_height"))) archive_thumbnails_height = i; if (-1 != (i = cfg_get_int("grab", "pwc_wb_red"))) pwc_wb_red = i; if (-1 != (i = cfg_get_int("grab", "pwc_wb_blue"))) pwc_wb_blue = i; if (-1 != (i = cfg_get_int("grab", "flip_horizontal"))) flip_horizontal = 1; if (-1 != (i = cfg_get_int("grab", "flip_vertical"))) flip_vertical = 1; if (-1 != (i = cfg_get_int("grab", "orientation"))) orientation = i; if (cam_framerate > 60) cam_framerate = 60; if (cam_framerate < 1) cam_framerate = 1; /* clear logfile */ if (logfile) { fp = fopen(logfile, "w"); if (fp) fclose(fp); } /* print config */ camlog("camE " VERSION " - (c) 1999, 2000 Gerd Knorr, Tom Gilbert\n"); camlog("grabber config: size %dx%d, input %d, norm %d, " "jpeg quality %d\n", grab_width, grab_height, grab_input, grab_norm, grab_quality); imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT); imlib_add_path_to_font_path(ttf_dir); imlib_add_path_to_font_path("."); imlib_context_set_operation(IMLIB_OP_COPY); imlib_set_cache_size(0); if (title_style_file) title_style = gib_style_new_from_ascii(title_style_file); if (text_style_file) text_style = gib_style_new_from_ascii(text_style_file); if (overlay_file) overlay_im = imlib_load_image(overlay_file); title_fn = imlib_load_font(title_font); if (!title_fn) fprintf(stderr, "can't load font %s\n", title_font); text_fn = imlib_load_font(text_font); if (!text_fn) fprintf(stderr, "can't load font %s\n", text_font); if (offline_image) { Imlib_Image image = imlib_load_image(offline_image); if (!image) { fprintf(stderr, "can't load offline image %s, ignoring\n", offline_image); offline_image = NULL; } else { imlib_context_set_image(image); imlib_free_image_and_decache(); } } /* init everything */ grab_init(); /* go! */ do { just_shot = 0; upload_successful = 1; end_shot.tv_sec = 0; end_shot.tv_usec = 0; start_shot.tv_sec = 0; start_shot.tv_usec = 0; if (((grab_blockfile && (stat(grab_blockfile, &st) == -1)) || !grab_blockfile) && check_interface(watch_interface)) { offline_done = 0; gettimeofday(&start_shot, 0); if (action_pre_shot) { camlog("running pre-shot action\n"); system(action_pre_shot); camlog("pre-shot action done\n"); } camlog("* taking shot\n"); /* Prevent camera lag... */ image = grab_one(&width, &height); imlib_context_set_image(image); if (!image) { fprintf(stderr, "no image captured\n"); exit(2); } if (crop) { if (!crop_width) crop_width = grab_width; if (!crop_height) crop_height = grab_height; tmp_image = gib_imlib_create_cropped_scaled_image(image, crop_x, crop_y, crop_width, crop_height, crop_width, crop_height, 1); gib_imlib_free_image_and_decache(image); image = tmp_image; imlib_context_set_image(image); /* Set new values for width and height, else the image's text might not show up in the correct position. */ width = crop_width; height = crop_height; } if (scale) { if (!scale_width) scale_width = grab_width; if (!scale_height) scale_height = grab_height; tmp_image = gib_imlib_create_cropped_scaled_image(image, 0, 0, scale_width, scale_height, scale_width, scale_height, 1); gib_imlib_blend_image_onto_image(tmp_image, image, 1, 0, 0, grab_width, grab_height, 0, 0, scale_width, scale_height, 1, 0, 0); gib_imlib_free_image_and_decache(image); image = tmp_image; imlib_context_set_image(image); /* Set new values for width and height, else the image's text might not show up in the correct position. */ width = scale_width; height = scale_height; } camlog("** shot taken\n"); if (action_post_shot) { camlog("running post-shot action\n"); system(action_post_shot); camlog("post-shot action done\n"); } if (flip_horizontal) { imlib_image_flip_horizontal(); } if (flip_vertical) { imlib_image_flip_vertical(); } if (orientation && orientation > 0 && orientation < 4) { imlib_image_orientate(orientation); /* Changing orientation flips width and height, so we must swap them. */ int swap_dimensions; if(orientation == 1 || orientation == 3) { swap_dimensions = height; height = width; width = swap_dimensions; } } if (overlay_im) draw_overlay(image); add_time_text(image, get_message(), width, height); save_image(image, temp_file); do_postprocess(temp_file); archive_jpeg(image); gib_imlib_free_image_and_decache(image); upload_successful = do_upload(temp_file); just_shot = 1; gettimeofday(&end_shot,0); } else if (offline_image && !offline_done && ((upload_blockfile && (stat(upload_blockfile, &st) == -1)) || !upload_blockfile) && check_interface(watch_interface)) { /* blockfile was just created */ camlog("uploading offline image\n"); offline_done = do_upload(offline_image); camlog("OFFLINE\n"); } if (!single_shot) { new_delay = timeval_float(grab_delay); if (just_shot && upload_successful) { end_shot = sub_timevals(end_shot, start_shot); if (bw_percent < 100) bw_res_change(end_shot); if (delay_correct) { new_delay = sub_timevals(new_delay, end_shot); if (float_timeval(new_delay) < 0) { new_delay.tv_sec = 0; new_delay.tv_usec = 0; } camlog("Sleeping %d secs (corrected)\n", new_delay); } else { camlog("Sleeping %d secs\n", grab_delay); } } if (upload_successful && (float_timeval(new_delay) > 0)) select( 0, 0, 0, 0, &new_delay ); } } while(!single_shot); return 0;}intexecvp_with_timeout(int timeout, char *file, char **argv){ int status, ret; signal(SIGALRM, alarm_handler); alarm(timeout); if ((childpid = fork()) < 0) { fprintf(stderr, "fork (%s)\n", strerror(errno)); exit(2); } else if (childpid == 0) { /* child */ execvp(file, argv); fprintf(stderr, "execvp %s (%s)\n", file, strerror(errno)); exit(2); } else if (childpid > 0) { /* parent */ ret = waitpid(childpid, &status, 0); alarm(0); childpid = 0; if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { camlog("exec failed for %s\n", file); return 0; } } return 1;}voidalarm_handler(int sig){ signal(sig, SIG_IGN); camlog("timeout reached, abandoning\n"); if (childpid) { kill(childpid, SIGTERM); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -