diff --git a/.gitignore b/.gitignore index d6eb23b..31c49aa 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ release.properties *.releaseBackup dependency-reduced-pom.xml .idea/ -*.iml \ No newline at end of file +*.iml +report/ \ No newline at end of file diff --git a/src/main/java/net/sf/javaanpr/gui/ReportGenerator.java b/src/main/java/net/sf/javaanpr/gui/ReportGenerator.java index 59c2710..3818120 100644 --- a/src/main/java/net/sf/javaanpr/gui/ReportGenerator.java +++ b/src/main/java/net/sf/javaanpr/gui/ReportGenerator.java @@ -31,14 +31,12 @@ public class ReportGenerator { private String directory; private StringBuilder output; // TODO refactor into a form - private boolean enabled; public ReportGenerator(String directory) throws IOException { this.directory = directory; - this.enabled = true; File f = new File(directory); - if (!f.exists() || !f.isDirectory()) { - throw new IOException("Report directory '" + directory + "' doesn't exist or isn't a directory"); + if (!f.exists() && !f.mkdirs()) { + throw new IOException("Report directory '" + directory + "' doesn't exist and couldn't be created"); } this.output = new StringBuilder(); this.output.append("" + "" @@ -46,22 +44,12 @@ public ReportGenerator(String directory) throws IOException { + "@import \"style.css\";" + ""); } - public ReportGenerator() { - this.enabled = false; - } - public void insertText(String text) { - if (!this.enabled) { - return; - } this.output.append(text + "\n"); } public void insertImage(BufferedImage image, String cls, int w, int h) throws IllegalArgumentException, IOException { - if (!this.enabled) { - return; - } String imageName = String.valueOf(image.hashCode()) + ".jpg"; this.saveImage(image, imageName); if ((w != 0) && (h != 0)) { @@ -73,9 +61,6 @@ public void insertImage(BufferedImage image, String cls, int w, int h) } public void finish() throws IOException { - if (!this.enabled) { - return; - } this.output.append(""); FileOutputStream os = new FileOutputStream(this.directory + File.separator + "index.html"); Writer writer = new OutputStreamWriter(os); @@ -99,9 +84,6 @@ public void saveStreamToFile(InputStream inStream, File out) throws IOException } public void saveImage(BufferedImage bi, String filename) throws IOException, IllegalArgumentException { - if (!this.enabled) { - return; - } String type = new String(filename.substring(filename.lastIndexOf('.') + 1, filename.length()).toLowerCase()); if (!type.equals("bmp") && !type.equals("jpg") && !type.equals("jpeg") && !type.equals("png")) { throw new IllegalArgumentException("Unsupported file format"); diff --git a/src/main/java/net/sf/javaanpr/intelligence/Intelligence.java b/src/main/java/net/sf/javaanpr/intelligence/Intelligence.java index cb42070..59c82ad 100644 --- a/src/main/java/net/sf/javaanpr/intelligence/Intelligence.java +++ b/src/main/java/net/sf/javaanpr/intelligence/Intelligence.java @@ -289,12 +289,16 @@ public String recognize(CarSnapshot carSnapshot, final boolean enableReportGener Main.rg.insertText(""); Main.rg.insertText("Recognized plate : " + parsedOutput); Main.rg.insertText(""); + Main.rg.finish(); } return parsedOutput; } } // TODO failed! lastProcessDuration = time.getTime(); + if (enableReportGeneration) { + Main.rg.finish(); + } return null; } } diff --git a/src/main/java/net/sf/javaanpr/jar/Main.java b/src/main/java/net/sf/javaanpr/jar/Main.java index 2170974..04824f9 100644 --- a/src/main/java/net/sf/javaanpr/jar/Main.java +++ b/src/main/java/net/sf/javaanpr/jar/Main.java @@ -53,7 +53,7 @@ public final class Main { /** * The report generator. */ - public static ReportGenerator rg = new ReportGenerator(); + public static ReportGenerator rg; /** * The intelligence. */ @@ -77,6 +77,14 @@ public final class Main { + " and saves it into output file\n" + " -newalphabet -i -o \n" + " Normalize all images in and save\n" + " it to ."; + static { + try { + rg = new ReportGenerator("report"); + } catch (IOException e) { + throw new IllegalStateException("Error during report generator initialization.", e); + } + } + private Main() { // intentionally empty } @@ -142,7 +150,6 @@ public static void main(String[] args) throws Exception { // TODO refactor Main.rg = new ReportGenerator(args[4]); Main.systemLogic = new Intelligence(); Main.systemLogic.recognize(new CarSnapshot(args[2]), true); - Main.rg.finish(); } else if ((args.length == 3) && args[0].equals("-newconfig") && args[1].equals("-o")) { // save default config into args[2] Configurator.getConfigurator().saveConfiguration(args[2]);