1

I have already created a QR code in java. I want to send it as an email in an HTML template (img<src=???>). But I don't want to save that QR code image file on my disk.

Is there any way to put it as a byte array (or something like that...IDK) into an HTML template?

try {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 450, 450);
        File qrImg = new File("qr-code.png");
        BufferedImage bfi = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ImageIO.write(bfi, "PNG", qrImg);
        return qrImg;
    } catch (IOException | WriterException e) {
        throw new RuntimeException(e);
    }
  • Do you store your HTML template on your disk? – Lajos Arpad Mar 13 '23 at 11:03
  • @LajosArpad It stores as a String variable (String email = """
    """)
    – ItsMeCastiel Mar 13 '23 at 11:10
  • 2
    You can convert BufferedImage to byte array like this https://stackoverflow.com/questions/15414259/java-bufferedimage-to-byte-array-and-back encode the byte array to a base64 string like this https://stackoverflow.com/questions/2418485/how-do-i-convert-a-byte-array-to-base64-in-java and use HTMLs capability to render base64 encoded images like this https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html – Tarmo Mar 13 '23 at 11:11
  • Did you try generating the QR code and adding it to your String? – Lajos Arpad Mar 13 '23 at 11:27
  • @LajosArpad yes, But most email services won't support base64 images – ItsMeCastiel Mar 13 '23 at 12:11
  • Can you upload the image to a trusted server? – Lajos Arpad Mar 13 '23 at 12:17

1 Answers1

0

I resolved this by using cid in HTML img tag

Link: https://stackoverflow.com/a/29205203/19317541