diff --git a/app/src/main/java/com/xiaoqu/watch/ui/bind/BindFragment.kt b/app/src/main/java/com/xiaoqu/watch/ui/bind/BindFragment.kt index bd838ab..9217392 100644 --- a/app/src/main/java/com/xiaoqu/watch/ui/bind/BindFragment.kt +++ b/app/src/main/java/com/xiaoqu/watch/ui/bind/BindFragment.kt @@ -86,77 +86,35 @@ class BindFragment : BaseFragment() { } } - /** - * 将 ZXing BitMatrix 转换为圆角风格二维码 Bitmap - * 每个模块(小方块)都是圆角矩形,白色背景带圆角 - */ + /** 将 ZXing BitMatrix 转换为 Bitmap,整体图片加圆角裁剪 */ private fun bitMatrixToBitmap(matrix: BitMatrix): Bitmap { - // 找到二维码实际内容边界(去掉 ZXing 空白边距) - val w = matrix.width - val h = matrix.height - var left = w; var top = h; var right = 0; var bottom = 0 - for (y in 0 until h) { - for (x in 0 until w) { - if (matrix[x, y]) { - if (x < left) left = x - if (x > right) right = x - if (y < top) top = y - if (y > bottom) bottom = y + val width = matrix.width + val height = matrix.height + // 生成标准二维码 Bitmap + val pixels = IntArray(width * height) + for (y in 0 until height) { + for (x in 0 until width) { + pixels[y * width + x] = if (matrix[x, y]) { + 0xFF000000.toInt() + } else { + 0xFFFFFFFF.toInt() } } } - val qrCols = right - left + 1 - val qrRows = bottom - top + 1 + val squareBitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888) - // 输出参数 - val moduleSize = 16f // 每个模块像素大小 - val padding = moduleSize * 2 // 内边距 - val outputSize = (maxOf(qrCols, qrRows) * moduleSize + padding * 2).toInt() + // 圆角裁剪(iPhone 风格,12% 圆角) + val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) + val canvas = android.graphics.Canvas(output) + 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) - 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 + return output } /**