以 QR 方块为中心的问题

Issue centering a QR square

我正在编写一个 Android 测试应用程序以查看如何处理 QR 码。 这是主要 activity (MainActivity.kt) 的代码,下面是一个问题。

package me.software.myTestApp

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter


class MainActivity : AppCompatActivity() {
    private var constraintLayout: ConstraintLayout? = null
    private var deviceFrameID:Int = 0
    var squareImg: ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        constraintLayout = findViewById(R.id.main)
        deviceFrameID = resources.getIdentifier("main","id",packageName)

        squareImg = ImageView(this)
        squareImg?.id = View.generateViewId()

        val constrSet = ConstraintSet()

        constraintLayout?.addView(squareImg)

        val bm = getQrCodeBitmap(
            "HELLI-HELLO",
            "JIko98TEST"
        )

        val mDrawable: Drawable = BitmapDrawable(resources, bm)
        squareImg?.background = mDrawable
        val params = squareImg?.layoutParams
        params?.width = 512
        params?.height = 512
        squareImg?.layoutParams = params

        constrSet.connect(squareImg!!.id, ConstraintSet.LEFT, deviceFrameID, ConstraintSet.LEFT)
        constrSet.connect(squareImg!!.id, ConstraintSet.RIGHT, deviceFrameID, ConstraintSet.RIGHT)
        constrSet.connect(squareImg!!.id, ConstraintSet.TOP, deviceFrameID, ConstraintSet.TOP)
        constrSet.connect(squareImg!!.id, ConstraintSet.BOTTOM, deviceFrameID, ConstraintSet.BOTTOM)

        constrSet.applyTo(constraintLayout)
    }

    fun getQrCodeBitmap(ssid: String, password: String): Bitmap {/*From the net*/
        val size = 512 //pixels
        val qrCodeContent = "WIFI:S:$ssid;T:WPA;P:$password;;"
        val hints = hashMapOf<EncodeHintType, Int>().also { it[EncodeHintType.MARGIN] = 1 } // Make the     QR code buffer border narrower
        val bits = QRCodeWriter().encode(qrCodeContent, BarcodeFormat.QR_CODE, size, size)
        return Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565).also {
            for (x in 0 until size) {
                for (y in 0 until size) {
                    it.setPixel(x, y, if (bits[x, y]) Color.BLACK else Color.WHITE)
                }
            }
        }
    }
}

通过使用此代码,我希望看到一个水平和垂直居中的完美方形 QR 码。但我看到的是一个相当居中的二维码,而不是正方形,它被拉伸成显示器的形状。我需要修改什么才能得到完美的正方形?

ConstraintSet 不知道 QR 码视图,因此任何限制它的尝试都会失败,因为 ConstraintSet[=19 不知道它=]. ConstraintSet 由对 ConstraintSet#clone(ConstraintLayout)

的调用填充

将代码更改为以下内容:

constraintLayout?.addView(squareImg)

// The ConstraintLayout now contains all views, so the ConstraintSet
// can get a handle to those views for later connections.
val constrSet = ConstraintSet()
constrSet.clone(constraintLayout)

...