Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/guides/java-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions examples/ExifToolExample.java
Original file line number Diff line number Diff line change
@@ -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 = "<init>";
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);
}
}
}
38 changes: 38 additions & 0 deletions examples/ExifToolExample.pl
Original file line number Diff line number Diff line change
@@ -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";
}