Skip to content

Instantly share code, notes, and snippets.

@wombat
Last active December 5, 2023 12:07
Show Gist options
  • Save wombat/8825038 to your computer and use it in GitHub Desktop.
Save wombat/8825038 to your computer and use it in GitHub Desktop.
QR-Code with embedded logo
// Create new configuration that specifies the error correction
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// Create a qr code with the url as content and a size of 250x250 px
bitMatrix = writer.encode("http://www.wombatsoftware.de", BarcodeFormat.QR_CODE, 250, 250, hints);
MatrixToImageConfig config = new MatrixToImageConfig(MatrixToImageConfig.BLACK, MatrixToImageConfig.WHITE);
// Load QR image
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
// Load logo image
BufferedImage logoImage = ImageIO.read(request.getSession().getServletContext().getResourceAsStream("/images/logo.png"));
// Calculate the delta height and width between QR code and logo
int deltaHeight = qrImage.getHeight() - logoImage.getHeight();
int deltaWidth = qrImage.getWidth() - logoImage.getWidth();
// Initialize combined image
BufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) combined.getGraphics();
// Write QR code to new image at position 0/0
g.drawImage(qrImage, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
// Write logo into combine image at position (deltaWidth / 2) and
// (deltaHeight / 2). Background: Left/Right and Top/Bottom must be
// the same space for the logo to be centered
g.drawImage(logoImage, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null);
// Write combined image as PNG to OutputStream
ImageIO.write(combined, "png", baos);
} catch (WriterException e) {
LOG.error("WriterException occured", e);
} catch (IOException e) {
LOG.error("IOException occured", e);
}
// Do something with the OutputStream
@Chaosology-Asartheen
Copy link

how can i specify the name of the file

@lome
Copy link

lome commented Jan 12, 2021

You can check out this small library I wrote on top of the ZXing library.

@SimonScholz
Copy link

SimonScholz commented Oct 23, 2023

You might also want to check out the highly customizable qr-code-with-logo library for this purpose.

image

Gradle:

dependencies {
    implementation("io.github.simonscholz:qr-code-with-logo:0.3.0")
}

Maven:

<dependency>
    <groupId>io.github.simonscholz</groupId>
    <artifactId>qr-code-with-logo</artifactId>
    <version>0.3.0</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment