Skip to content

Instantly share code, notes, and snippets.

@Youmoo
Forked from wombat/QRAndLogo.java
Created September 5, 2017 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Youmoo/81d919f1a84543e6dc6b7eb5a1bf6dd8 to your computer and use it in GitHub Desktop.
Save Youmoo/81d919f1a84543e6dc6b7eb5a1bf6dd8 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment