javaandroidqr-codezxing

How to generate QR code with logo inside it?


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!


Solution

  • 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