diff --git a/docs/guides/java-integration.md b/docs/guides/java-integration.md index 5e589b2d3..4f9a075ed 100644 --- a/docs/guides/java-integration.md +++ b/docs/guides/java-integration.md @@ -213,8 +213,40 @@ String perlScript = Files.readString(Path.of("legacy.pl")); engine.eval(perlScript); ``` +### Repeated Execution / Batch Processing + +For processing multiple items (e.g., files), don't run a CLI script repeatedly. +Instead, use the Perl module directly and call methods in a loop: + +```java +// Load module once, call methods repeatedly +String initCode = """ + use Image::ExifTool; + our $exif = Image::ExifTool->new(); + + sub process_file { + my ($file) = @_; + return $exif->ImageInfo($file, qw(Make Model)); + } + 1; + """; + +engine.eval(initCode); + +// Now call the subroutine for each file - no recompilation needed +for (String file : files) { + engine.eval("process_file('" + file + "')"); +} +``` + +See `examples/ExifToolExample.java` and `examples/ExifToolExample.pl` for a complete +working example using Image::ExifTool. + ## Examples +- `examples/ExifToolExample.pl` - Batch image processing with Image::ExifTool +- `examples/ExifToolExample.java` - Java integration example + See also: - [Quick Start](../../QUICKSTART.md) - Basic examples - [Architecture](../reference/architecture.md) - How it works internally diff --git a/examples/ExifToolExample.java b/examples/ExifToolExample.java new file mode 100644 index 000000000..8b7ae7dfb --- /dev/null +++ b/examples/ExifToolExample.java @@ -0,0 +1,78 @@ +package examples; + +import org.perlonjava.app.cli.CompilerOptions; +import org.perlonjava.app.scriptengine.PerlLanguageProvider; +import org.perlonjava.runtime.runtimetypes.*; + +/** + * Example: Using Image::ExifTool module from Java for batch processing. + * + * For repeated execution with different files, using the Image::ExifTool + * Perl module directly is more efficient than running the CLI script + * repeatedly. The module is loaded once, then methods can be called + * for each file. + * + * Prerequisites: + * Download and extract ExifTool in the PerlOnJava root directory: + * cd PerlOnJava + * curl -LO https://exiftool.org/Image-ExifTool-13.44.tar.gz + * tar xzf Image-ExifTool-13.44.tar.gz + * + * Run the Perl version with: + * ./gradlew run --args='examples/ExifToolExample.pl' + * + * Or compile and run the Java version (requires fat jar). + */ +public class ExifToolExample { + + public static void main(String[] args) throws Exception { + // Initialize PerlOnJava + PerlLanguageProvider.resetAll(); + + // Add ExifTool lib to @INC + RuntimeArray inc = GlobalVariable.getGlobalArray("main::INC"); + RuntimeArray.push(inc, new RuntimeScalar("Image-ExifTool-13.44/lib")); + + // Load Image::ExifTool and define helper subroutine + String initScript = """ + use strict; + use warnings; + use Image::ExifTool; + + our $exif = Image::ExifTool->new(); + + sub process_image { + my ($file) = @_; + my $info = $exif->ImageInfo($file, qw(Make Model DateTimeOriginal)); + print "File: $file\\n"; + for my $tag (sort keys %$info) { + print " $tag: $info->{$tag}\\n"; + } + print "\\n"; + } + 1; + """; + + CompilerOptions options = new CompilerOptions(); + options.fileName = ""; + options.code = initScript; + + System.out.println("Loading Image::ExifTool..."); + PerlLanguageProvider.executePerlCode(options, true); + System.out.println("Ready.\n"); + + // Process multiple images by calling the Perl subroutine + String[] images = { + "Image-ExifTool-13.44/t/images/Canon.jpg", + "Image-ExifTool-13.44/t/images/Nikon.jpg" + }; + + RuntimeScalar processImage = GlobalVariable.getGlobalCodeRef("main::process_image"); + + for (String image : images) { + RuntimeArray callArgs = new RuntimeArray(); + RuntimeArray.push(callArgs, new RuntimeScalar(image)); + RuntimeCode.apply(processImage, callArgs, RuntimeContextType.VOID); + } + } +} diff --git a/examples/ExifToolExample.pl b/examples/ExifToolExample.pl new file mode 100644 index 000000000..a9c613b77 --- /dev/null +++ b/examples/ExifToolExample.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +# Example: Using Image::ExifTool module for batch processing +# +# Prerequisites: +# Download and extract ExifTool in the PerlOnJava root directory: +# cd PerlOnJava +# curl -LO https://exiftool.org/Image-ExifTool-13.44.tar.gz +# tar xzf Image-ExifTool-13.44.tar.gz +# +# Run with: +# ./gradlew run --args='examples/ExifToolExample.pl' + +use strict; +use warnings; +use lib 'Image-ExifTool-13.44/lib'; +use Image::ExifTool; + +# Create one ExifTool instance - reusable for multiple files +my $exif = Image::ExifTool->new(); + +# Sample images to process +my @images = ( + 'Image-ExifTool-13.44/t/images/Canon.jpg', + 'Image-ExifTool-13.44/t/images/Nikon.jpg', + 'Image-ExifTool-13.44/t/images/Sony.jpg', +); + +print "ExifTool version: ", $exif->GetValue('ExifToolVersion'), "\n\n"; + +for my $file (@images) { + my $info = $exif->ImageInfo($file, qw(Make Model DateTimeOriginal ImageSize)); + + print "File: $file\n"; + for my $tag (sort keys %$info) { + print " $tag: $info->{$tag}\n"; + } + print "\n"; +}