Skip to content

Commit

Permalink
reinmplemented finding of the ocr images
Browse files Browse the repository at this point in the history
  • Loading branch information
finkf committed Jun 18, 2015
1 parent 4cf26ae commit f2fcb28
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public final void parse(String xml, String img, String enc) {
InputSource is = new InputSource(getReader(xml, enc));
xr.parse(is);
} catch (IOException ex) {
Log.error(this, "Could not read %s: %s", xml, ex);
Log.error(this, "Could not read %s: %s", xml, ex.getMessage());
throw new RuntimeException(ex);
} catch (SAXException ex) {
Log.error(this, "Invalid Xml file %s: %s", xml, ex);
Log.error(this, "Invalid Xml file %s: %s", xml, ex.getMessage());
throw new RuntimeException(ex);
}
}
Expand Down
83 changes: 58 additions & 25 deletions CorrectionBackend/src/jav/correctionBackend/CorrectionSystem.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package jav.correctionBackend;

import jav.logging.log4j.Log;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -157,34 +160,27 @@ public boolean accept(File d, String name) {
File imgd = new File(imagedir);
String[] xmlfiles = xmld.list(fil);
java.util.Arrays.sort(xmlfiles);

HashMap<String, String> imgs = new HashMap<String, String>();
if (imagedir != null) {
String[] imgfiles = imgd.list(new FilenameFilter() {

@Override
public boolean accept(File d, String name) {
return name.endsWith(".tif") || name.endsWith(".jpg") || name.endsWith(".jpeg");
}
});

for (int j = 0; j < imgfiles.length; j++) {
imgs.put(imgfiles[j].substring(0, imgfiles[j].lastIndexOf(".")), imgfiles[j]);
}
}
HashMap<String, String> mappings = getImageFileMappings(imgd);

long time_start = System.currentTimeMillis();
for (int i = 0; i < xmlfiles.length; i++) {
for (String xmlfile: xmlfiles) {
String basename = getBaseName(xmlfile);
String imagefile = "";
if (mappings.containsKey(basename))
imagefile = mappings.get(basename);
ph.progress("Parsing file " + xmlfile + " (" + imagefile + ")");
try {
if (imgs.containsKey(xmlfiles[i].substring(0, xmlfiles[i].indexOf(".")))) {
ph.progress("Parsing file: " + xmlfiles[i]);
parser.parse(xmld.getCanonicalPath() + "/" + xmlfiles[i], imgs.get(xmlfiles[i].substring(0, xmlfiles[i].indexOf("."))), encoding);
} else {
ph.progress("Parsing file: " + xmlfiles[i]);
parser.parse(xmld.getCanonicalPath() + "/" + xmlfiles[i], "", encoding);
}
parser.parse(
new File(xmld, xmlfile).getCanonicalPath(),
imagefile,
encoding
);
} catch (IOException ex) {
Logger.getLogger(CorrectionSystem.class.getName()).log(Level.SEVERE, null, ex);
Log.error(
this, "could not parse %s: invalid image file: %s",
xmlfile,
ex.getMessage()
);
}
}
long duration = System.currentTimeMillis() - time_start;
Expand All @@ -198,6 +194,43 @@ public boolean accept(File d, String name) {
return retval;
}

private HashMap<String, String> getImageFileMappings(File dir) {
HashMap<String, String> mappings = new HashMap<>();
String[] images = getAllImageFiles(dir);
for (String image: images) {
File imagefile = new File(dir, image);
try {
mappings.put(getBaseName(image), imagefile.getCanonicalPath());
} catch (IOException ex) {
Log.error(
this,
"invalid image file %s: %s",
imagefile,
ex.getMessage()
);
}
}
return mappings;
}

private String[] getAllImageFiles(File dir) {
if (dir == null)
return new String[0];
return dir.list(new FilenameFilter() {
@Override
public boolean accept(File d, String name) {
return name.endsWith(".tif") ||
name.endsWith(".jpg") ||
name.endsWith(".jpeg");
}
});
}

private static String getBaseName(String filename) {
return filename.substring(0, filename.indexOf('.') + 1);
}


public void importProfile( Document doc, String filename) {
new ProfileImporter().parse(doc, filename);
}
Expand All @@ -211,7 +244,7 @@ public void closeDocument() {
conn.close();
jcp.dispose();
} catch (SQLException ex) {
Logger.getLogger(CorrectionSystem.class.getName()).log(Level.SEVERE, null, ex);
Log.error(this, "could not shutdown database: %s", ex.getMessage());
}
}

Expand Down

0 comments on commit f2fcb28

Please sign in to comment.