fix bugs
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import commands.Analyse;
|
||||
|
@@ -195,6 +195,7 @@ public class SubjectGenerator {
|
||||
int width = (int) page.getMediaBox().getWidth();
|
||||
|
||||
// generate rectangle nom
|
||||
|
||||
pdPageContentStream.addRect(width - 238, height - 196, 155, 50); // RECT
|
||||
// text
|
||||
pdPageContentStream.beginText();
|
||||
|
@@ -138,6 +138,11 @@ public class Read implements Callable <Void> {
|
||||
config.readConfig();
|
||||
|
||||
GestionnaireCopies ocr = new GestionnaireCopies(directory_name); // Instantie l'ocr
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GenerateCSV csv = new GenerateCSV(ocr.createHashMapforCSV(),config.getParam().get("Code"), result_name, logger);
|
||||
|
||||
csv.createFile(); //Génère le fichier csv à partir de la HMap retournée par l'OCR
|
||||
|
@@ -4,58 +4,53 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class GenerateCSV {
|
||||
|
||||
HashMap<String,String> etudiants;
|
||||
|
||||
Map<String, String> etudiants;
|
||||
int numLength;
|
||||
String path = "../export";
|
||||
Logger logger;
|
||||
|
||||
public GenerateCSV(HashMap<String,String> map, String length, String pth, Logger lg) {
|
||||
|
||||
public GenerateCSV(Map<String, String> map, String length, String pth, Logger lg) {
|
||||
this.etudiants = map;
|
||||
this.numLength = Integer.parseInt(length);
|
||||
this.path = path+"/"+pth;
|
||||
this.path = path + "/" + pth;
|
||||
this.logger = lg;
|
||||
}
|
||||
|
||||
|
||||
// Teste validité du numero etudiant (selon param de la config passé : numLength)
|
||||
|
||||
|
||||
// Teste validité du numero etudiant (selon param de la config passé :
|
||||
// numLength)
|
||||
|
||||
public boolean isValid(String s) {
|
||||
int i = 0;
|
||||
logger.debug("Checking string validity");
|
||||
if (s.length() == this.numLength) {
|
||||
while (i < s.length()) {
|
||||
|
||||
|
||||
int nb = Character.getNumericValue(s.charAt(i));
|
||||
|
||||
|
||||
if (nb <= 0 || nb >= 9) {
|
||||
logger.fatal("Student id's characters are not recognized");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
logger.debug("String validity ok");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
logger.fatal("Student id's length is not correct");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void createFile() {
|
||||
try (PrintWriter writer = new PrintWriter(new File(this.path))) {
|
||||
try (PrintWriter writer = new PrintWriter(new File("E:\\S3T\\Projet\\pt-s3t-g4\\pdf"))) {
|
||||
|
||||
logger.info("Creating csv file");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -65,37 +60,33 @@ public class GenerateCSV {
|
||||
sb.append(System.getProperty("line.separator"));
|
||||
|
||||
writer.write(sb.toString());
|
||||
|
||||
|
||||
if (!etudiants.isEmpty()) {
|
||||
|
||||
|
||||
for (String etud : this.etudiants.keySet()) {
|
||||
|
||||
|
||||
// Si etudiant HashMap est null, pas ecrit
|
||||
if (!(etud == null)) {
|
||||
|
||||
if (etud != null) {
|
||||
|
||||
if (this.isValid(etud)) {
|
||||
writer.write(etud+";"+etudiants.get(etud)+System.getProperty("line.separator"));
|
||||
writer.write(etud + ";" + etudiants.get(etud) + System.getProperty("line.separator"));
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
logger.debug("Null id not added to csv");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
logger.info("File creation succeed");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
logger.fatal("Students list for csv generation is empty");
|
||||
}
|
||||
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.fatal(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package ocr;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import java.io.File;
|
||||
@@ -9,6 +11,11 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
|
||||
@@ -21,12 +28,14 @@ public class GestionnaireCopies {
|
||||
public GestionnaireCopies(String chemin) {
|
||||
|
||||
copies = createImagesCopies(chemin);
|
||||
listeCopie = new ArrayList<Copie>();
|
||||
listeCopie = new ArrayList<>();
|
||||
|
||||
for(BufferedImage i : copies)
|
||||
{
|
||||
listeCopie.add(new Copie(i));
|
||||
|
||||
}
|
||||
applyOcr();
|
||||
}
|
||||
|
||||
public List<BufferedImage> createImagesCopies(String path){
|
||||
@@ -37,36 +46,83 @@ public class GestionnaireCopies {
|
||||
//LISTE DES IMAGES
|
||||
List<BufferedImage> images = new ArrayList<>(); // stockera les images (resultat)
|
||||
// CONVERT PAGES TO IMAGES
|
||||
|
||||
|
||||
try {
|
||||
String pdfFilesDirectory = "/Users/louis/Desktop/IUT_S3/PT/pt-s3t-g4/pdf";
|
||||
String pdfFilesDirectory = "E:\\S3T\\Projet\\pt-s3t-g4\\pdf";
|
||||
// nom du fichier pdf à ouvrir (TODO: changer le chemin)
|
||||
List<String> files = pdfAnalyzer.listAllFiles(pdfFilesDirectory, ".pdf");
|
||||
for (String fname : files) {
|
||||
pdfFile = new File(fname);
|
||||
document = PDDocument.load(pdfFile); // charge le fichier pdf cree pour le traiter
|
||||
images.addAll(pdfAnalyzer.convertPagesToBWJPG(document));
|
||||
|
||||
images.addAll(pdfAnalyzer.convertPagesToBWJPG(document,1));
|
||||
// appelle la methode qui convertit les pages en images (jpg) noir et blanches
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
/*
|
||||
File img = new File("E:\\S3T\\Projet\\pt-s3t-g4\\pdf\\img_0.jpg");
|
||||
|
||||
BufferedImage in;
|
||||
try {
|
||||
in = ImageIO.read(img);
|
||||
|
||||
Image tmp = in.getScaledInstance(500, 331, Image.SCALE_SMOOTH);
|
||||
BufferedImage dimg = new BufferedImage(500, 331, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
Graphics2D g2d = dimg.createGraphics();
|
||||
g2d.drawImage(tmp, 0, 0, null);
|
||||
g2d.dispose();
|
||||
|
||||
|
||||
|
||||
System.out.println(in.getHeight() + " " + in.getWidth());
|
||||
JFrame frame = new JFrame();
|
||||
frame.getContentPane().add(new JLabel(new ImageIcon(dimg)));
|
||||
//frame.getContentPane().add(new JLabel(new ImageIcon(images.get(0))));
|
||||
frame.setVisible(true);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
pdfAnalyzer.saveOnDisk(images, "E:\\S3T\\Projet\\pt-s3t-g4\\pdf\\");
|
||||
|
||||
|
||||
*/
|
||||
//LISTE DES IMAGES COMPRENANT L'IMAGE DE LA NOTE ET DU NUM ETUDIANT
|
||||
|
||||
return images;
|
||||
|
||||
}
|
||||
|
||||
public HashMap<String,String> createHashMapforCSV(){
|
||||
public Map<String,String> createHashMapforCSV(){
|
||||
|
||||
HashMap<String,String> temp = new HashMap<>();
|
||||
|
||||
|
||||
for(Copie c : listeCopie)
|
||||
{
|
||||
temp.put(c.getBase().gethMapImgs().get("NumEtu").getDescription(), c.getBase().gethMapImgs().get("Note").getDescription());
|
||||
{
|
||||
|
||||
String numEtu = c.getBase().gethMapImgs().get("NumEtu").getDescription();
|
||||
String noteEtu = c.getBase().gethMapImgs().get("Note").getDescription();
|
||||
|
||||
temp.put(numEtu, noteEtu);
|
||||
}
|
||||
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
public void applyOcr()
|
||||
{
|
||||
for(Copie c : listeCopie)
|
||||
{
|
||||
c.getBase().applyOcrForEach();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,10 +16,12 @@ public class ImagesCopie {
|
||||
|
||||
public void applyOcrForEach() {
|
||||
|
||||
for(String s : hMapImgs.keySet())
|
||||
{
|
||||
hMapImgs.get(s).applyOcrImg();
|
||||
for(Img s : hMapImgs.values())
|
||||
{
|
||||
s.applyOcrImg();
|
||||
System.out.println("Desc : "+s.getDescription());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Map<String, Img> gethMapImgs() {
|
||||
|
@@ -4,6 +4,7 @@ import java.awt.image.BufferedImage;
|
||||
|
||||
|
||||
|
||||
|
||||
public class ImgNote extends Img{
|
||||
|
||||
public ImgNote(BufferedImage img) {
|
||||
@@ -13,7 +14,9 @@ public class ImgNote extends Img{
|
||||
|
||||
@Override
|
||||
public void applyOcrImg() {
|
||||
|
||||
setDescription(OCR.applyOcrNumber(getImg()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ import java.awt.image.BufferedImage;
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
|
||||
public class OCR {
|
||||
public abstract class OCR {
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ public class OCR {
|
||||
Tesseract tesseract = new Tesseract();
|
||||
String str="";
|
||||
try {
|
||||
tesseract.setOcrEngineMode(2);
|
||||
tesseract.setDatapath("E:\\S3T\\Projet\\pt-s3t-g4\\NGCC\\Tess4J");
|
||||
|
||||
//tesseract.setOcrEngineMode(2);
|
||||
tesseract.setTessVariable("tessedit_char_whitelist","0-9");
|
||||
str=tesseract.doOCR(img);
|
||||
} catch (TesseractException e) {
|
||||
|
@@ -63,7 +63,7 @@ public class PdfToImage {
|
||||
return true;
|
||||
}
|
||||
|
||||
public ArrayList<BufferedImage> convertPagesToBWJPG(PDDocument document) {
|
||||
public ArrayList<BufferedImage> convertPagesToBWJPG(PDDocument document, int n) {
|
||||
// convertit chaque page d'un document pdf en image noir et blanc
|
||||
// retourne une array liste d'images
|
||||
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||
@@ -71,6 +71,9 @@ public class PdfToImage {
|
||||
try {
|
||||
int pageCounter = 0;
|
||||
for (PDPage page : document.getPages()) {
|
||||
if (pageCounter == n) {
|
||||
return images;
|
||||
}
|
||||
System.out.println("page.getRotation() : " + page.getRotation());
|
||||
System.out.println("pageCounter : " + pageCounter);
|
||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter++, 300, ImageType.BINARY); // BINARY =
|
||||
@@ -86,8 +89,9 @@ public class PdfToImage {
|
||||
return images;
|
||||
}
|
||||
|
||||
public void saveOnDisk(ArrayList<BufferedImage> images, String originalFileDir) {
|
||||
public void saveOnDisk(List<BufferedImage> images, String originalFileDir) {
|
||||
// sauvegarde sur le disque les images
|
||||
|
||||
int pageCounter = 0;
|
||||
try {
|
||||
for (BufferedImage img : images) {
|
||||
|
@@ -4,11 +4,18 @@ import java.awt.image.BufferedImage;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Rogneur {
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
public abstract class Rogneur {
|
||||
|
||||
|
||||
|
||||
// Retourne une hashmap contenant une image et la description de son contenu
|
||||
public static Map<String, Img> createHMapImgs(BufferedImage imgOriginale) {
|
||||
|
||||
|
||||
Map<String,Img> temp = new HashMap<>();
|
||||
temp.put("NumEtu", rogneurNumEtu(imgOriginale));
|
||||
temp.put("Note", rogneurNote(imgOriginale));
|
||||
@@ -20,19 +27,51 @@ public class Rogneur {
|
||||
// rogne la partie du numEtu
|
||||
public static Img rogneurNumEtu(BufferedImage imgOriginale)
|
||||
{
|
||||
return (new ImgNumEtu(imgOriginale.getSubimage((imgOriginale.getWidth()/4)+4
|
||||
, imgOriginale.getHeight()-imgOriginale.getHeight()+115
|
||||
, (imgOriginale.getWidth()/4+150)-(imgOriginale.getWidth()/4+4)
|
||||
, imgOriginale.getHeight()-imgOriginale.getHeight()+146-(imgOriginale.getHeight()-imgOriginale.getHeight()+115) )));
|
||||
int ratioX = (imgOriginale.getWidth()/595);
|
||||
int ratioY = (imgOriginale.getWidth()/841);
|
||||
|
||||
int numEtuX1 = ratioX*((595/4)+ 4);
|
||||
int numEtuY1 = ratioY*115;
|
||||
|
||||
int numEtuX2 = ratioX*((595/4)+150);
|
||||
int numEtuY2 = ratioY*146;
|
||||
|
||||
System.out.println("X1 + X2 : " +numEtuX1 + " " + numEtuX2);
|
||||
System.out.println("Y1 + Y2 : " +numEtuY1 + " " + numEtuY2);
|
||||
|
||||
BufferedImage temp = imgOriginale.getSubimage(numEtuX1, numEtuY1, numEtuX2-numEtuX1, numEtuY2-numEtuY1);
|
||||
|
||||
JFrame frame = new JFrame();
|
||||
frame.getContentPane().add(new JLabel(new ImageIcon(temp)));
|
||||
//frame.getContentPane().add(new JLabel(new ImageIcon(images.get(0))));
|
||||
frame.setVisible(true);
|
||||
|
||||
return new ImgNumEtu(temp);
|
||||
|
||||
}
|
||||
|
||||
// rogne la partie de la note
|
||||
public static Img rogneurNote(BufferedImage imgOriginale)
|
||||
{
|
||||
return (new ImgNumEtu(imgOriginale.getSubimage((imgOriginale.getWidth()/4)+4
|
||||
, imgOriginale.getHeight()-imgOriginale.getHeight()+160
|
||||
, (imgOriginale.getWidth()/4+150)-(imgOriginale.getWidth()/4+4)
|
||||
, imgOriginale.getHeight()-imgOriginale.getHeight()+200-(imgOriginale.getHeight()-imgOriginale.getHeight()+160) )));
|
||||
int ratioX = (imgOriginale.getWidth()/595);
|
||||
int ratioY = (imgOriginale.getWidth()/841);
|
||||
|
||||
int numNoteX1 = ratioX*((595/4) + 4);
|
||||
int numNoteY1 = ratioY*160;
|
||||
|
||||
int numNoteX2 = ratioX*((595/4) +75);
|
||||
int numNoteY2 = ratioY*200;
|
||||
|
||||
System.out.println("X1 + X2 : " +numNoteX1 + " " + numNoteX2);
|
||||
System.out.println("Y1 + Y2 : " +numNoteY1 + " " + numNoteY2);
|
||||
BufferedImage temp = imgOriginale.getSubimage(numNoteX1, numNoteY1, numNoteX2-numNoteX1, numNoteY2-numNoteY1);
|
||||
|
||||
JFrame frame = new JFrame();
|
||||
frame.getContentPane().add(new JLabel(new ImageIcon(temp)));
|
||||
//frame.getContentPane().add(new JLabel(new ImageIcon(images.get(0))));
|
||||
frame.setVisible(true);
|
||||
|
||||
return new ImgNote(temp);
|
||||
}
|
||||
|
||||
// rogne la partie du format de la note
|
||||
|
Reference in New Issue
Block a user