How to generate QR code with logo inside it?

前端 未结 2 486
余生分开走
余生分开走 2020-12-05 20:59

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 li

相关标签:
2条回答
  • 2020-12-05 21:24

    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

    0 讨论(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.

    0 讨论(0)
提交回复
热议问题