Skip to content

Commit

Permalink
Fixed report generation finalization and output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
oskopek committed Aug 7, 2016
1 parent dcde115 commit e805cd0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ release.properties
*.releaseBackup
dependency-reduced-pom.xml
.idea/
*.iml
*.iml
report/
22 changes: 2 additions & 20 deletions src/main/java/net/sf/javaanpr/gui/ReportGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,25 @@ 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" + "<html>"
+ "<head><title>ANPR report</title>" + "</head>" + "<style type=\"text/css\">"
+ "@import \"style.css\";" + "</style>");
}

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)) {
Expand All @@ -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("</html>");
FileOutputStream os = new FileOutputStream(this.directory + File.separator + "index.html");
Writer writer = new OutputStreamWriter(os);
Expand All @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/sf/javaanpr/intelligence/Intelligence.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,16 @@ public String recognize(CarSnapshot carSnapshot, final boolean enableReportGener
Main.rg.insertText("<span class='recognized'>");
Main.rg.insertText("Recognized plate : " + parsedOutput);
Main.rg.insertText("</span>");
Main.rg.finish();
}
return parsedOutput;
}
}
// TODO failed!
lastProcessDuration = time.getTime();
if (enableReportGeneration) {
Main.rg.finish();
}
return null;
}
}
11 changes: 9 additions & 2 deletions src/main/java/net/sf/javaanpr/jar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class Main {
/**
* The report generator.
*/
public static ReportGenerator rg = new ReportGenerator();
public static ReportGenerator rg;
/**
* The intelligence.
*/
Expand All @@ -77,6 +77,14 @@ public final class Main {
+ " and saves it into output file\n" + " -newalphabet -i <srcdir> -o <dstdir>\n"
+ " Normalize all images in <srcdir> and save\n" + " it to <dstdir>.";

static {
try {
rg = new ReportGenerator("report");
} catch (IOException e) {
throw new IllegalStateException("Error during report generator initialization.", e);
}
}

private Main() {
// intentionally empty
}
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit e805cd0

Please sign in to comment.