Skip to content

Commit

Permalink
OCR for PDFs in scan inbox. issue #2294
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Jan 28, 2024
1 parent 25b11bc commit 3f6ee61
Show file tree
Hide file tree
Showing 23 changed files with 2,770 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,9 @@ public void onEvent(Event e) {
}
} else if (e instanceof ScannerStatusEvent) {
this.lblScanStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/scanner.png")));
this.lblScanStatus.setText("" + ((ScannerStatusEvent) e).getFileNames().size());
this.lblScanStatus.setToolTipText(((ScannerStatusEvent) e).getFileNames().size() + " " + java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/editors/EditorsRegistry").getString("status.scansfound"));
if (((ScannerStatusEvent) e).getFileNames().size() > 0) {
this.lblScanStatus.setText("" + ((ScannerStatusEvent) e).getFileMetadata().size());
this.lblScanStatus.setToolTipText(((ScannerStatusEvent) e).getFileMetadata().size() + " " + java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/editors/EditorsRegistry").getString("status.scansfound"));
if (((ScannerStatusEvent) e).getFileMetadata().size() > 0) {
this.lblScanStatus.setEnabled(true);
} else {
this.lblScanStatus.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1709,9 +1709,9 @@ public void onEvent(Event e) {
this.revalidate();
this.repaint();
} else if (e instanceof ScannerStatusEvent) {
this.lblScans.setText("" + ((ScannerStatusEvent) e).getFileNames().size());
this.lblScans.setToolTipText(((ScannerStatusEvent) e).getFileNames().size() + " " + java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/editors/EditorsRegistry").getString("status.scansfound"));
if(!((ScannerStatusEvent) e).getFileNames().isEmpty())
this.lblScans.setText("" + ((ScannerStatusEvent) e).getFileMetadata().size());
this.lblScans.setToolTipText(((ScannerStatusEvent) e).getFileMetadata().size() + " " + java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/editors/EditorsRegistry").getString("status.scansfound"));
if(!((ScannerStatusEvent) e).getFileMetadata().isEmpty())
this.lblScans.setEnabled(true);
else
this.lblScans.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<Component id="cmdRenameScan" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cmdSplitPdf" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cmdOcr" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="115" max="32767" attributes="0"/>
Expand All @@ -42,6 +44,7 @@
<Component id="cmdDeleteScan" min="-2" max="-2" attributes="0"/>
<Component id="cmdRenameScan" min="-2" max="-2" attributes="0"/>
<Component id="cmdSplitPdf" min="-2" max="-2" attributes="0"/>
<Component id="cmdOcr" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
</Group>
Expand Down Expand Up @@ -96,5 +99,15 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdSplitPdfActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="cmdOcr">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons16/material/baseline_font_download_off_red_48dp.png"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdOcrActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,29 @@
package com.jdimension.jlawyer.client.editors.documents;

import com.jdimension.jlawyer.client.mail.SaveToCaseExecutor;
import com.jdimension.jlawyer.client.settings.ClientSettings;
import com.jdimension.jlawyer.pojo.FileMetadata;
import com.jdimension.jlawyer.services.IntegrationServiceRemote;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
*
* @author jens
*/
public class EditScanPanel extends javax.swing.JPanel {

private static final Logger log = Logger.getLogger(EditScanPanel.class.getName());

private SaveToCaseExecutor executor = null;

private String openedFromEditorClass = null;

private ArrayList<FileMetadata> noOcrFiles = new ArrayList<>();

/**
* Creates new form EditScanPanel
*
Expand Down Expand Up @@ -707,6 +717,33 @@ public void setDetails(List<String> selectedDocuments, SaveToCaseExecutor execut
}
}

this.cmdOcr.setEnabled(false);
this.cmdOcr.setToolTipText(null);
this.noOcrFiles.clear();
try {

ClientSettings settings = ClientSettings.getInstance();
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
IntegrationServiceRemote isr = locator.lookupIntegrationServiceRemote();

for (String f : selectedDocuments) {
if (f.toLowerCase().endsWith(".pdf")) {
FileMetadata meta = isr.getObservedFileMetadata(f);
if (meta.getOcrStatus()==FileMetadata.OCRSTATUS_WITHOUTOCR) {
noOcrFiles.add(meta);
}
}
}
if (!noOcrFiles.isEmpty()) {
this.cmdOcr.setEnabled(true);
this.cmdOcr.setToolTipText(noOcrFiles.size() + " PDF-Dokumente sind nicht durchsuchbar - Klick für OCR/Texterkennung");
}

} catch (Exception ex) {
log.error(ex);

}

}

/**
Expand All @@ -722,6 +759,7 @@ private void initComponents() {
cmdDeleteScan = new javax.swing.JButton();
cmdRenameScan = new javax.swing.JButton();
cmdSplitPdf = new javax.swing.JButton();
cmdOcr = new javax.swing.JButton();

lblDescription.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/folder_documents.png"))); // NOI18N
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/editors/addresses/CaseForContactEntryPanel"); // NOI18N
Expand Down Expand Up @@ -753,6 +791,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

cmdOcr.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons16/material/baseline_font_download_off_red_48dp.png"))); // NOI18N
cmdOcr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdOcrActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
Expand All @@ -766,7 +811,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdRenameScan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdSplitPdf)))
.addComponent(cmdSplitPdf)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdOcr)))
.addContainerGap(115, Short.MAX_VALUE))
);
layout.setVerticalGroup(
Expand All @@ -778,7 +825,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdDeleteScan)
.addComponent(cmdRenameScan)
.addComponent(cmdSplitPdf))
.addComponent(cmdSplitPdf)
.addComponent(cmdOcr))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
Expand Down Expand Up @@ -807,8 +855,28 @@ private void cmdSplitPdfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-F
this.executor.splitPdfCallback();
}//GEN-LAST:event_cmdSplitPdfActionPerformed

private void cmdOcrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOcrActionPerformed
try {

ClientSettings settings = ClientSettings.getInstance();
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
IntegrationServiceRemote isr = locator.lookupIntegrationServiceRemote();

for (FileMetadata meta : this.noOcrFiles) {
isr.performOcrForObservedFile(meta.getFileName());
}

} catch (Exception ex) {
log.error(ex);

}


}//GEN-LAST:event_cmdOcrActionPerformed

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cmdDeleteScan;
private javax.swing.JButton cmdOcr;
private javax.swing.JButton cmdRenameScan;
private javax.swing.JButton cmdSplitPdf;
private javax.swing.JLabel lblDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,11 @@
import com.jdimension.jlawyer.client.events.EventBroker;
import com.jdimension.jlawyer.client.events.ScannerStatusEvent;
import com.jdimension.jlawyer.client.settings.ClientSettings;
import com.jdimension.jlawyer.pojo.FileMetadata;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

/**
Expand All @@ -681,7 +680,7 @@
public class ScannerDocumentsTimerTask extends java.util.TimerTask {

private static final Logger log = Logger.getLogger(ScannerDocumentsTimerTask.class.getName());
private static ArrayList<String> lastFiles = new ArrayList<String>();
private static HashMap<FileMetadata, Date> lastFiles = new HashMap<>();
private boolean bypassCache=false;

/**
Expand All @@ -700,18 +699,12 @@ public void run() {
try {
ClientSettings settings = ClientSettings.getInstance();
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
HashMap<File, Date> files = locator.lookupSingletonServiceRemote().getObservedFiles(this.bypassCache);
HashMap<FileMetadata, Date> currentFiles = locator.lookupSingletonServiceRemote().getObservedFiles(this.bypassCache);

ArrayList<String> currentFiles = new ArrayList<>();
for (File f : files.keySet()) {
currentFiles.add(f.getName());
}
Collections.sort(currentFiles);

if (!currentFiles.equals(lastFiles)) {
if (!areKeySetsDifferent(lastFiles, currentFiles)) {

EventBroker eb = EventBroker.getInstance();
eb.publishEvent(new ScannerStatusEvent(files));
eb.publishEvent(new ScannerStatusEvent(currentFiles));
}
lastFiles = currentFiles;

Expand All @@ -721,5 +714,19 @@ public void run() {
}
}
}

private static boolean areKeySetsDifferent(Map<FileMetadata, Date> map1, Map<FileMetadata, Date> map2) {
if (map1.size() != map2.size()) {
return true; // Different number of keys
}

for (FileMetadata key : map1.keySet()) {
if (!map2.containsKey(key)) {
return true; // Key present in map1 but not in map2
}
}

return false; // Key sets are the same
}

}
Loading

0 comments on commit 3f6ee61

Please sign in to comment.