Skip to content
Manuel Bl edited this page Jan 17, 2019 · 4 revisions

Vector file formats (such as PDF or SVG) are a good fit for QR bills. Raster image file formats such as PNG are a bad fit for QR bills as the QR code requires a high resolution and does not properly align with the raster. (JPEG is particularly bad as the compression is optimized for photographs and not document). In certain situations however, PNG is the only option.

To generate PNG files, create the QR bill by passing a PNGCanvas instance:

package net.codecrete.qrbill.examples;

import net.codecrete.qrbill.generator.Address;
import net.codecrete.qrbill.generator.Bill;
import net.codecrete.qrbill.generator.QRBill;

import net.codecrete.qrbill.canvas.PNGCanvas;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class QRBillExample {

    public static void main(String[] args) {

        // Setup bill
        Bill bill = new Bill();
        bill.setLanguage(Bill.Language.fr);
        bill.setAccount("CH4431999123000889012");
        // ...
        // see https://github.com/manuelbl/SwissQRBill
        // ...
        debtor.setCountryCode("CH");
        bill.setDebtor(debtor);

        PNGCanvas canvas = new PNGCanvas(144);
        byte[] png = QRBill.generate(bill, canvas);

        Path path = Paths.get("qrbill.png");
        try {
            Files.write(path, png);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("QR bill saved at " + path.toAbsolutePath());
    }
}

Also see C# code

To create the PNGCanvas instance you have to specify the PNG resolution (in pixels per inch). A resolution of at least 144 pixel/in is recommended as the QR code can have rather small blocks that should still be readable by a QR code reader. 144 pixel/in is what Apple would call "Retina" or what is known as "High DPI" in Windows.

Note: The generation of PNG files requires far more computing power (takes far more time) than the generation of PDF or SVG files.