fix: 恢复二维码为iPhone风格整体圆角裁剪(12%)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-27 17:57:41 +09:30
parent 30f7ac1206
commit c244011c96

View File

@@ -86,77 +86,35 @@ class BindFragment : BaseFragment<FragmentBindBinding>() {
} }
} }
/** /** 将 ZXing BitMatrix 转换为 Bitmap整体图片加圆角裁剪 */
* 将 ZXing BitMatrix 转换为圆角风格二维码 Bitmap
* 每个模块(小方块)都是圆角矩形,白色背景带圆角
*/
private fun bitMatrixToBitmap(matrix: BitMatrix): Bitmap { private fun bitMatrixToBitmap(matrix: BitMatrix): Bitmap {
// 找到二维码实际内容边界(去掉 ZXing 空白边距) val width = matrix.width
val w = matrix.width val height = matrix.height
val h = matrix.height // 生成标准二维码 Bitmap
var left = w; var top = h; var right = 0; var bottom = 0 val pixels = IntArray(width * height)
for (y in 0 until h) { for (y in 0 until height) {
for (x in 0 until w) { for (x in 0 until width) {
if (matrix[x, y]) { pixels[y * width + x] = if (matrix[x, y]) {
if (x < left) left = x 0xFF000000.toInt()
if (x > right) right = x } else {
if (y < top) top = y 0xFFFFFFFF.toInt()
if (y > bottom) bottom = y
} }
} }
} }
val qrCols = right - left + 1 val squareBitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
val qrRows = bottom - top + 1
// 输出参数 // 圆角裁剪iPhone 风格12% 圆角)
val moduleSize = 16f // 每个模块像素大小 val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val padding = moduleSize * 2 // 内边距 val canvas = android.graphics.Canvas(output)
val outputSize = (maxOf(qrCols, qrRows) * moduleSize + padding * 2).toInt() val paint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG)
val rect = android.graphics.RectF(0f, 0f, width.toFloat(), height.toFloat())
val radius = width * 0.12f
canvas.drawRoundRect(rect, radius, radius, paint)
paint.xfermode = android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(squareBitmap, 0f, 0f, paint)
squareBitmap.recycle()
val bitmap = Bitmap.createBitmap(outputSize, outputSize, Bitmap.Config.ARGB_8888) return output
val canvas = android.graphics.Canvas(bitmap)
// 白色圆角背景
val bgPaint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply {
color = 0xFFFFFFFF.toInt()
style = android.graphics.Paint.Style.FILL
}
val bgRadius = outputSize * 0.08f
canvas.drawRoundRect(
android.graphics.RectF(0f, 0f, outputSize.toFloat(), outputSize.toFloat()),
bgRadius, bgRadius, bgPaint
)
// 黑色模块画笔
val modulePaint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply {
color = 0xFF000000.toInt()
style = android.graphics.Paint.Style.FILL
}
// 居中偏移
val offsetX = padding + (maxOf(qrCols, qrRows) - qrCols) * moduleSize / 2
val offsetY = padding + (maxOf(qrCols, qrRows) - qrRows) * moduleSize / 2
// 模块圆角和缝隙
val cornerRadius = moduleSize * 0.35f
val inset = moduleSize * 0.1f
// 逐个绘制圆角模块
for (row in 0 until qrRows) {
for (col in 0 until qrCols) {
if (matrix[left + col, top + row]) {
val px = offsetX + col * moduleSize + inset
val py = offsetY + row * moduleSize + inset
val size = moduleSize - inset * 2
canvas.drawRoundRect(
android.graphics.RectF(px, py, px + size, py + size),
cornerRadius, cornerRadius, modulePaint
)
}
}
}
return bitmap
} }
/** /**