【发布时间】:2016-05-08 08:32:43
【问题描述】:
我正在为 Android 设备开发应用程序。 我想生成带有标志的二维码。
有什么办法吗? 我不知道该怎么做。 请问你能帮帮我吗?可能有一些现成的库或如何做到这一点的例子。
谢谢!
【问题讨论】:
标签: java android qr-code zxing
我正在为 Android 设备开发应用程序。 我想生成带有标志的二维码。
有什么办法吗? 我不知道该怎么做。 请问你能帮帮我吗?可能有一些现成的库或如何做到这一点的例子。
谢谢!
【问题讨论】:
标签: java android qr-code 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;
}
【讨论】:
我创建了以下 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
}
【讨论】:
在 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
}
}
【讨论】:
网上有很多二维码生成器,比如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">
【讨论】: