【问题标题】:How to generate QR code with logo inside it?如何生成带有徽标的二维码?
【发布时间】:2016-05-08 08:32:43
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

标签: java android qr-code zxing


【解决方案1】:

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

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 了解更多信息

【讨论】:

  • 这如何保证图像足够小,以至于仍有足够的纠错信息供读者解析?您的图像不会在我的设备上进行任何扫描,而 OP 的示例图像则可以轻松扫描。 -1.
  • 用户如何确定要缩放多少?真正的解决方案是了解 QR 码中的确切纠错信息及其布局,并使用它来确定可行的可扫描覆盖大小。
  • 我认为,OP的要求是嵌入他的标志,可能是透明的叠加图像,不影响二维码的可读性。徽标的比例不是问题的规范。而我只是试图用之前帮助我的方法帮助他。感谢您提出这个问题,一定会用完美的解决方案更新它。
  • 标志在哪里?我的意思是我们在哪里提供徽标图像?
【解决方案2】:

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

可以找到my full solution here

【讨论】:

  • 是的,如果您将 hashMapOf(EncodeHintType.ERROR_CORRECTION 设置为 ErrorCorrectionLevel.H),请查看“我的完整解决方案”链接以获取更多信息。
【解决方案3】:

Kotlin 中使用 zxing 库并覆盖 assets 文件夹。

  • 需要进行更正,因为叠加层会隐藏部分二维码;

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

【讨论】:

【解决方案4】:

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

你可以直接引用二维码图片的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">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多