|
|
|
@ -87,6 +87,7 @@ public class MainFrame extends JFrame {
@@ -87,6 +87,7 @@ public class MainFrame extends JFrame {
|
|
|
|
|
private Date date = new Date(); |
|
|
|
|
|
|
|
|
|
private String patchedPath = ""; |
|
|
|
|
private String project = ""; |
|
|
|
|
|
|
|
|
|
private Configuration config; |
|
|
|
|
|
|
|
|
@ -155,13 +156,26 @@ public class MainFrame extends JFrame {
@@ -155,13 +156,26 @@ public class MainFrame extends JFrame {
|
|
|
|
|
* Scans the given path for occurences of '$VARIABLES' and creates input fields for the found variables. |
|
|
|
|
* @param path |
|
|
|
|
*/ |
|
|
|
|
private void addFieldsFor(String path) { |
|
|
|
|
private void addFieldsForPath(String path) { |
|
|
|
|
Vector<String> marks = new Vector<>(); |
|
|
|
|
Matcher matches = MARKEN.matcher(path); |
|
|
|
|
while (matches.find()) marks.add(matches.group(1)); |
|
|
|
|
toolbar.addFieldsFor(marks); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
public String appMissing(String appTest){ |
|
|
|
|
List<String> cmd = Arrays.asList(appTest.split(" ")); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Process process = new ProcessBuilder(cmd).start(); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode == 0) return null; |
|
|
|
|
} catch (InterruptedException | IOException e) { |
|
|
|
|
LOG.error(t("{} terminated: "),appTest,e); |
|
|
|
|
} |
|
|
|
|
return cmd.get(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void checkDependencies() { |
|
|
|
|
List<String> missing = List.of("convert --version", "killall --version", "pdfsandwich -version", "pdftk --version", "scanimage --version",FILE_BROWSER+" --version") |
|
|
|
|
.stream().map(this::appMissing).filter(app -> app != null).collect(Collectors.toList()); |
|
|
|
@ -226,18 +240,7 @@ public class MainFrame extends JFrame {
@@ -226,18 +240,7 @@ public class MainFrame extends JFrame {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String appMissing(String appTest){ |
|
|
|
|
List<String> cmd = Arrays.asList(appTest.split(" ")); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Process process = new ProcessBuilder(cmd).start(); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode == 0) return null; |
|
|
|
|
} catch (InterruptedException | IOException e) { |
|
|
|
|
LOG.error(t("{} terminated: "),appTest,e); |
|
|
|
|
} |
|
|
|
|
return cmd.get(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* checks, whether the scan button may be enabled. |
|
|
|
@ -246,6 +249,12 @@ public class MainFrame extends JFrame {
@@ -246,6 +249,12 @@ public class MainFrame extends JFrame {
|
|
|
|
|
toolbar.readyToScan(!(isScanning || patchedPath==null || patchedPath.isBlank() || patchedPath.contains("$"))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String extension(String path) { |
|
|
|
|
var target = config.get(CONFIG_TARGET); |
|
|
|
|
return path +"."+ (JPG.equals(target) ? "jpg":"pdf"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void notifyAppsMissing(List<String> missingApps) { |
|
|
|
|
StringBuilder message = new StringBuilder(); |
|
|
|
|
message.append("<html>"); |
|
|
|
@ -267,25 +276,26 @@ public class MainFrame extends JFrame {
@@ -267,25 +276,26 @@ public class MainFrame extends JFrame {
|
|
|
|
|
* @param path |
|
|
|
|
* @param dimension |
|
|
|
|
*/ |
|
|
|
|
private void performScan(String path, Dimension dimension) { |
|
|
|
|
private void performScan(String project, String path, Dimension dimension) { |
|
|
|
|
LOG.debug("performScan({})",path); |
|
|
|
|
/* We need to save all required config values before starting to scan, because they might be changed during scanning */ |
|
|
|
|
String target = config.getOrCreate(CONFIG_TARGET, "pdf"); |
|
|
|
|
|
|
|
|
|
setScanning(true); |
|
|
|
|
|
|
|
|
|
File folder = new File(path); |
|
|
|
|
|
|
|
|
|
var targetFile = new File(path); |
|
|
|
|
File folder = targetFile.getParentFile(); |
|
|
|
|
if (!folder.exists()) { |
|
|
|
|
LOG.warn(t("Path '{}' does not exist!"),path); |
|
|
|
|
folder.mkdirs(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
docTable.add(path); |
|
|
|
|
docTable.add(project); |
|
|
|
|
|
|
|
|
|
int resolution = toolbar.getResolution(); |
|
|
|
|
boolean improbeBrightness = toolbar.getImproveBrightness(); |
|
|
|
|
long timestamp = new Date().getTime(); |
|
|
|
|
String raw = timestamp+".jpg"; |
|
|
|
|
//long timestamp = new Date().getTime();
|
|
|
|
|
//String raw = timestamp+".jpg";
|
|
|
|
|
var raw = targetFile.getName()+".jpg"; |
|
|
|
|
|
|
|
|
|
Vector<String> cmd = new Vector<>(); |
|
|
|
|
cmd.add("scanimage"); |
|
|
|
@ -305,19 +315,19 @@ public class MainFrame extends JFrame {
@@ -305,19 +315,19 @@ public class MainFrame extends JFrame {
|
|
|
|
|
builder.directory(folder); |
|
|
|
|
try { |
|
|
|
|
Process process = builder.start(); |
|
|
|
|
docTable.setState(path,t("Scanning…")); |
|
|
|
|
docTable.setState(project,t("Scanning…")); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode != 0) { |
|
|
|
|
LOG.error(t("Error code: {} for {}"),errorCode,cmd); |
|
|
|
|
docTable.setState(path, t("Scan failed.")); |
|
|
|
|
docTable.setState(project, t("Scan failed.")); |
|
|
|
|
setScanning(false); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} catch (InterruptedException | IOException e) { |
|
|
|
|
LOG.error(t("{} terminated: "),builder,e); |
|
|
|
|
} |
|
|
|
|
setScanning(false); |
|
|
|
|
toolbar.nextPage(); |
|
|
|
|
|
|
|
|
|
if (improbeBrightness) { |
|
|
|
|
cmd = new Vector<>(); |
|
|
|
@ -329,11 +339,11 @@ public class MainFrame extends JFrame {
@@ -329,11 +339,11 @@ public class MainFrame extends JFrame {
|
|
|
|
|
builder.directory(folder); |
|
|
|
|
try { |
|
|
|
|
Process process = builder.start(); |
|
|
|
|
docTable.setState(path,t("Adjusting brightness…")); |
|
|
|
|
docTable.setState(project,t("Adjusting brightness…")); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode != 0) { |
|
|
|
|
LOG.error(t("Error code: {} for {}"),errorCode,cmd); |
|
|
|
|
docTable.setState(path, t("Adjustment failed.")); |
|
|
|
|
docTable.setState(project, t("Adjustment failed.")); |
|
|
|
|
setScanning(false); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -344,12 +354,12 @@ public class MainFrame extends JFrame {
@@ -344,12 +354,12 @@ public class MainFrame extends JFrame {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (JPG.equals(target)) { |
|
|
|
|
docTable.setState(path, t("Image scanned")); |
|
|
|
|
docTable.preview(path); |
|
|
|
|
docTable.setState(project, t("Image scanned")); |
|
|
|
|
docTable.preview(folder); |
|
|
|
|
return; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
String pdf = timestamp+".page.pdf"; |
|
|
|
|
String pdf = targetFile.getName() + (PDF.equals(target) ? ".pdf" : ".tmp.pdf"); //timestamp+".page.pdf";
|
|
|
|
|
cmd = new Vector<>(); |
|
|
|
|
cmd.add("convert"); |
|
|
|
|
cmd.add(raw); |
|
|
|
@ -358,26 +368,27 @@ public class MainFrame extends JFrame {
@@ -358,26 +368,27 @@ public class MainFrame extends JFrame {
|
|
|
|
|
builder.directory(folder); |
|
|
|
|
try { |
|
|
|
|
Process process = builder.start(); |
|
|
|
|
docTable.setState(path,t("Converting to PDF…")); |
|
|
|
|
docTable.setState(project,t("Converting to PDF…")); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode != 0) { |
|
|
|
|
LOG.error("Error code: {} for {}",errorCode,cmd); |
|
|
|
|
docTable.setState(path, t("Conversion failed.")); |
|
|
|
|
docTable.setState(project, t("Conversion failed.")); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (InterruptedException | IOException e) { |
|
|
|
|
LOG.error("{} terminated: ",builder,e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Path.of(path, raw).toFile().delete(); |
|
|
|
|
LOG.debug("removing {}",raw); |
|
|
|
|
folder.toPath().resolve(raw).toFile().delete(); |
|
|
|
|
//Path.of(folder,raw).toFile().delete();
|
|
|
|
|
if (PDF.equals(target)) { |
|
|
|
|
docTable.setState(path, t("PDF created")); |
|
|
|
|
docTable.preview(path); |
|
|
|
|
docTable.setState(project, t("PDF created")); |
|
|
|
|
docTable.preview(folder); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String ocr = timestamp+".page.ocr.pdf"; |
|
|
|
|
String ocr = targetFile.getName() + ".pdf";//timestamp+".page.ocr.pdf";
|
|
|
|
|
|
|
|
|
|
cmd = new Vector<>(); |
|
|
|
|
cmd.add("pdfsandwich"); |
|
|
|
@ -396,20 +407,26 @@ public class MainFrame extends JFrame {
@@ -396,20 +407,26 @@ public class MainFrame extends JFrame {
|
|
|
|
|
builder.directory(folder); |
|
|
|
|
try { |
|
|
|
|
Process process = builder.start(); |
|
|
|
|
docTable.setState(path,t("Text recognition…")); |
|
|
|
|
docTable.setState(project,t("Text recognition…")); |
|
|
|
|
int errorCode = process.waitFor(); |
|
|
|
|
if (errorCode != 0) { |
|
|
|
|
LOG.error("error code: {} for {}",errorCode,cmd); |
|
|
|
|
docTable.setState(path, t("OCR failed.")); |
|
|
|
|
docTable.setState(project, t("OCR failed.")); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
if (!folder.toPath().resolve(ocr).toFile().exists()){ |
|
|
|
|
docTable.setState(project,t("OCR failed.")); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} catch (InterruptedException | IOException e) { |
|
|
|
|
LOG.error("{} terminated: ",builder,e); |
|
|
|
|
} |
|
|
|
|
Path.of(path, pdf).toFile().delete(); |
|
|
|
|
docTable.setState(path,t("Finished text recognition.")); |
|
|
|
|
docTable.preview(path); |
|
|
|
|
|
|
|
|
|
LOG.debug("removing {}",pdf); |
|
|
|
|
folder.toPath().resolve(pdf).toFile().delete(); |
|
|
|
|
docTable.setState(project,t("Finished text recognition.")); |
|
|
|
|
docTable.preview(folder); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -440,7 +457,7 @@ public class MainFrame extends JFrame {
@@ -440,7 +457,7 @@ public class MainFrame extends JFrame {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* open file in preview panel |
|
|
|
|
* @param file |
|
|
|
|
* @param filePath |
|
|
|
|
*/ |
|
|
|
|
private void preview(Path filePath) { |
|
|
|
|
if (filePath == null) return; |
|
|
|
@ -482,7 +499,7 @@ public class MainFrame extends JFrame {
@@ -482,7 +499,7 @@ public class MainFrame extends JFrame {
|
|
|
|
|
*/ |
|
|
|
|
private void scan(ActionEvent ev) { |
|
|
|
|
updateConfig(); |
|
|
|
|
new Thread(() -> performScan(patchedPath,dimension)).start(); |
|
|
|
|
new Thread(() -> performScan(project,patchedPath,dimension)).start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -592,10 +609,17 @@ public class MainFrame extends JFrame {
@@ -592,10 +609,17 @@ public class MainFrame extends JFrame {
|
|
|
|
|
.replace("$"+t("YEAR"), year+"") |
|
|
|
|
.replace("$"+t("MONTH"), month<10 ? "0"+month : ""+month) |
|
|
|
|
.replace("$"+t("DAY"), day < 10 ? "0"+day : ""+day); |
|
|
|
|
|
|
|
|
|
addFieldsFor(patchedPath); |
|
|
|
|
for (Entry<String, String> entry : fields.entrySet()) patchedPath = patchedPath.replace("$"+entry.getKey(), entry.getValue()); |
|
|
|
|
statusBar.setPath(patchedPath); |
|
|
|
|
addFieldsForPath(patchedPath); |
|
|
|
|
project = extension(patchedPath); |
|
|
|
|
for (Entry<String, String> entry : fields.entrySet()) { |
|
|
|
|
var key = entry.getKey(); |
|
|
|
|
patchedPath = patchedPath.replace("$"+entry.getKey(), entry.getValue()); |
|
|
|
|
if (!t("PAGE").equals(key)){ |
|
|
|
|
project = project.replace("$"+entry.getKey(), entry.getValue()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
statusBar.setPath(extension(patchedPath)); |
|
|
|
|
checkScanButton(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|