14

I am developing the application for Android devices. I want to generate QR code with logo inside it.

With ZXing I know how to generate simple QR codes like this one: Original

But I want to generate QR code with logo inside it. So I want to get something like this: With Logo

Is there any way to do it? I have no idea how to do it. Could you help me please? May there is some ready library or example of how to do it.

Thank you!

Community
  • 1
  • 1
Huitarheroherohero
  • 315
  • 1
  • 4
  • 11
  • You may want to refer to one of these questions: https://stackoverflow.com/questions/13247701/how-to-add-a-logo-to-qr-code-in-android, https://stackoverflow.com/questions/18296555/how-to-add-logo-to-qr-codes – Andrew Brooke Jan 30 '16 at 16:51
  • 1
    http://www.qrcode-monkey.com/ – Yuliia Ashomok Jan 30 '16 at 16:55
  • Possible duplicate of [Is there a library to create Design QR codes with Java?](http://stackoverflow.com/questions/4322881/is-there-a-library-to-create-design-qr-codes-with-java) – rds Feb 14 '16 at 15:46

4 Answers4

13

You can add your logo it as an Image Overlay like

public BufferedImage getQRCodeWithOverlay(BufferedImage qrcode) 
{
    BufferedImage scaledOverlay = scaleOverlay(qrcode);

    Integer deltaHeight = qrcode.getHeight() - scaledOverlay.getHeight();
    Integer deltaWidth  = qrcode.getWidth()  - scaledOverlay.getWidth();

    BufferedImage combined = new BufferedImage(qrcode.getWidth(), qrcode.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D)combined.getGraphics();
    g2.drawImage(qrcode, 0, 0, null);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, overlayTransparency));
    g2.drawImage(scaledOverlay, Math.round(deltaWidth/2), Math.round(deltaHeight/2), null);
    return combined;
}

private BufferedImage scaleOverlay(BufferedImage qrcode)
{
    Integer scaledWidth = Math.round(qrcode.getWidth() * overlayToQRCodeRatio);
    Integer scaledHeight = Math.round(qrcode.getHeight() * overlayToQRCodeRatio);

    BufferedImage imageBuff = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics g = imageBuff.createGraphics();
    g.drawImage(overlay.getScaledInstance(scaledWidth, scaledHeight, BufferedImage.SCALE_SMOOTH), 0, 0, new Color(0,0,0), null);
    g.dispose();
    return imageBuff;
}

Please refer this post & github for more info

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
  • How does this guarantee that the image is small enough such that there is still enough error correction information for a reader to parse it? Your image doesn't scan whatsoever on my device, while the OP's example image does so easily. -1. – nanofarad Feb 14 '16 at 15:28
  • How would the user be able to determine by how much to scale it? A real solution to this is aware of the exact error correction information in a QR code and how it is laid out, and uses that to determine a feasibly-scannable overlay size. – nanofarad Feb 14 '16 at 16:54
  • I think, OP's requirement was to embed his logo, may be as a transparent overlay image, which doesn't affect the readability of QR code. the ratio to which the logo is scaled was not a specification to the question. And I just tried to help him with the method helped me earlier. Thanks for raising this concern, will surely update it with a perfect solution. – Let'sRefactor Feb 14 '16 at 17:27
  • Where is logo? I mean where we are giving logo image? – Ali Akram Sep 18 '21 at 06:35
1

In Kotlin with zxing library and overlay from assets folder.

  • The correction needs to be made because the overlay will hide some part of the QR Code;

  • Class MatrixToImageWriter is from: https://github.com/kenglxn/QRGen

    private fun generateQrCodeWithOverlay(qrCodeData: String): Bitmap? {
    
       val hints = HashMap<EncodeHintType?, Any?>()
       // The Error Correction level H provide a QR Code that can be covered by 30%
       hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H
    
       val writer = QRCodeWriter()
    
       return try {
           // Create a QR Code from qrCodeData and 512 by 512 pixels, same size as my Logo
           val encodedQrCode = writer.encode(qrCodeData, BarcodeFormat.QR_CODE, 512, 512, hints)
    
           var qrCodeBitmap: Bitmap = MatrixToImageWriter.toBitmap(encodedQrCode)
    
           val qrCodeCanvas = Canvas(qrCodeBitmap)
    
           // Used to resize the image
           val scaleFactor = 4
    
           val logo =
             BitmapFactory.decodeStream(app.assets.open("path/to/your/logo.png"))
    
           // Resizing the logo increasing the density to keep it sharp
           logo.density = logo.density * scaleFactor
    
           val xLogo = (512 - logo.width / scaleFactor) / 2f
           val yLogo = (512 - logo.height / scaleFactor) / 2f
    
           qrCodeCanvas.drawBitmap(logo, xLogo, yLogo, null)
    
           qrCodeBitmap
       } catch (e: Exception) {
           // handle errors
           null
       }
    }
    
  • I import the library but don't found "EncodeHintType" "QRCodeWriter" and "ErrorCorrectionLevel" – Md Imran Choudhury Dec 02 '21 at 09:12
  • Did you import the two libraries, zxing and QRGen? Because [EncodeHintType](https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/EncodeHintType.java), [QRCodeWriter](https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/qrcode/QRCodeWriter.java) and [ErrorCorrectionLevel](https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/qrcode/decoder/ErrorCorrectionLevel.java) belongs to zxing library. – Leonardo Ferreira Dec 06 '21 at 13:36
0

I created the following Kotlin Extention, which adds a Bitmap to the centre of another Bitmap:

fun Bitmap.addOverlayToCenter(overlayBitmap: Bitmap): Bitmap {

    val bitmap2Width = overlayBitmap.width
    val bitmap2Height = overlayBitmap.height
    val marginLeft = (this.width * 0.5 - bitmap2Width * 0.5).toFloat()
    val marginTop = (this.height * 0.5 - bitmap2Height * 0.5).toFloat()
    val canvas = Canvas(this)
    canvas.drawBitmap(this, Matrix(), null)
    canvas.drawBitmap(overlayBitmap, marginLeft, marginTop, null)
    return this
}

Can find my full solution here.

Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53
  • Yes, if you set hashMapOf(EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.H), check out the "my full solution here" link for more information. – Paul Spiesberger Jan 29 '20 at 09:15
-1

There is plenty online QR code generator, such as https://app.aotol.com/qr/api

You can just reference the QR image url to it such as

<img src="https://app.aotol.com/qr/api?qr_content=https://wwww.google.com&qr_logo=https://blog.hubspot.com/hubfs/image8-2.jpg">
Zhang Zhan
  • 815
  • 8
  • 27