From 1fd25b26e51b4642c82f45c6908caa5ae068041f Mon Sep 17 00:00:00 2001 From: lingutln Date: Thu, 1 Mar 2012 00:23:54 +0000 Subject: [PATCH] Saving imagemap as a zip file functionality. Done by Nikhil. svn path=/; revision=304 --- .../ist/actions/ExportImageMapAction.java | 3 +- .../apps/ist/export/imagemap/Exporter.java | 152 ++++++++++++------ .../apps/ist/export/imagemap/ImageMap.java | 12 ++ 3 files changed, 114 insertions(+), 53 deletions(-) diff --git a/Annotation/src/ie/dcu/apps/ist/actions/ExportImageMapAction.java b/Annotation/src/ie/dcu/apps/ist/actions/ExportImageMapAction.java index ed9fd59..29bbf79 100644 --- a/Annotation/src/ie/dcu/apps/ist/actions/ExportImageMapAction.java +++ b/Annotation/src/ie/dcu/apps/ist/actions/ExportImageMapAction.java @@ -10,6 +10,7 @@ import java.io.*; import java.util.List; import java.util.logging.Level; + import org.eclipse.swt.program.Program; /** @@ -18,7 +19,7 @@ import org.eclipse.swt.program.Program; * @author Kevin McGuinness */ public class ExportImageMapAction extends AppAction{ - + public ExportImageMapAction(ActionManager m) { super(m); } diff --git a/Annotation/src/ie/dcu/apps/ist/export/imagemap/Exporter.java b/Annotation/src/ie/dcu/apps/ist/export/imagemap/Exporter.java index 32fcac1..a4e00e0 100644 --- a/Annotation/src/ie/dcu/apps/ist/export/imagemap/Exporter.java +++ b/Annotation/src/ie/dcu/apps/ist/export/imagemap/Exporter.java @@ -9,6 +9,8 @@ import java.awt.Polygon; import java.awt.image.*; import java.io.*; import java.util.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import javax.imageio.ImageIO; @@ -20,6 +22,7 @@ import javax.imageio.ImageIO; public class Exporter { // private final List masks; private final BufferedImage image; + private File file; private RolloverEffect effect; private String htmlFile = "imagemap.html"; @@ -91,6 +94,10 @@ public class Exporter { public void export(File folder, List masks) throws IOException, ExportException { + // Create a zip file for saving imageMaps + file = new File("/home/lingutln/practice/example.zip"); + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); + // Create image map ImageMap map = new ImageMap(); map.setImageHref(imageFile); @@ -105,73 +112,114 @@ public class Exporter { List preloads = new ArrayList(); map.addPreload(imageFile); - for(SegmentationMask mask : masks) - { - ContourTracer tracer = new ContourTracer(mask, SegmentationMask.FOREGROUND); - List trace = tracer.trace(); + + try { + // Low (fast) compression.. most of data is PNG compressed anyway + out.setLevel(0); + out.flush(); + // Create an entry for the image + ZipEntry entryImage = new ZipEntry(imageFile); - if (trace.size() == 0) { - throw new ExportException("No objects found"); - } - preloads.clear(); - preloads.add(getPreloads(trace, mask.layerNumber).get(0)); - // Write image - ImageIO.write(image, "png", new File(folder, imageFile)); + // Start the entry + out.putNextEntry(entryImage); - if (effect != null) { - int i = 0; + ImageIO.write(image, "png", out); + + for(SegmentationMask mask : masks) + { + ContourTracer tracer = new ContourTracer(mask, SegmentationMask.FOREGROUND); + List trace = tracer.trace(); - // Generate effect images - for (Polygon object : trace) { - RenderedImage im = effect.createEffect(image, object); - // '0' for Rollover effect - File output = new File(folder, preloads.get(i)); - ImageIO.write(im, "png", output); + if (trace.size() == 0) { + throw new ExportException("No objects found"); } - } - - // Add javascript preloads - for (String str : preloads) { - map.addPreload(str); - } - - // Add areas - for (Polygon polygon : trace) { - MapArea area = new MapArea(); - switch (exportShape) { - case Polygon: - area.setPolygon(polygon); - break; - case Rectangle: - area.setRect(polygon.getBounds()); - break; - case Circle: - // TODO: Implement circle! + preloads.clear(); + preloads.add(getPreloads(trace, mask.layerNumber).get(0)); + // Write image + ImageIO.write(image, "png", new File(folder, imageFile)); + + if (effect != null) { + int i = 0; - default: - area.setPolygon(polygon); - break; + // Generate effect images + for (Polygon object : trace) { + RenderedImage im = effect.createEffect(image, object); + // '0' for Rollover effect + File output = new File(folder, preloads.get(i)); + + // Low (fast) compression.. most of data is PNG compressed anyway + out.setLevel(0); + + // Create an entry for the image + ZipEntry entryMaskImage = new ZipEntry(preloads.get(i).toString()); + + // Start the entry + out.putNextEntry(entryMaskImage); + // Writing to the Zip file + ImageIO.write(im, "png", out); + + // writing to the folder + ImageIO.write(im, "png", output); + } } - area.setAlt(objectDescription); - area.setHref(objectLink); + // Add javascript preloads + for (String str : preloads) { + map.addPreload(str); + } - if (effect != null) { + // Add areas + for (Polygon polygon : trace) { + MapArea area = new MapArea(); + switch (exportShape) { + case Polygon: + area.setPolygon(polygon); + break; + case Rectangle: + area.setRect(polygon.getBounds()); + break; + case Circle: + // TODO: Implement circle! + + default: + area.setPolygon(polygon); + break; + } + + area.setAlt(objectDescription); + area.setHref(objectLink); + if (effect != null) { + + + area.addAttr("onmouseover", + String.format("rollover(document.%s, image%s)", + imageName, mask.layerNumber)); + area.addAttr("onmouseout", + String.format("rollover(document.%s, image0)", imageName)); + area.addAttr("title",mask.segmentName); + } - area.addAttr("onmouseover", - String.format("rollover(document.%s, image%s)", - imageName, mask.layerNumber)); - area.addAttr("onmouseout", - String.format("rollover(document.%s, image0)", imageName)); - area.addAttr("title",mask.segmentName); + map.addArea(area); } - - map.addArea(area); } + // Create an entry for the imageMap + ZipEntry entryImageMap = new ZipEntry(htmlFile); + + // Start the entry + out.putNextEntry(entryImageMap); + + // Exporting to zip file + map.exportToFile(out); + // Exporting to Folder map.exportToFile(new File(folder, htmlFile)); } + finally + { + // Close + out.close(); + } } private List getPreloads(List trace, int layerNumber) { diff --git a/Annotation/src/ie/dcu/apps/ist/export/imagemap/ImageMap.java b/Annotation/src/ie/dcu/apps/ist/export/imagemap/ImageMap.java index 58ccfbb..2a3e1c4 100644 --- a/Annotation/src/ie/dcu/apps/ist/export/imagemap/ImageMap.java +++ b/Annotation/src/ie/dcu/apps/ist/export/imagemap/ImageMap.java @@ -3,7 +3,9 @@ */ package ie.dcu.apps.ist.export.imagemap; +import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.DataOutputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -12,6 +14,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.zip.ZipOutputStream; /** * Exports HTML image maps @@ -196,6 +199,15 @@ public class ImageMap { } } + public void exportToFile(ZipOutputStream o) throws IOException { + DataOutputStream out = new DataOutputStream( + new BufferedOutputStream(o)); + System.out.println("Exxporting content : "+export()); + out.writeBytes(export()); + // Flush + out.flush(); + } + private String substitute(String template, Map subs) { // This could be more efficient.. String result = template; -- 2.34.1