File Coverage

blib/lib/XUL/Image.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package XUL::Image;
2              
3 1     1   32600 use 5.006001;
  1         5  
  1         41  
4 1     1   481 use Moose;
  0            
  0            
5              
6             use Time::HiRes 'sleep';
7             use Win32::GuiTest qw(
8             FindWindowLike GetWindowText
9             SetForegroundWindow SendKeys
10             );
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__
79              
80             =head1 NAME
81              
82             XUL::Image - Converting XUL slides to Images
83              
84             =head1 DESCRIPTION
85            
86             use XUL::Image;
87              
88             $obj = XUL::Image->new(count => 32);
89            
90             $obj->go;
91              
92             =head1 METHODS
93              
94             =head2 new(%option)
95              
96             =over
97              
98             =item * title => $title
99              
100             This option gives the window title of Mozilla FireFox and 'Mozilla' is default
101              
102             =item * count => $count
103              
104             This option gives number of slides to be shooted, which is required
105              
106             =item * delay => $delay
107              
108             This option gives the delay between each mechanical operation, and 1 second is default
109              
110             =item * out_dir => $out_dir
111              
112             This option gives a directory under which all the intermediate image files will be saved. and 'xul_img' is default
113              
114             =back
115              
116             =head2 go()
117              
118             invoke this method to start converting xul slides into images, before excuting make sure xul files has been opened by
119             Mozilla FireFox, not in full screen mode or minimized.
120              
121             =head1 SEE ALSO
122              
123             L<XUL::Image::PPT>
124              
125             =head1 AUTHOR
126              
127             Sal Zhong E<lt>zhongxiang721@gmail.comE<gt>
128              
129             =head1 COPYRIGHT
130              
131             Copyright (c) 2006~2007 Sal Zhong. All rights reserved.
132              
133             This library is free software; you can redistribute it
134             and/or modify it under the same terms as perl itself.