又一个Perl的程序(原创)

#!/usr/bin/perl -W

use Tk;
use Tk::PathEntry;
use Tk::message;
use DirHandle;
use Cwd;

my $srcPath = cwd();
my $dstPath = cwd();

 $mw = MainWindow -> new(-title => "Auto Red Eye Removal");

 $srcFrame = $mw -> Frame(-width => 600, -height => 10);
 $srcFrame -> Label(-width => 10) -> pack(-side => 'left');
 $srcFrame -> Label(-text => "源文件路径:") -> pack(-side => 'left');
 $srcEntry = $srcFrame -> PathEntry(-textvariable => /$srcPath, -width => 70);
 $srcFrame -> Label(-width => 10) -> pack(-side => 'right');
 $srcEntry -> pack(-side => 'right');

 $dstFrame = $mw -> Frame(-width => 600, -height => 10);
 $dstFrame -> Label(-width => 10) -> pack(-side => 'left');
 $dstFrame -> Label(-text => "目标文件路径:") -> pack(-side => 'left');
 $dstEntry = $dstFrame -> PathEntry(-textvariable => /$dstPath, , -width => 70);
 $dstFrame -> Label(-width => 10) -> pack(-side => 'right');
 $dstEntry -> pack(-side => 'right');

 $cmdFrame = $mw -> Frame(-width => 600, -height => 35);
 $cmdFrame -> Label(-width => 25) -> pack(-fill => 'x', -side => 'left');
 $cmdFrame -> Label(-width => 25) -> pack(-fill => 'x', -side => 'right');
 $runButton = $cmdFrame -> Button(-text => "Run", -height => 1, -width => 8,
     -command => /&Run);
 $exitButton = $cmdFrame -> Button(-text => "Exit", -height => 1, -width => 8,
     -command => sub{ exit }, -default => 'active');
 $runButton -> pack(-side => 'left');
 $exitButton -> pack(-side => 'right');

 $outputFrame = $mw -> Frame(-width => 600, -height => 400);
 $outputText = $outputFrame -> Text(-width => 80, -height => 25, -wrap => 'word',
                 -background => 'gray', -font => 'normal');
 $outputText -> pack(-side => 'left', -fill => 'y');

 $scrollBar = $outputFrame -> Scrollbar(-command => ['yview' => $outputText]);
 $outputText -> configure(-yscrollcommand => ['set' => $scrollBar]);
 $scrollBar -> pack(-side => 'right', -fill => 'y');

 $srcFrame -> pack(-side => 'top', -fill => 'both');
 $dstFrame -> pack(-side => 'top', -fill => 'both');
 $cmdFrame -> pack(-side => 'top', -fill => 'both');
 $outputFrame -> pack(-side => 'top');
 $mw -> resizable(0, 0);

MainLoop;

sub Run() {
  my $src_path = $srcPath."/";
  my $dst_path = $dstPath."/";

  if($src_path eq '' || $dst_path eq '') {
    $mw -> messageBox(-type => 'OK', -title => "Error",
       -message => "请输入目标文件路径或源文件路径!!");
    die;
  }
  elsif ($src_path eq ' ' || $dst_path eq ' ') {
    $mw -> messageBox(-type => 'OK', -title => "Error",
       -message => "请输入目标文件路径或源文件路径!!");
    die;
  }
 
  $src_path = &Trim($src_path);
  $dst_path = &Trim($dst_path);
 
  my @arrayFiles;
  if(&isFolder($src_path)) {
   @arrayFiles = &getJPGFiles($src_path);
  }
  else {
  $mw -> messageBox(-type => 'OK', -title => "Error",
     -message => "源文件夹路径不存在,请输入正确的文件夹路径!");
  die;
  }
 
  if(&isFolder($dst_path)) {
   
  }
  else {
   $mw -> messageBox(-type => 'OK', -title => "Error",
     -message => "目标文件夹路径不存在,请输入正确的文件夹路径!");
  die;
  }
 
  foreach my $file (@arrayFiles) {
   $outputText -> insert('end', "Source file is: ".$src_path.$file."/n".
               "Dest file is: ".$dst_path.$file."/n");
  $outputText -> insert('end', "Current redeye is detected: ");
  
   $command = "demo $src_path"."$file "."$dst_path"."$file 1 1 0";
   eval {
    open(FD, "$command 2>&1 |");
    while($line = <FD>) {
     $outputText -> insert('end', $line);
    }
    $outputText -> insert('end', "/n");
   }
 }
 
  close(FD);
  die;
}

sub Trim() {
 $tmp = $_[0];
 
 for($tmp) {
  s/^/s+//;
  s//s+$//;
 }
 return $tmp;
}

sub getJPGFiles() {
 my $tmppath = $_[0];
 
 $tmppath = $tmppath."/";
 my $dh = new DirHandle("$tmppath");
 
 @files = DirHandle::read($dh);
 undef $dh;
 
 @files = sort(@files);
 shift(@files);
 shift(@files);
 
 if(@files == 0) {
  $mw -> messageBox(-type => 'OK', -title => "Error",
     -message => "当前文件夹为空,请检查!");
  die;
 }
 
 my @tempfiles;

 foreach my $f (@files) {
  if($f =~ /(.jpg)$/ || $f =~ /(.JPG)$/) {
   push @tempfiles, $f;
  }
 }
 
 if(@tempfiles == 0) {
  $mw -> messageBox(-type => 'OK', -title => "Error",
     -message => "当前文件夹中不存在jpg文件!");
  die;
 }
 
 @files = @tempfiles;
 return @files;
}

sub isFolder() {
 $tmp = $_[0];
 
 my $dh = new DirHandle("$tmp");
 
 if(!(defined $dh)) {
  return 0;
 }
 
 undef $dh;
 return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值