Android实现图片和二维码拼在一起
- 依赖
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.7'
- 使用依赖里的方法生成二维码
QRCodeEncoder.syncEncodeQRCode(CODE_URL, 100)
- 自己选择一张图片和上面的二维码合并
private fun combineBitmap(bmp: Bitmap, code: Bitmap): Bitmap {
val textPaint = Paint()
textPaint.isAntiAlias = true
textPaint.textSize = 40f
textPaint.color = Color.parseColor("#000000")
val bgWidth = bmp.width
val bgHeight = bmp.height
val fgWidth = code.width
val fgHeight = code.height
val newBmp = Bitmap.createBitmap(
bgWidth,
bgHeight + fgHeight + fgHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(newBmp)
canvas.drawRGB(255, 255, 255)
canvas.drawBitmap(bmp, 0.toFloat(), 0.toFloat(), null)
canvas.drawBitmap(code, bgWidth.toFloat() / 8, bgHeight.toFloat(), null)
canvas.drawText(
"长按识别二维码",
bgWidth.toFloat() / 3,
bgHeight.toFloat() + fgWidth.toFloat() / 2,
textPaint
)
canvas.save()
canvas.restore()
return newBmp
}