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

新手上路,请多包涵

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

使用 ZXing 我知道如何生成像这样的简单二维码: 原来的

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

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

谢谢!

原文由 Huitarheroherohero 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 654
2 个回答

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

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 以获取更多信息

原文由 Let‘sRefactor 发布,翻译遵循 CC BY-SA 3.0 许可协议

Kotlin 中,使用 zxing 库和资产文件夹中的叠加层。

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

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

原文由 Leonardo Ferreira 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题