<?xml version="1.0" encoding="windows-1252"?>
<node id="21591" title="Webcam 0.2" created="2000-07-07 21:35:52" updated="2005-08-15 09:48:35">
<type id="1748">
sourcecode</type>
<author id="21188">
Arjen</author>
<data>
<field name="doctext">
&lt;CODE&gt;
#!/usr/bin/perl -w
use Tk;
use Tk::JPEG;
use Tk::NoteBook;
use Tk::FileSelect;
use Tk::DialogBox;
use File::Copy;
use Net::FTP;

$version = "0.2";
%options = ();

# general data
$first_in_seq = 1;

# No configurable data
$sequence = 0;
$no_config = 0;

	&amp;read_rc;
	&amp;make_stamprc;
	
# The Main screen!
	$main = MainWindow-&gt;new();
	$main-&gt;title("Webcam II upload util");
	$main-&gt;minsize(qw(250 250));

	$menu = $main-&gt;Frame(-relief=&gt;'groove',
				-borderwidth=&gt;1)-&gt;pack(-side=&gt;'top',-fill=&gt;'x');
	$file_menu = $menu-&gt;Menubutton(-text=&gt;'File',-tearoff=&gt;0)-&gt;pack(-side=&gt;'left');
		$file_menu-&gt;command(-label=&gt;'Configure',-command=&gt;sub{ &amp;configure });
		$file_menu-&gt;separator;
		$file_menu-&gt;command(-label=&gt;'Exit',-command=&gt;sub{ &amp;write_rc; exit;  });
	$help_menu = $menu-&gt;Menubutton(-text=&gt;'Help',-tearoff=&gt;0)-&gt;pack(-side=&gt;'right');
		$help_menu-&gt;command(-label=&gt;'About',-command=&gt;sub{ &amp;about });

# lets build a frame for the Picture....
	
	$picture = $main-&gt;Frame-&gt;pack(-side=&gt;'top',
				      -fill=&gt;'x',
					padx=&gt;3,
					pady=&gt;3);
	$snapshot = $picture-&gt;Button(-text=&gt;'No Picture Yet',-command=&gt;sub{ 
                     if ($sequence == 0){ &amp;take_shot } 
		})-&gt;pack(-side=&gt;'top',-fill=&gt;'x');
	if ( ($options{'pic_name'}) &amp;&amp; (-r $options{'pic_name'}) ) {
		$snap = $picture-&gt;Photo(-format=&gt;'JPEG',-file =&gt; $options{'pic_name'});
		$snapshot-&gt;configure( -image =&gt; $snap );
	}

# The buttonbar
	$single_frm = $main-&gt;Frame-&gt;pack(-side=&gt;'top',-fill=&gt;'x',padx=&gt;3,pady=&gt;3);
	$single_frm-&gt;Label(-text=&gt;'Single shot: ')-&gt;pack(-side=&gt;'left');
	$save_opts = $single_frm-&gt;Menubutton(-text=&gt;'Save',-relief=&gt;'raised',-tearoff=&gt;0)-&gt;pack(-side=&gt;'right');
	$save_local = $save_opts-&gt;command(-label=&gt;'Save local',-command=&gt;sub{ if ($sequence == 0){ &amp;save_single } });
	$save_ftp = $save_opts-&gt;command(-label=&gt;'Save on FTP',-command=&gt;sub{ print "Do FTP save\n"; },-state=&gt;'disabled');
	$take_shot = $single_frm-&gt;Button(-text=&gt;'Take',-command=&gt;sub{ if ($sequence == 0){ &amp;take_shot } })-&gt;pack(-side=&gt;'right');
	$change_text = $single_frm-&gt;Button(-text=&gt;'Change text',-command=&gt;sub{ &amp;change_text })-&gt;pack(-side=&gt;'right');

	$seq_frm = $main-&gt;Frame-&gt;pack(-side=&gt;'top',-fill=&gt;'x',padx=&gt;3,pady=&gt;3);
	$seq_frm-&gt;Label(-text=&gt;'FTP sequence: ')-&gt;pack(-side=&gt;'left');
	$stop_seq = $seq_frm-&gt;Button(-text=&gt;'Stop',-command=&gt;sub{ $start_seq-&gt;configure(-state=&gt;'normal'); 
						      $sequence = 0; $first_in_seq = 1; 
						    })-&gt;pack(-side=&gt;'right');
	$start_seq = $seq_frm-&gt;Button(-text=&gt;'Start',-command=&gt;sub{ $start_seq-&gt;configure(-state=&gt;'disabled'); 
								    $sequence = 1; 
								    &amp;take_continuos;})-&gt;pack(-side=&gt;'right');
	
	if ($no_config) { 
				$snapshot-&gt;configure(-state=&gt;'disabled');
				$save_local-&gt;configure(-state=&gt;'disabled');
				$save_ftp-&gt;configure(-state=&gt;'disabled');
				$take_shot-&gt;configure(-state=&gt;'disabled');
				$change_text-&gt;configure(-state=&gt;'disabled');
				$stop_seq-&gt;configure(-state=&gt;'disabled');
				$start_seq-&gt;configure(-state=&gt;'disabled');
				&amp;configure; 
			}
	MainLoop;

	&amp;write_rc;

sub read_rc {
	if ( -r "$ENV{HOME}/.webcamII" ) {
		open(RCFILE, "$ENV{HOME}/.webcamII") or die "Can't open/create rc file!\n$ENV{HOME}/.webcamII\n\t$!\n";
		while (&lt;RCFILE&gt;) {
			chomp;
			s/#.*//;
			s/^\s+//;
			s/\s+$//;
			next unless length;
			($opt,$val) = split(/\s*=\s*/,$_,2);
			$options{$opt} = $val;
		}
		close(RCFILE);
		if (! $options{"pic_name"} ) { $options{"pic_name"} = "$ENV{HOME}/webcamII.jpg"  };
		if (! $options{"ftp_server"} ) { if ($main) { $start_seq-&gt;configure(-state=&gt;'disabled'); $no_config = 1} }
		if (! $options{"dev_name"} ) { warn "No device was configured!"; $no_config = 1; };
	} else {
		$no_config = 1;
	}
}

sub write_rc {
	open(RCFILE, "&gt;$ENV{HOME}/.webcamII") or die "Can't open/create rc file!\n$ENV{HOME}/.webcamII\n\t$!\n";
	foreach $key (keys %options) {
		print RCFILE "$key = $options{$key}\n";
	}
	close(RCFILE);
}

sub about {
	$about_win = $main-&gt;Toplevel(-title=&gt;"About webcamII.pl");
	$about_win-&gt;Label(-text=&gt;"webcamII.pl\n\nAuthor: Arjen Wiersma &lt;arjen\@wiersma.org&gt;\nVersion:" .
                                 "$version\n\ncomments:\nThis program was made to be a wrapper to some\n" . 
                                 "utilities i used to take and modify pictures from my webcam.\n\n" . 
                                 "I would like to have comments and ideas as to improving the program.\n")-&gt;pack(-side=&gt;'top');
	$about_win-&gt;Button(-text=&gt;"Ok, I've seen enough",-command=&gt;[$about_win=&gt;'destroy'])-&gt;pack(-side=&gt;'top');
}

sub change_text {
	$top = $options{'top_string'};
	$exect = $options{'exec_top'};
	$low = $options{'low_string'};
	$execl = $options{'exec_low'};
	$change_win = $main-&gt;Toplevel(-title=&gt;"Change picture text");
	$top_text = $change_win-&gt;Frame-&gt;pack(-side=&gt;'top',-fill=&gt;'x');
	$top_text-&gt;Label(-text=&gt;'Top text: ')-&gt;pack(-side=&gt;'left');
	$top_text-&gt;Checkbutton(-text=&gt;'execute',-variable=&gt;\$exect)-&gt;pack(-side=&gt;'right');
	$top_text-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$top)-&gt;pack(-side=&gt;'right');
        $low_text = $change_win-&gt;Frame-&gt;pack(-side=&gt;'top',-fill=&gt;'x');
	$low_text-&gt;Label(-text=&gt;'Low text: ')-&gt;pack(-side=&gt;'left');
	$low_text-&gt;Checkbutton(-text=&gt;'execute',-variable=&gt;\$execl)-&gt;pack(-side=&gt;'right');
	$low_text-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$low)-&gt;pack(-side=&gt;'right');
	$buttons = $change_win-&gt;Frame(-relief=&gt;'groove')-&gt;pack(-side=&gt;'top',-fill=&gt;'x');
	$buttons-&gt;Button(-text=&gt;'Save',-command=&gt;sub{   $options{'top_string'} = $top;
							$options{'exec_top'} = $exect;
							$options{'low_string'} = $low;
							$options{'exec_low'} = $execl;
							&amp;make_stamprc; 
						    })-&gt;pack(-side=&gt;'left');
	$buttons-&gt;Button(-text=&gt;'Close',-command=&gt;[$change_win=&gt;'destroy'])-&gt;pack(-side=&gt;'right');
}

sub configure {
	$con_win = $main-&gt;Toplevel(-title=&gt;"webcamII.pl configuration");
	
	$options = $con_win-&gt;NoteBook-&gt;pack(padx=&gt;3,pady=&gt;3);
# General configuration
	$webcamII_page = $options-&gt;add("webcamII",-label=&gt;'webcamII');

	$dev_lbl = $webcamII_page-&gt;Label(-text=&gt;'Device: ');
	$dev_ent = $webcamII_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'dev_name'});
	Tk::grid($dev_lbl,-column=&gt;'0',-row=&gt;'0',-sticky=&gt;'e');
	Tk::grid($dev_ent,-column=&gt;'1',-row=&gt;'0');

	$pic_lbl = $webcamII_page-&gt;Label(-text=&gt;'Picture: ');
	$pic_ent = $webcamII_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'pic_name'});
        Tk::grid($pic_lbl,-column=&gt;'0',-row=&gt;'1',-sticky=&gt;'e');
	Tk::grid($pic_ent,-column=&gt;'1',-row=&gt;'1');

# FTP configuration
	$FTP_page = $options-&gt;add("FTP",-label=&gt;'FTP');
	$FTP_serv_lbl = $FTP_page-&gt;Label(-text=&gt;"Server: ");
	$FTP_serv_ent = $FTP_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'ftp_server'});
	Tk::grid($FTP_serv_lbl,-column=&gt;'0',-row=&gt;'0',-sticky=&gt;'e');
	Tk::grid($FTP_serv_ent,-column=&gt;'1',-row=&gt;'0');
        
	$FTP_user_lbl = $FTP_page-&gt;Label(-text=&gt;"Username: ");
	$FTP_user_ent = $FTP_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'ftp_user'});
	Tk::grid($FTP_user_lbl,-column=&gt;'0',-row=&gt;'1',-sticky=&gt;'e');
	Tk::grid($FTP_user_ent,-column=&gt;'1',-row=&gt;'1');

	$FTP_pass_lbl = $FTP_page-&gt;Label(-text=&gt;"Password: ");
	$FTP_pass_ent = $FTP_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'ftp_pass'});
        Tk::grid($FTP_pass_lbl,-column=&gt;'0',-row=&gt;'2',-sticky=&gt;'e');
	Tk::grid($FTP_pass_ent,-column=&gt;'1',-row=&gt;'2');

	$FTP_cwd_lbl = $FTP_page-&gt;Label(-text=&gt;"Directory: ");
	$FTP_cwd_ent = $FTP_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'ftp_cwd'});
	Tk::grid($FTP_cwd_lbl,-column=&gt;'0',-row=&gt;'3',-sticky=&gt;'e');
	Tk::grid($FTP_cwd_ent,-column=&gt;'1',-row=&gt;'3');
				
	$FTP_delay_lbl = $FTP_page-&gt;Label(-text=&gt;"Delay: ")-&gt;pack(-side=&gt;'left');
	$FTP_delay_ent = $FTP_page-&gt;Entry(-width=&gt;5,-textvariable=&gt;\$options{'delay_time'});
        Tk::grid($FTP_delay_lbl,-column=&gt;'0',-row=&gt;'4',-sticky=&gt;'e');
	Tk::grid($FTP_delay_ent,-column=&gt;'1',-row=&gt;'4',-sticky=&gt;'w');
# Stamp configuration
	$stamp_page = $options-&gt;add("Stamp",-label=&gt;'Stamp');
	$top_lbl = $stamp_page-&gt;Label(-text=&gt;"Top string: ");
	$top_ent = $stamp_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'top_string'});
	$top_exec = $stamp_page-&gt;Checkbutton(-text=&gt;'execute',-variable=&gt;\$options{'exec_top'});
        Tk::grid($top_lbl,-column=&gt;'0',-row=&gt;'0',-sticky=&gt;'e');
	Tk::grid($top_ent,-column=&gt;'1',-row=&gt;'0',-sticky=&gt;'w');
	Tk::grid($top_exec,-column=&gt;'2',-row=&gt;'0',-sticky=&gt;'w');

        $low_lbl = $stamp_page-&gt;Label(-text=&gt;"Low string: ");
	$low_ent = $stamp_page-&gt;Entry(-width=&gt;20,-textvariable=&gt;\$options{'low_string'});
	$low_exec = $stamp_page-&gt;Checkbutton(-text=&gt;'execute',-variable=&gt;\$options{'exec_low'});
	Tk::grid($low_lbl,-column=&gt;'0',-row=&gt;'1',-sticky=&gt;'e');
	Tk::grid($low_ent,-column=&gt;'1',-row=&gt;'1',-sticky=&gt;'w');
	Tk::grid($low_exec,-column=&gt;'2',-row=&gt;'1',-sticky=&gt;'w');
	$con_win-&gt;Button(-text=&gt;'Close',-command=&gt;[$con_win=&gt;'destroy'])-&gt;pack(-side=&gt;'right');
}

sub take_shot {
	&amp;make_stamprc;
	if (! -r "/tmp/webcamIIrc") { &amp;make_stamprc }
# Take a shot from the camera
	system("vidcat -d $options{'dev_name'} -f jpeg &gt; /tmp/webcamII.jpg") == 0 
		or die "vidcat failed: $1\n";
	system("stamp -r /tmp/webcamIIrc") == 0 or die "stamp failed: $!\n";
	$snap = $picture-&gt;Photo(-format=&gt;'JPEG',-file =&gt; $options{'pic_name'});
	$snapshot-&gt;configure(-image=&gt;$snap);
}

sub take_continuos {
	&amp;make_stamprc;
	if (! -r "/tmp/webcamIIrc") { &amp;make_stamprc }
	return unless $sequence;
	if ($sequence) {
		system("vidcat -d $options{'dev_name'} -f jpeg &gt; /tmp/webcamII.jpg") == 0 or die "vidcat failed: $1\n";
        	system("stamp -r /tmp/webcamIIrc") == 0 or die "stamp failed: $!\n";
		$snap = $picture-&gt;Photo(-format=&gt;'JPEG',-file =&gt; $options{'pic_name'});
	        $snapshot-&gt;configure(-image=&gt;$snap);
		if (! &amp;upload ) { 
					$sequence = 0;
					$start_seq-&gt;configure(-state=&gt;'normal');
					exit;
				}
		$main-&gt;after(($options{'delay_time'} * 1000),\&amp;take_continuos);
	}
}

sub save_single {
	@types = ( ["Jpeg files",['.jpg']] );
	$file= $main-&gt;getSaveFile(-filetypes=&gt;\@types,
				  -initialfile=&gt;'snap',
				  -defaultextension=&gt;'.jpg');
	if (defined $file) {
		copy($options{'pic_name'},$file) == 1 or warn "Couldn't copy!\n\t$!\n";
	}
}

sub upload {
	$ftp = Net::FTP-&gt;new($options{'ftp_server'},-timeout=&gt;60) or return 1;
	$ftp-&gt;login($options{'ftp_user'},$options{'ftp_pass'}) or die;
	if ( $options{'ftp_cwd'} ) { $ftp-&gt;cwd($options{'ftp_cwd'}) or die; }
	$ftp-&gt;type('I');
	$ftp-&gt;put($options{'pic_name'}) or die;
	$ftp-&gt;quit or return 1;
}

sub make_stamprc {
# make a RC file for STAMP.
	open (STAMP, "&gt;/tmp/webcamIIrc");
	print STAMP "infile\t/tmp/webcamII.jpg\n";
	if ($options{'pic_name'}) { print STAMP "outfile\t$options{'pic_name'}\n"; }
	print STAMP "use3d\t1\n";
	print STAMP "rotate\t0\n";
	if ($options{'low_string'}) { print STAMP "lowerstring\t$options{'low_string'}\n"; }
	if ($options{'exec_low'}  ) { print STAMP "lstringexec\t$options{'exec_low'}\n"; }
	if ($options{'top_string'}) { print STAMP "upperstring\t$options{'top_string'}\n"; }
	if ($options{'exec_top'}  ) { print STAMP "ustringexec\t$options{'exec_top'}\n"; }
	print STAMP "upperfont\t/usr/local/share/stamp/fonts/computer.fnt\n";
	print STAMP "lowerfont\t/usr/local/share/stamp/fonts/computer.fnt\n";
	print STAMP "redfore\t255\n";
	print STAMP "greenfore\t238\n";
	print STAMP "bluefore\t245\n";
	print STAMP "redback\t20\n";
	print STAMP "blueback\t115\n";
	print STAMP "greenback\t6\n";
	print STAMP "shaderate\t10\n";
	print STAMP "usecolors\t1\n";
	close(STAMP);
}
&lt;/CODE&gt;</field>
<field name="codedescription">
I created this program when I still lived back in the netherlands. I had a Webcam-II hooked up to my Linux machine and at that time there were not many good programs for webcams, so I created my own.&lt;BR&gt;
&lt;BR&gt;
This program uses Stamp and vidcat as utilities to capture and textstamp the picture.&lt;BR&gt;
&lt;BR&gt;
&lt;A HREF="http://www.wiersma.org/webcamii.html"&gt;My original page&lt;/A&gt; for links to the above programs.&lt;BR&gt;
&lt;BR&gt;
Cheers</field>
<field name="codecategory">
GUI Programming</field>
<field name="codeauthor">
Arjen Wiersma (arjen@wiersma.org)</field>
</data>
</node>
