From 9786d2dd036cd96bf1553befea52342a592ef1f7 Mon Sep 17 00:00:00 2001 From: brossetti Date: Fri, 27 Oct 2017 13:24:08 -0400 Subject: [PATCH 1/2] Added a maven-projects example for printing image metadata based on imagej/tutorials#35 --- maven-projects/metadata/pom.xml | 108 +++++++++++++++ .../metadata/src/main/java/PrintMetadata.java | 123 ++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 maven-projects/metadata/pom.xml create mode 100644 maven-projects/metadata/src/main/java/PrintMetadata.java diff --git a/maven-projects/metadata/pom.xml b/maven-projects/metadata/pom.xml new file mode 100644 index 00000000..dc434cae --- /dev/null +++ b/maven-projects/metadata/pom.xml @@ -0,0 +1,108 @@ + + + 4.0.0 + + + org.scijava + pom-scijava + 16.1.0 + + + + imagej-tutorials + metadata + 1.0.0-SNAPSHOT + + Metadata + This example shows how to access and print metadata with SCIFIO. + https://github.com/[MY-ORG]/[MY-REPO] + 2013 + + [MY-ORGANIZATION-NAME] + [MY-ORGANIZATION-WEB-SITE] + + + + CC0 1.0 Universal License + http://creativecommons.org/publicdomain/zero/1.0/ + repo + + + + + + [MY-GITHUB-ID] + [MY-FULL-NAME] + https://imagej.net/User:[MY-IMAGEJ-WIKI-ACCOUNT] + + + + + None + + + + + + ImageJ Forum + http://forum.imagej.net/ + + + + + scm:git:git://github.com/[MY-ORG]/[MY-REPO] + scm:git:git@github.com:[MY-ORG]/[MY-REPO] + HEAD + https://github.com/[MY-ORG]/[MY-REPO] + + + GitHub Issues + http://github.com/[MY-ORG]/[MY-REPO]/issues + + + None + + + + PrintMetadata + cc0 + N/A + ImageJ software for multidimensional image processing and analysis. + + + + + imagej.public + http://maven.imagej.net/content/groups/public + + + + + + net.imagej + imagej + + + io.scif + scifio + + + io.scif + scifio-bf-compat + runtime + + + ome + formats-bsd + runtime + + + ome + formats-gpl + runtime + + + diff --git a/maven-projects/metadata/src/main/java/PrintMetadata.java b/maven-projects/metadata/src/main/java/PrintMetadata.java new file mode 100644 index 00000000..b254ea46 --- /dev/null +++ b/maven-projects/metadata/src/main/java/PrintMetadata.java @@ -0,0 +1,123 @@ +/* + * To the extent possible under law, the ImageJ developers have waived + * all copyright and related or neighboring rights to this tutorial code. + * + * See the CC0 1.0 Universal license for details: + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +import java.io.IOException; + +import io.scif.services.FormatService; +import io.scif.Format; +import io.scif.FormatException; +import io.scif.Metadata; +import io.scif.FieldPrinter; + +import net.imagej.ImageJ; +import net.imagej.Dataset; + +import org.scijava.ItemIO; +import org.scijava.command.Command; +import org.scijava.log.LogService; +import org.scijava.plugin.Parameter; +import org.scijava.plugin.Plugin; + +/** + * This example illustrates how to print image metadata using the {@link FormatService} and {@link FieldPrinter}. + *

+ * We use the {@link FormatService} to determine the {@link Format} of the input image. Then, we parse + * the metadata from the image file and use {@link FieldPrinter} to print the metadata fields as strings. + *

+ *

+ * An optional {@code formatMetadata} method is included to (hopefully) make the text more readable. + *

+ */ +@Plugin(type = Command.class, menuPath = "Plugins>Print Metadata") +public class PrintMetadata implements Command { + + // -- Needed services -- + + // for determining the Format of the input image + @Parameter + private FormatService formatService; + + // for logging errors + @Parameter + private LogService log; + + // -- Inputs and outputs to the command -- + + // input image + @Parameter + private Dataset img; + + // output metadata string + @Parameter(label = "Metadata", type = ItemIO.OUTPUT) + private String mString; + + @Override + public void run() { + // we need the file path to determine the file format + final String filePath = img.getSource(); + + // catch any Format or IO exceptions + try { + + // determine the Format based on the extension and, if necessary, the source data + Format format = formatService.getFormat(filePath); + + // create an instance of the associated Parser and parse metadata from the image file + Metadata metadata = format.createParser().parse(filePath); + + // use FieldPrinter to traverse metadata tree and return as a String + String metadataTree = new FieldPrinter(metadata).toString(); + + // (optional) remove some of the tree formatting to make the metadata easier to read + mString = formatMetadata(metadataTree); + } + catch (final FormatException | IOException e) { + log.error(e); + } + + } + + /** + * This function makes the metadata easier to read by removing some of the tree formatting from FieldPrinter. + * @param metadataTree raw metadata string returned by FieldPrinter().toString() + * @return formatted version of metadataTree + */ + private String formatMetadata(String metadataTree) { + + // remove ending braces | replace ", " between OME fields + String tmp = metadataTree.replaceAll("(\\t+}\\n)|(,\\s)", "\n"); + + // remove beginning braces | remove indenting + return tmp.replaceAll("(\\t+\\{\\n)|(\\t+)", ""); + } + + /** + * This main function serves for development purposes. + * It allows you to run the plugin immediately out of + * your integrated development environment (IDE). + * + * @param args unused + * @throws Exception + */ + public static void main(final String... args) throws Exception { + // create the ImageJ application context with all available services + final ImageJ ij = new ImageJ(); + ij.launch(args); + + // open a sample image + final Dataset img = ij.scifio().datasetIO().open("http://imagej.net/images/FluorescentCells.jpg"); + + // show the image + ij.ui().show(img); + + // invoke the plugin + ij.command().run(PrintMetadata.class, true); + + } + +} \ No newline at end of file From da9f1781b9b9e6266444057f9a80722fa15b2335 Mon Sep 17 00:00:00 2001 From: brossetti Date: Fri, 27 Oct 2017 18:50:11 -0400 Subject: [PATCH 2/2] corrections based on review by @imagejan --- maven-projects/metadata/pom.xml | 32 +++++++++---------- .../{PrintMetadata.java => GetMetadata.java} | 13 ++++---- 2 files changed, 23 insertions(+), 22 deletions(-) rename maven-projects/metadata/src/main/java/{PrintMetadata.java => GetMetadata.java} (91%) diff --git a/maven-projects/metadata/pom.xml b/maven-projects/metadata/pom.xml index dc434cae..b688ffff 100644 --- a/maven-projects/metadata/pom.xml +++ b/maven-projects/metadata/pom.xml @@ -8,18 +8,18 @@ org.scijava pom-scijava - 16.1.0 + 17.1.1 imagej-tutorials metadata - 1.0.0-SNAPSHOT + 0.1.0-SNAPSHOT Metadata - This example shows how to access and print metadata with SCIFIO. + This example shows how to access and display metadata with SCIFIO. https://github.com/[MY-ORG]/[MY-REPO] - 2013 + 2017 [MY-ORGANIZATION-NAME] [MY-ORGANIZATION-WEB-SITE] @@ -67,7 +67,7 @@ - PrintMetadata + GetMetadata cc0 N/A ImageJ software for multidimensional image processing and analysis. @@ -92,17 +92,17 @@ io.scif scifio-bf-compat - runtime + runtime + + + ome + formats-bsd + runtime + + + ome + formats-gpl + runtime - - ome - formats-bsd - runtime - - - ome - formats-gpl - runtime - diff --git a/maven-projects/metadata/src/main/java/PrintMetadata.java b/maven-projects/metadata/src/main/java/GetMetadata.java similarity index 91% rename from maven-projects/metadata/src/main/java/PrintMetadata.java rename to maven-projects/metadata/src/main/java/GetMetadata.java index b254ea46..4f5f78c8 100644 --- a/maven-projects/metadata/src/main/java/PrintMetadata.java +++ b/maven-projects/metadata/src/main/java/GetMetadata.java @@ -24,17 +24,18 @@ import org.scijava.plugin.Plugin; /** - * This example illustrates how to print image metadata using the {@link FormatService} and {@link FieldPrinter}. + * This example illustrates how to access and display image metadata using the {@link FormatService} and + * {@link FieldPrinter}. *

* We use the {@link FormatService} to determine the {@link Format} of the input image. Then, we parse - * the metadata from the image file and use {@link FieldPrinter} to print the metadata fields as strings. + * the metadata from the image file and use {@link FieldPrinter} to retrieve the metadata fields as strings. *

*

* An optional {@code formatMetadata} method is included to (hopefully) make the text more readable. *

*/ -@Plugin(type = Command.class, menuPath = "Plugins>Print Metadata") -public class PrintMetadata implements Command { +@Plugin(type = Command.class, menuPath = "Image>Show Metadata") +public class GetMetadata implements Command { // -- Needed services -- @@ -116,8 +117,8 @@ public static void main(final String... args) throws Exception { ij.ui().show(img); // invoke the plugin - ij.command().run(PrintMetadata.class, true); + ij.command().run(GetMetadata.class, true); } -} \ No newline at end of file +}