lib/XUL/Image.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« script/xul2img.pl ↑ Summary lib/XUL/Image/PPT.pm »
1 package XUL::Image;
2  
3 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Win32::GuiTest qw(
Subroutine BEGIN: called CC=1 SCAR=2.6
8 FindWindowLike GetWindowText
9 SetForegroundWindow SendKeys
10 0 );
11 use Clipboard;
12 use File::Spec;
13 use Image::Magick;
14  
15 our $VERSION = '0.03';
16  
17 has 'title' => (is => 'ro', isa => 'Str', default => 'Mozilla');
18 has 'count' => (is => 'ro', isa => 'Int', required => 1);
19 has 'delay' => (is => 'rw', isa => 'Int', default => 1);
20 has 'outdir' => (is => 'rw', isa => 'Str', default => 'xul_img');
21  
22 sub go {
23 my $self = shift;
24 my %args = @_;
25 my @windows =
26 grep { index(GetWindowText($_), $self->title) >= 0 } FindWindowLike(0, 'Mozilla');
27 if (!@windows) {
28 die "error: Can't find window titled '", $self->title, "'.";
29 }
30 if (@windows > 1){
31 warn "warning: multiple mozilla windows found, will use the first one.\n";
32 }
33 SetForegroundWindow($windows[0]);
34 sleep($self->delay);
35  
36 SendKeys("{F11}");
37 sleep($self->delay);
38  
39 if ($args{reset}) {
40 SendKeys("{HOME}");
41 sleep($self->delay);
42 }
43  
44 mkdir $self->outdir if !-d $self->outdir;
45 my @files;
46 for (1..$self->count) {
47 SendKeys("{PRTSCR}{DOWN}");
48 sleep($self->delay);
49 my $fbase = sprintf("%03d", $_) . ".png";
50 push @files, $fbase;
51 my $outfile = File::Spec->catfile($self->outdir, $fbase);
52 warn "generating $outfile...\n";
53 my $imdata = Clipboard->paste;
54 $self->crop_img($imdata, $outfile);
55 }
56 my $listing = File::Spec->catfile($self->outdir, 'listing.txt');
57 open my $fh, "> $listing"
58 or die "Cannot open $listing for writing: $!\n";
59 print $fh join("\n", @files);
60 close $fh;
61 warn "$listing generated.\n";
62 SendKeys("{F11}");
63 }
64  
65 sub crop_img {
66 my ($self, $imdata, $outfile) = @_;
67 my $image = Image::Magick->new;
68 $image->BlobToImage($imdata);
69 my $ret = $image->Crop( geometry => '+0+33' );
70 warn $ret if $ret;
71 $ret = $image->Trim;
72 warn $ret if $ret;
73 $ret = $image->Write($outfile);
74 warn $ret if $ret;
75 }
76  
77 1;
78 __END__
« script/xul2img.pl ↑ Summary lib/XUL/Image/PPT.pm »