首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android的二维码中间设置logo

如何在android的二维码中间设置logo
EN

Stack Overflow用户
提问于 2015-11-13 14:14:23
回答 1查看 695关注 0票数 3

我正在使用zing库生成二维码,我已经使用URL、文本等生成了简单的二维码,但我的问题是,如何在二维码中间生成带有公司徽标的二维码。

我在堆栈溢出中找到了很多答案,但没有一个能解决我的问题。

EN

回答 1

Stack Overflow用户

发布于 2018-10-08 14:16:54

我已经为我的项目生成了(Kotlin回答)

代码语言:javascript
复制
    fun CreateQRCode(qrCodeData: String) {

        try {

            val hintMap = HashMap<EncodeHintType, ErrorCorrectionLevel>()

            val width = 250
            val height = 250

            val smallestDimension = if (width < height) width else height

            hintMap[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H

            val qrCodeWriter = QRCodeWriter()
            val bitMatrix = qrCodeWriter.encode(
                    qrCodeData,
                    BarcodeFormat.QR_CODE,
                    smallestDimension, smallestDimension) // width x height

            val pixels = IntArray(smallestDimension * smallestDimension)
            // All are 0, or black, by default
            for (y in 0 until height) {
                val offset = y * smallestDimension
                for (x in 0 until smallestDimension) {
                    //pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
                    pixels[offset + x] = if (bitMatrix.get(x, y))
                        ResourcesCompat.getColor(resources, R.color.colorPrimary, null)
                    else
                        Color.WHITE
                }
            }
//            var image = DataMatrixWriter.toBufferedImage(bitMatrix)
            // Load logo image
//              var overly = getOverly(LOGO);

            val bitmap = Bitmap.createBitmap(smallestDimension, smallestDimension, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, smallestDimension, 0, 0, smallestDimension, smallestDimension)
            //setting bitmap to image view

            val options = BitmapFactory.Options()
            options.outWidth = 250
            options.inJustDecodeBounds = false
            options.outHeight = 250

            var overlay = BitmapFactory.decodeResource(getResources(), R.mipmap.sugary_qr_icon, options);
            overlay = Bitmap.createScaledBitmap(overlay, 50, 50, true)
            val merged = mergeBitmaps(overlay, bitmap)
            var _params = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
            _params.addRule(Gravity.CENTER_VERTICAL)
//            ivQrCode!!.setPadding(180, 180, 180, 180)
            ivQrCode!!.layoutParams = _params;

            ivQrCode!!.setImageBitmap(merged)
            //            return bitmap;
        } catch (er: Exception) {
            if (debug)
                er.printStackTrace()
        }
    }

mergeBitmaps函数

代码语言:javascript
复制
private fun mergeBitmaps(overlay: Bitmap, bitmap: Bitmap): Bitmap {

    val height = bitmap.height
    val width = bitmap.width

    val combined = Bitmap.createBitmap(width, height, bitmap.config)
    val canvas = Canvas(combined)
    val canvasWidth = canvas.width
    val canvasHeight = canvas.height

    canvas.drawBitmap(bitmap, Matrix(), null)

    Log.e("WIDTH", canvasWidth.toString() + " " + overlay.width)

    val centreX = (canvasWidth - overlay.width) / 2
    val centreY = (canvasHeight - overlay.height) / 2
    canvas.drawBitmap(overlay, centreX.toFloat(), centreY.toFloat(), null)
    val bmOverlay = Bitmap.createBitmap(overlay.width, overlay.height, overlay.config);
    var canvas = Canvas(bmOverlay);
    canvas.drawBitmap(overlay, Matrix(), null);
    canvas.drawBitmap(bitmap, 0f, 0f, null);

    return bmOverlay
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33686931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档