
br.com.jhonsapp.bootstrap.document.service.ocr.OpticalCharacterRecognitionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bootstrap-document Show documentation
Show all versions of bootstrap-document Show documentation
A complete architecture for creating and managing documents.
The newest version!
package br.com.jhonsapp.bootstrap.document.service.ocr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.asprise.ocr.Ocr;
import br.com.jhonsapp.bootstrap.document.model.Document;
import br.com.jhonsapp.bootstrap.document.service.exception.DocumentServiceException;
import br.com.jhonsapp.bootstrap.document.service.message.DocumentMessage;
import br.com.jhonsapp.bootstrap.document.service.ocr.xml.DocumentXML;
import br.com.jhonsapp.bootstrap.document.service.util.DocumentUtil;
import br.com.jhonsapp.repository.util.FileUtil;
@Service
public class OpticalCharacterRecognitionService {
@Autowired
private LocalStorageDocumentPdf localStorage;
public boolean inspectDocument(byte[] documentByte, Document document) {
String xmlBarcode = extractXmlBarcodeByOCR(documentByte);
DocumentXML documentXML = DocumentXML.buildDocumentXMLByStringXml(xmlBarcode);
//Quantidade de páginas diferente do documento original.
if(hasNumberPagesDifferentFromOriginalDocument(documentXML, document))
throw new DocumentServiceException(DocumentMessage.number_pages_different_original_document);
//Documento não possui código de barra.
if(hasPagesWithoutBarcode(documentXML, document))
throw new DocumentServiceException(DocumentMessage.pages_without_barcode);
//Documento contendo páginas sem código de barra ou paginas de outro documento.
if(hasPagesFromAnotherDocument(documentXML, document))
throw new DocumentServiceException(DocumentMessage.pages_from_another_document);
return true;
}
public String extractXmlBarcodeByOCR(byte[] documentByte){
if(FileUtil.isInvalid(documentByte))
throw new DocumentServiceException(DocumentMessage.invalid_document);
String destination = localStorage.savePDF(documentByte);
Ocr.setUp(); // one time setup
Ocr ocr = new Ocr(); // create a new OCR engine
ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
String xmlBarcode = ocr.recognize(destination, -1, 0, 0, 0, 50,
Ocr.RECOGNIZE_TYPE_BARCODE, Ocr.OUTPUT_FORMAT_XML);
localStorage.deletePDF(destination);
return xmlBarcode;
}
private boolean hasNumberPagesDifferentFromOriginalDocument(DocumentXML documentOcr, Document document) {
return documentOcr.getPage().length != document.getPages();
}
private boolean hasPagesWithoutBarcode(DocumentXML documentOcr, Document document) {
boolean result = false;
for(int i = 0; i < document.getPages(); i++) {
if(!StringUtils.hasText(documentOcr.getPage()[i].getBlock())) {
result = true;
break;
}
}
return result;
}
private boolean hasPagesFromAnotherDocument(DocumentXML documentOcr, Document document) {
boolean result = false;
for(int i = 0; i < document.getPages(); i++) {
String code = DocumentUtil.createCode(document.getCode(), i + 1);
if(!documentOcr.getPage()[i].getBlock().equals(code)) {
result = true;
break;
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy