Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
362c3eb11e
37
NGCC/src/ocr_orm/ImageNGCC.java
Normal file
37
NGCC/src/ocr_orm/ImageNGCC.java
Normal file
@ -0,0 +1,37 @@
|
||||
package ocr_orm;
|
||||
|
||||
import java.awt.image.*;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.pdfbox.rendering.ImageType;
|
||||
|
||||
import java.awt.Image;
|
||||
@SuppressWarnings("unused")
|
||||
public class ImageNGCC {
|
||||
|
||||
BufferedImage imgcopie ;
|
||||
BufferedImage imgNumEtu;
|
||||
BufferedImage imgNote;
|
||||
|
||||
public ImageNGCC(BufferedImage buf){
|
||||
imgcopie=buf;
|
||||
imgNumEtu = imgcopie.getSubimage(0,0, 100, 50);
|
||||
imgNote = imgcopie.getSubimage(50,50,100,50);
|
||||
}
|
||||
|
||||
public BufferedImage getImgNumEtu() {
|
||||
return imgNumEtu;
|
||||
}
|
||||
|
||||
public BufferedImage getImgNote() {
|
||||
return imgNote;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
//https://docs.oracle.com/javase/tutorial/2d/images/drawimage.html
|
46
NGCC/src/ocr_orm/ListeImageNGCC.java
Normal file
46
NGCC/src/ocr_orm/ListeImageNGCC.java
Normal file
@ -0,0 +1,46 @@
|
||||
package ocr_orm;
|
||||
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
;
|
||||
|
||||
|
||||
|
||||
public class ListeImageNGCC {
|
||||
|
||||
private ArrayList<ImageNGCC> listeImage = new ArrayList<ImageNGCC>();
|
||||
|
||||
|
||||
public ListeImageNGCC(ArrayList<BufferedImage> liste) {
|
||||
|
||||
for(int i=0; i<liste.size();i++)
|
||||
{
|
||||
//ON AJOUTE UNE IMAGE NGCC CREE A PARTIR DE CHAQUE IMAGE DE LA LISTE D'IMAGE BUF
|
||||
listeImage.add(new ImageNGCC(liste.get(i)) );
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<ImageNGCC> getListeImage(ArrayList<BufferedImage> liste) {
|
||||
return listeImage;
|
||||
}
|
||||
|
||||
public int taille() {
|
||||
return listeImage.size();
|
||||
}
|
||||
|
||||
public HashMap<String, String> doOCR(){
|
||||
HashMap<String,String> maMap = new HashMap<String, String>();
|
||||
OCR ocr = new OCR();
|
||||
for (int i=0;i<listeImage.size();i++) {
|
||||
String strNote;
|
||||
String strNumEtu;
|
||||
strNote = ocr.getOCR(listeImage.get(i).getImgNote());
|
||||
strNumEtu = ocr.getOCR(listeImage.get(i).getImgNumEtu());
|
||||
maMap.put(strNumEtu, strNote);
|
||||
}
|
||||
return maMap;
|
||||
}
|
||||
|
||||
}
|
46
NGCC/src/ocr_orm/Main.java
Normal file
46
NGCC/src/ocr_orm/Main.java
Normal file
@ -0,0 +1,46 @@
|
||||
package ocr_orm;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String args[]) throws IOException {
|
||||
|
||||
PdfToImage pdfAnalyzer = new PdfToImage();
|
||||
File pdfFile;
|
||||
PDDocument document = null;
|
||||
//LISTE DES IMAGES
|
||||
ArrayList<BufferedImage> images = new ArrayList<>(); // stockera les images (resultat)
|
||||
//HASHMAP POUR LE CSV
|
||||
HashMap<String,String> listeNumNote = new HashMap<String, String>();
|
||||
|
||||
// CONVERT PAGES TO IMAGES
|
||||
try {
|
||||
String pdfFilesDirectory = "C:\\Users\\ph807242\\eclipse-workspace\\PT\\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));
|
||||
// appelle la methode qui convertit les pages en images (jpg) noir et blanches
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
//LISTE DES IMAGES COMPRENANT L'IMAGE DE LA NOTE ET DU NUM ETUDIANT
|
||||
ListeImageNGCC liNGCC = new ListeImageNGCC(images);
|
||||
|
||||
listeNumNote = liNGCC.doOCR();
|
||||
|
||||
|
||||
}
|
||||
}
|
18
NGCC/src/ocr_orm/OCR.java
Normal file
18
NGCC/src/ocr_orm/OCR.java
Normal file
@ -0,0 +1,18 @@
|
||||
package ocr_orm;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
|
||||
public class OCR {
|
||||
|
||||
|
||||
|
||||
public String getOCR(BufferedImage img) {
|
||||
//FAIRE L'OCR
|
||||
String str="";
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
101
NGCC/src/ocr_orm/PdfToImage.java
Normal file
101
NGCC/src/ocr_orm/PdfToImage.java
Normal file
@ -0,0 +1,101 @@
|
||||
package ocr_orm;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.rendering.ImageType;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
|
||||
public class PdfToImage {
|
||||
|
||||
public List<String> listAllFiles(String directory, String extension) {
|
||||
// https://www.mkyong.com/java/java-how-to-list-all-files-in-a-directory/
|
||||
List<String> files = new ArrayList<String>();
|
||||
try (Stream<Path> walk = Files.walk(Paths.get(directory))) {
|
||||
// voir simplification si necessaire
|
||||
files = walk.map(x -> x.toString()).filter(f -> f.endsWith(extension)).collect(Collectors.toList());
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public BufferedImage blackWhiteConvert(BufferedImage image) {
|
||||
// Convertit une image en image en noir et blanc
|
||||
// TODO : voir recursivite
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
if (image.getRGB(x, y) < 128) {
|
||||
image.setRGB(x, y, 0);
|
||||
} else {
|
||||
image.setRGB(x, y, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
public boolean isBlackWhite(BufferedImage image) {
|
||||
// verifie si une image est en noir et blanc
|
||||
// TODO : voir recursivite
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
if ((image.getRGB(x, y) != 0) || (image.getRGB(x, y) != 255)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ArrayList<BufferedImage> convertPagesToBWJPG(PDDocument document) {
|
||||
// convertit chaque page d'un document pdf en image noir et blanc
|
||||
// retourne une array liste d'images
|
||||
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||
try {
|
||||
int pageCounter = 0;
|
||||
for (PDPage page : document.getPages()) {
|
||||
System.out.println("page.getRotation() : " + page.getRotation());
|
||||
System.out.println("pageCounter : " + pageCounter);
|
||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter++, 300, ImageType.BINARY); // BINARY =
|
||||
// noir et
|
||||
// blanc
|
||||
images.add(bim);
|
||||
System.out.println("Ajout n°" + pageCounter);
|
||||
}
|
||||
// document.close();
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
public void saveOnDisk(ArrayList<BufferedImage> images, String originalFileDir) {
|
||||
// sauvegarde sur le disque les images
|
||||
int pageCounter = 0;
|
||||
try {
|
||||
for (BufferedImage img : images) {
|
||||
ImageIO.write(img, "JPEG", new File(originalFileDir + "img_" + pageCounter++ + ".jpg"));
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user