如何生成带logo的二维码?

问题描述 投票:0回答:5

我正在开发适用于 Android 设备的应用程序。 我想生成带有徽标的二维码。

有了

ZXing
,我知道如何生成简单的二维码,如下所示:

但是我想生成里面有logo的二维码。 所以我想得到这样的东西:

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

谢谢!

java android qr-code zxing
5个回答
14
投票

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

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

请参阅此帖子github了解更多信息


3
投票

Kotlin 中,使用 zxing 库并覆盖来自 asset 文件夹的内容。

  • 需要进行修正,因为叠加会隐藏QR码的某些部分;

  • 类 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
       }
    }
    

0
投票

我创建了以下 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
}

可以在这里找到我的完整解决方案


0
投票

您可能还想尝试我的库,它提供了许多不同的二维码样式和方便的选项来添加徽标作为图像或 Base64 字符串。

您可以在这里查看:https://github.com/SimonScholz/qr-code-with-logo或直接在maven cental上查看:https://central.sonatype.com/artifact/io.github。 simonscholz/带有徽标的二维码

在自述文件中,您可以找到很多示例,并且存储库包含 Java 和 Kotlin 的示例。

我什至创建了一个小型 Swing 桌面应用程序来创建二维码,它也可以为您生成代码。

目前我也在致力于支持SVG图像。


-1
投票

有很多在线二维码生成器,例如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">
© www.soinside.com 2019 - 2024. All rights reserved.