Advertisement
Guest User

anzeiger.pl

a guest
Jan 12th, 2017
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.65 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # install the necessary perl libraries.
  4. # if you want to have plate number recognition, you need to install alpr.
  5.  
  6. use utf8;
  7. use strict;
  8. use warnings;
  9. use FindBin;
  10. use File::Spec; # used to convert relative to absolute paths
  11. use Gtk2 '-init';
  12. use Gtk2::ImageView; # apt-get install libgtk2-imageview-perl
  13. #use Gtk2::Gdk;
  14. use Glib qw(TRUE FALSE);
  15. use JSON::PP qw(decode_json);
  16. use Data::Dumper;
  17. use File::Temp qw(tempdir);
  18. use File::Basename;
  19.  
  20. @ARGV == 1 or die "too few arguments (should be one image)";
  21.  
  22. my %presets = (
  23.     " kein" => { strasse => "", hausnummer => "", vorwurf => "" },
  24.     XYZStraße => {
  25.       strasse => "XYZStraße",
  26.       hausnummer => "45",
  27.       vorwurf => "Parken auf dem Rad mit Behinderung. Ich musste absteigen und mein Fahrrad um das geparkte Auto schieben."
  28.     }, 
  29. );
  30.  
  31. my $template =  '"Sehr geehrte Damen und Herren,\n\n' .
  32.         'ich erstatte Anzeige einer Verkehrsordnungswidrigkeit wegen:\n\n$state{vorwurf}\n\n' .
  33.         'Ort: $state{strasse}\nin Höhe: $state{hausnummer}\n\n' .
  34.         'Datum: $state{date}\nUhrzeit: $state{time}\n' .
  35.         'amtl. Kennzeichen: $state{kennzeichen}\nHersteller: $state{hersteller}\nFarbe: $state{farbe}\n\n' .
  36.         'Ich bitte Sie, den Halter des Fahrzeugs entsprechend zu ahnden.\n\n' .
  37.         'Zeuge: Vorname Nachname\nAnschrift: Irgendwasstraße 666, 2xxxx Hamburg\n\n' .
  38.         'Mit freundlichem Gruß,\nVorname Nachname"';
  39.  
  40. my @monthNames = ("", "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
  41.  
  42. my %state; # will hold fields like file-name, date/time, license plate number, name of location, ...
  43.  
  44. # handle commandline argument
  45. $state{file} = $ARGV[0];
  46. $state{file} = File::Spec->rel2abs($state{file}) unless (File::Spec->file_name_is_absolute($state{file}));
  47.  
  48. my $tmpdir = tempdir( CLEANUP => 1 );
  49. my $fname = basename($state{file});
  50. my $foobar = `exiftran -o $tmpdir/$fname -a $state{file}`;
  51. $state{file} = "$tmpdir/$fname";
  52.  
  53. ($state{date}, $state{time}) = getDateTime();
  54. $state{kennzeichen} = getPlateNumber();
  55.  
  56. my $window = Gtk2::Window->new();
  57.  
  58. my $layout = Gtk2::Table->new(11, 3);
  59. $layout->set_homogeneous(1 == 0);
  60.  
  61. addLabelledEntry("_Datum: ", "date", $layout, 0);
  62. addLabelledEntry("_Uhrzeit: ", "time", $layout, 1);
  63. addLabelledEntry("_Kennzeichen: ", "kennzeichen", $layout, 2);
  64. addLabelledEntry("_Hersteller: ", "hersteller", $layout, 3);
  65. addLabelledEntry("_Farbe: ", "farbe", $layout, 4);
  66.  
  67. my $label = Gtk2::Label->new_with_mnemonic("_Preset: ");
  68. $label->set_alignment(1, 0.5);
  69. $label->set_width_chars(15);
  70. $layout->attach($label, 0, 3, 5, 6, 'shrink', 'shrink', 1, 1);
  71.  
  72. my $textview = Gtk2::TextView->new();
  73. $textview->set_accepts_tab(FALSE);
  74. $textview->set_cursor_visible(TRUE);
  75. $textview->set_wrap_mode("word");
  76.  
  77. my $combobox = Gtk2::ComboBox->new_text();
  78. foreach my $key (sort {$a cmp $b} keys %presets) {
  79.     $combobox->append_text($key);
  80. }
  81. $combobox->signal_connect('changed' => sub {
  82.     my $entry = $combobox->get_active_text();
  83.     foreach my $key (keys %{$presets{$entry}}) {
  84.         $state{$key} = $presets{$entry}->{$key};
  85.         $state{"${key}Entry"}->set_text($presets{$entry}->{$key}) if (exists $state{"${key}Entry"});
  86.     }
  87.     $textview->get_buffer->set_text(eval($template));
  88. });
  89. $layout->attach($combobox, 3, 6, 5, 6, 'fill', 'shrink', 1, 1);
  90.  
  91. #addLabelledEntry("_Straße: ", "strasse", $layout, 6);
  92. #addLabelledEntry("_Hausnummer: ", "hausnummer", $layout, 7);
  93.  
  94. #$textview->set_mnemonic("t");
  95. my $scrolledWindow = Gtk2::ScrolledWindow->new();
  96. $scrolledWindow->add_with_viewport($textview);
  97. $layout->attach($scrolledWindow, 0, 6, 8, 9, 'fill', ['expand', 'fill'], 3, 3);
  98.  
  99. my $mapsButton = Gtk2::Button->new_with_mnemonic('_Maps öffnen');
  100. $mapsButton->signal_connect('clicked' => sub { openMapsLocation(); });
  101. $layout->attach($mapsButton, 0, 2, 9, 11, 'fill', 'shrink', 3, 1);
  102. my $resizeButton = Gtk2::Button->new_with_mnemonic('_Bild _kleiner');
  103. $resizeButton->signal_connect('clicked' => sub {});
  104. $layout->attach($resizeButton, 2, 4, 9, 11, 'fill', 'shrink', 1, 1);
  105. my $composeButton = Gtk2::Button->new_with_mnemonic('_E-Mail ...');
  106. $composeButton->signal_connect('clicked' => sub { composeEMail(); });
  107. $layout->attach($composeButton, 4, 6, 9, 11, 'fill', 'shrink', 1, 1);
  108.  
  109. # add our image to the window
  110. #my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_size($state{file}, 600, 800);
  111. #my $image = Gtk2::Image->new_from_pixbuf($pixbuf);
  112. my $image = Gtk2::ImageView->new();
  113. my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($state{file});
  114. $image->set_pixbuf($pixbuf);
  115. $layout->attach(Gtk2::ImageView::ScrollWin->new($image), 6, 7, 0, 10, ['expand', 'fill'], ['expand', 'fill'], 0, 0);
  116. my $imgProps = Gtk2::Label->new(
  117.     $state{file} .
  118.     ": " .
  119.     $pixbuf->get_width() .
  120.     "x" .
  121.     $pixbuf->get_height() .
  122.     ", " .
  123.     humanReadableFilesize($state{file})
  124.     );
  125. $layout->attach($imgProps, 6,7, 10, 11, 'fill', 'shrink', 1, 1);
  126.  
  127. $window->add($layout);
  128. $window->set_default_size(800, 600);
  129. $window->show_all();
  130. $window->signal_connect(delete_event => sub { Gtk2->main_quit; TRUE });
  131. Gtk2->main();
  132.  
  133. sub addLabelledEntry {
  134.     my ($labelText, $stateField, $layout, $row) = @_;
  135.     my $label = Gtk2::Label->new_with_mnemonic($labelText);
  136.     $label->set_alignment(1, 0.5);
  137. #   $label->set_single_line_mode();
  138.     $label->set_width_chars(length($labelText)+3);
  139.     my $entry = Gtk2::Entry->new();
  140.     $entry->set_text($state{$stateField} or "");
  141.     $entry->signal_connect(changed => sub { $state{$stateField} = $entry->get_text(); });
  142.     $state{"${stateField}Entry"} = $entry;
  143.     $label->set_width_chars(15);
  144.     $label->set_mnemonic_widget($entry);
  145.     $layout->attach($label, 0, 3, $row, $row + 1, 'shrink', 'shrink', 1, 1);
  146.     $layout->attach($entry, 3, 6, $row, $row + 1, 'shrink', 'shrink', 1, 1);
  147. }
  148.  
  149. # get the date and the time of the image
  150. # this could come from the image's EXIF data
  151. # or just from the filename itself (as in this implementation)
  152. sub getDateTime {
  153.     my $file = $state{file};
  154.     if ($file =~ m/IMG_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)\d\d.*\.jpg$/) {
  155.         return ("$3. $monthNames[$2] $1", "$4:$5");
  156.     } else {
  157.         die "cannot parse filename $file";
  158.     }
  159. }
  160.  
  161. sub getPlateNumber {
  162.     my $file = $state{file};
  163.     my $pnumberjson = `alpr -j -c eu -n 1 $file`;
  164.     my $resultall = decode_json($pnumberjson);
  165.     my $result = $resultall->{"results"};
  166.     if (scalar @{$result} == 1) {
  167.         my $plate = $result->[0]->{"candidates"}->[0]->{"plate"};
  168.         if ($plate =~ m/^(\w[A-Z]*)(\d\d*)$/) {
  169.             my $origin = substr $1, 0, 2;
  170.             my $rest = substr $1, 2;
  171.             $plate = "$origin-$rest-$2";
  172.         }
  173.         return $plate;
  174.     }
  175.     return "";
  176. }
  177.  
  178. sub humanReadableFilesize {
  179.     my $file = $_[0];
  180.     my $size = -s $file;
  181.     return sprintf("%.2fMB", $size / 1048576) if ($size > 1048576);
  182.     return sprintf("%.2fkB", $size / 1024) if ($size > 1024);
  183.     return $size . "B";
  184. }
  185.  
  186. sub compressImage {
  187.    
  188. }
  189.  
  190. # use a script that I found which wraps exittool for us
  191. sub openMapsLocation {
  192.     my $mapsURL = `$FindBin::Bin/exif2map.pl -f $state{file}`;
  193.     chomp $mapsURL; chomp $mapsURL;
  194. print STDERR "opening --$mapsURL--";
  195.     `firefox $mapsURL`;
  196. }
  197.  
  198. sub composeEMail {
  199.     my %args;
  200.     # your own mail-account if you have multiple in Thunderbird
  201.     $args{preselectid} = "id2";
  202.     # address to send e-mail to
  203.     $args{to} = "bussgeldstelle\@eza.hamburg.de";
  204.     # attach the file
  205.     $args{attachment} = $state{file};
  206.     $args{subject} = "Anzeige wegen Falschparkens des Kfz $state{kennzeichen} am $state{date}";
  207.     $args{body} = $textview->get_buffer->get_property('text');
  208.     my $command;
  209.     my ($key, $val);
  210.     while (($key, $val) = each %args) {
  211.         $command .= "$key='$val',";
  212.     }
  213.     print $command;
  214.     print "\n\n";
  215.     # see http://kb.mozillazine.org/Command_line_arguments_-_Thunderbird for details
  216.     `thunderbird -compose \"$command\"`;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement