We have a PDF-file with around 130 pages. We loop through the pages and try to recognize barcodes. The code looks in general like this:
public BarcodeDokument scanBarcodeDokument(byte[] pdfContent, String dokumentId) {
BarcodeDokument barcodeDokument = new BarcodeDokument();
barcodeDokument.setErstelltAm(new Date());
barcodeDokument.setDokumentId(dokumentId);
barcodeDokument.setBarcodes(getBarcodes(pdfContent, barcodeDokument));
return barcodeDokument;
}
private List<Barcode> getBarcodes(byte[] pdfContent, BarcodeDokument barcodeDokument) {
List<Barcode> foundBarcodes = new ArrayList<>();
try (PDDocument pdDocument = Loader.loadPDF(pdfContent)) {
PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
int numberOfPages = pdDocument.getNumberOfPages();
barcodeDokument.setSeitenanzahl(numberOfPages);
for (int page = 0; page < numberOfPages; ++page) {
BufferedImage pdfImageFromPage = pdfRenderer.renderImageWithDPI(page, 100, ImageType.ARGB);
BarCodeReader reader = getBarcodeReader(pdfImageFromPage);
foundBarcodes.addAll(readBarcodes(reader, page));
pdfImageFromPage.flush();
reader.dispose();
}
} catch (InvalidPasswordException ipe) {
logger.warn(ipe.getMessage(), ipe);
return List.of();
} catch (IOException e) {
logger.error(e.getMessage(), e);
return List.of();
}
return foundBarcodes;
}
public BarCodeReader getBarcodeReader(BufferedImage pdfImageFromPage) {
BarCodeReader reader = new BarCodeReader(pdfImageFromPage, new MultyDecodeType(DecodeType.CODE_128, DecodeType.DATA_MATRIX));
reader.setQualitySettings(QualitySettings.getHighQualityDetection());
reader.setTimeout(60000);
return reader;
}
public List<Barcode> readBarcodes(BarCodeReader reader, int page) {
List<Barcode> result = new ArrayList<>();
for (BarCodeResult barCodeResult : reader.readBarCodes()) {
Barcode barcode = new Barcode();
barcode.setBarcodeformat(Barcodeformat.getBarcodeformat(barCodeResult.getCodeType()));
barcode.setInhalt(barCodeResult.getCodeText());
barcode.setSeite(page);
result.add(barcode);
}
return result;
}
The first thing to recognize is that there is a reader dispose, which seems from the .net world. Java should use try-with-resources and clear the memory.
The second is that the memory is getting higher and higher. For the 130 pages pdf file around 11GB memory. It doesn’t matter if you reuse the BarCodeReader instance or if you always create a new one. Maybe there is a little memory leak in the implementation.