This example shows how to scan documents (graphics or PDF) for one or multiple Swiss QR Codes.

import ch.codeblock.qrinvoice.MimeType;
import ch.codeblock.qrinvoice.QrInvoiceDocumentScanner;
import ch.codeblock.qrinvoice.model.QrInvoice;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

// ...


// either provide an input stream or a byte array
final InputStream imageInputStream = ...;

try {
    // this option uses the default QR Code library and performs default scanning efforts
    final QrCodeReaderOptions defaultOption = new QrCodeReaderOptions(ScanningEffortLevel.DEFAULT, QrCodeLibrary.DEFAULT);

    // You may however increase scanning efforts by choosing level "HARDER", which may result in much slower operations but higher detection rate
    // new QrCodeReaderOptions(ScanningEffortLevel.HARDER, QrCodeLibrary.DEFAULT);

    // you may also choose to use BoofCV in addition to ZXing using any available ScanningEffortLevel combination
    // new QrCodeReaderOptions(ScanningEffortLevel.DEFAULT, QrCodeLibrary.ZXING, QrCodeLibrary.BOOFCV);
    // new QrCodeReaderOptions(ScanningEffortLevel.HARDER, QrCodeLibrary.ZXING, QrCodeLibrary.BOOFCV);

    final Optional<QrInvoice> qrInvoiceOptional = QrInvoiceDocumentScanner.create(MimeType.PNG, defaultOption).scanDocumentUntilFirstSwissQrCode(imageInputStream);

    if (qrInvoiceOptional.isPresent()) {
        final QrInvoice qrInvoice = qrInvoiceOptional.get();

        // access QR-Invoice structure
        final BigDecimal amount = qrInvoice.getPaymentAmountInformation().getAmount();
        // ...
    } else {
        // no Swiss QR Code found
    }
} catch (IOException e) {
    // io exception during read
}

Instead of only scanning for one QR Code it is possible to scan for all Swiss QR Codes in a document.

try {
    final List<QrInvoice> qrInvoices = QrInvoiceDocumentScanner.create(MimeType.PNG).scanDocumentForAllSwissQrCodes(imageInputStream);

    for (final QrInvoice qrInvoice : qrInvoices) {
        // access QR-Invoice structure
        final BigDecimal amount = qrInvoice.getPaymentAmountInformation().getAmount();
        // ...
    }
} catch (IOException e) {
    // io exception during read
}

Please refer to How to Validate a QrInvoice Object for more information about validation.