Android 根据传入的内容生成二维码

/**
     * 生成简单二维码
     *
     * @param content                字符串内容
     * @param width                  二维码宽度
     * @param height                 二维码高度
     * @param character_set          编码方式(一般使用UTF-8)
     * @param error_correction_level 容错率 L:7% M:15% Q:25% H:35%
     * @param margin                 空白边距(二维码与边框的空白区域)
     * @param color_black            黑色色块
     * @param color_white            白色色块
     * @return BitMap
     */
    fun createQRCodeBitmap(content: String?, width: Int, height: Int,
                           character_set: String?, error_correction_level: String?,
                           margin: String?, color_black: Int, color_white: Int): Bitmap? {
        // 字符串内容判空
        if (TextUtils.isEmpty(content)) {
            return null
        }
        // 宽和高>=0
        return if (width < 0 || height < 0) {
            null
        } else try {
            /** 1.设置二维码相关配置  */
            val hints: Hashtable<EncodeHintType, String?> = Hashtable()
            // 字符转码格式设置
            if (!TextUtils.isEmpty(character_set)) {
                hints.put(EncodeHintType.CHARACTER_SET, character_set)
            }
            // 容错率设置
            if (!TextUtils.isEmpty(error_correction_level)) {
                hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level)
            }
            // 空白边距设置
            if (!TextUtils.isEmpty(margin)) {
                hints.put(EncodeHintType.MARGIN, margin)
            }
            /** 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象  */
            val bitMatrix = QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints)

            /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值  */
            val pixels = IntArray(width * height)
            for (y in 0 until height) {
                for (x in 0 until width) {
                    //bitMatrix.get(x,y)方法返回true是黑色色块,false是白色色块
                    if (bitMatrix[x, y]) {
                        pixels[y * width + x] = color_black //黑色色块像素设置
                    } else {
                        pixels[y * width + x] = color_white // 白色色块像素设置
                    }
                }
            }
            /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,并返回Bitmap对象  */
            val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
            bitmap
        } catch (e: WriterException) {
            e.printStackTrace()
            null
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成二维码可以使用第三方库,比如 `qrcodejs`。具体实现步骤如下: 1. 在 HTML 页面中引入 `qrcodejs` 库。 ```html <script src="https://cdn.staticfile.org/qrcodejs/1.0.0/qrcode.min.js"></script> ``` 2. 在 JavaScript 中获取订单编号,并将其作为参数传入 `qrcodejs` 库的构造函数中,创建二维码。 ```javascript var orderNo = '20210801001'; // 订单编号 var qrcode = new QRCode(document.getElementById('qrcode'), { text: orderNo, width: 256, height: 256, colorDark: '#000000', colorLight: '#ffffff', correctLevel: QRCode.CorrectLevel.H }); ``` 其中,`document.getElementById('qrcode')` 是指定二维码容器的 DOM 元素,`text` 参数是订单编号,`width` 和 `height` 分别是二维码的宽度和高度,`colorDark` 和 `colorLight` 分别是二维码的前景色和背景色,`correctLevel` 是纠错等级,取值包括 `QRCode.CorrectLevel.L`、`QRCode.CorrectLevel.M`、`QRCode.CorrectLevel.Q` 和 `QRCode.CorrectLevel.H`,其中 `H` 级别的纠错能力最强,但是二维码密度也会相应降低。 3. 将生成的二维码插入到 HTML 页面中。 ```html <div id="qrcode"></div> ``` 完整的代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>生成二维码</title> <script src="https://cdn.staticfile.org/qrcodejs/1.0.0/qrcode.min.js"></script> </head> <body> <div id="qrcode"></div> <script> var orderNo = '20210801001'; // 订单编号 var qrcode = new QRCode(document.getElementById('qrcode'), { text: orderNo, width: 256, height: 256, colorDark: '#000000', colorLight: '#ffffff', correctLevel: QRCode.CorrectLevel.H }); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值