如何生成里面有logo的二维码?

How to generate QR code with logo inside it?

我正在为 Android 设备开发应用程序。 我想生成带有logo的二维码

有了 ZXing 我知道如何生成像这样的简单二维码:

但我想生成带有徽标的二维码。 所以我想得到这样的东西:

有什么办法吗? 我不知道该怎么做。 请问你能帮帮我吗?可能有一些现成的库或如何做的例子。

谢谢!

您可以将徽标添加为 图像叠加层,例如

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;
}

请参考此post & github了解更多信息

我创建了以下 Kotlin 扩展,它将一个位图添加到另一个位图的中心:

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
}

可以找到.

网上有很多二维码生成器,比如https://app.aotol.com/qr/api

您可以只引用 QR 图像 url,例如

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

Kotlin 中使用 zxing 库和来自资产文件夹的覆盖。

  • 需要更正,因为覆盖会隐藏部分二维码;

  • Class MatrixToImageWriter 来自: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
       }
    }