如何在Android的QR码中间设置徽标 [英] How to to set logo into middle of QR Code in android

查看:72
本文介绍了如何在Android的QR码中间设置徽标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用zing库生成QR码,并且已经使用URL,文本等生成了简单的QR码.但是我的问题是,我如何才能在Qr Code的中间生成带有公司徽标的QR Code.

I am using the zing library for generating the QR code and I have generated the simple QR Code using URL,Text etc. but my question is that how can i generate the QR Code with company logo in middle of Qr Code.

我在堆栈溢出中发现了很多答案,但是没有一个可以解决我的问题.

I have found so many answer in stack overflow but non of them can solve my problem.

推荐答案

我已经为我的项目生成了(科特琳回答)

I already generated for my project (Kotlin answer)

    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函数

mergeBitmaps function

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
}

这篇关于如何在Android的QR码中间设置徽标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆