代码之家  ›  专栏  ›  技术社区  ›  algorix

如何显示android网络配置中的“共享Wi-Fi”二维码?

  •  0
  • algorix  · 技术社区  · 2 年前

    如何以编程方式(Kotlin或Java)显示当前连接网络的二维码“共享Wi-Fi”?

    Network details screen.

    我能做的最接近的事情就是显示网络设置:

    startActivity( Intent(Settings.ACTION_WIFI_SETTINGS) )

    也许有一些“行动”可以使用“意向”进入“共享Wi-Fi”屏幕?

    0 回复  |  直到 2 年前
        1
  •  1
  •   Jumaer Ahamed    2 年前

    com.google.zxing:core------这个库会被使用,也许我们可以使用默认的方式,但这个时间过程会有点复杂。。 >**下面的代码我们可以根据wifi名称和密码生成二维码,,如果需要,甚至我们可以在QR中间使用徽标 **

    /** *这个类可以用作公共类来生成任何qr。。 **/

    对象QrUtils{

    /**
     * generate wifi qr with this method ..
     * @param ssid is the exact name of network to generate qr ..
     * @param encryption is the network type encryption
     * @param password is the network pass
     * @param middleLogoBitmap is anything you want to set
     * @param context the fragmentContext ..
     * @param isShowMiddleIcon default true ---  to set icon ... can set false if required ..
     * @return [Bitmap] is the final result of QR to show
     */
    
    fun generateWifiQrCode(ssid: String,
                           encryption : String,
                           password:String ,
                           middleLogoBitmap: Bitmap?,
                           context: Context , isShowMiddleIcon : Boolean? = true
    ): Bitmap {
        val logoBitmap =  middleLogoBitmap ?: getDefaultBitmap(context)
        val size = 1024 //pixels
        val qrCodeContent = "WIFI:S:$ssid;T:$encryption;P:$password;;"
        val hints = hashMapOf<EncodeHintType, Int>().also {
            it[EncodeHintType.MARGIN] = 0
    
        }
        // Make the QR code buffer border narrower
        val bits = QRCodeWriter()
            .encode(qrCodeContent, BarcodeFormat.QR_CODE, size, size,hints)
        val qrBitmap = 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)
                }
            }
        }
        // no icon ..
        if(isShowMiddleIcon == false){
            return  qrBitmap
        }
    
        // something wrong to found default ..
        if(logoBitmap == null){
            return  qrBitmap
        }
    
        // custom or default icon will set ..
        return generateQrWithLogo(qrBitmap,logoBitmap)
    }
    
    
    /**
     * This method will generating qr with logo
     * @param qrcode is the exact bitmap of data
     * @param bitmap is the logo to set in middle ..
     * @return [Bitmap] is the final result of updated QR to show ..
     */
    private fun generateQrWithLogo(qrcode: Bitmap, bitmap: Bitmap): Bitmap {
        val combined =
            Bitmap.createBitmap(qrcode.width, qrcode.height, qrcode.config)
        val canvas = Canvas(combined)
        val canvasWidth = canvas.width
        val canvasHeight = canvas.height
        canvas.drawBitmap(qrcode, Matrix(), null)
    
        val resizeLogo =
            bitmap.let { Bitmap.createScaledBitmap(it, canvasWidth / 5, canvasHeight / 5, true) }
        val centreX = (canvasWidth - resizeLogo.width) / 2
        val centreY = (canvasHeight - resizeLogo.height) / 2
        val targetBmp: Bitmap = resizeLogo.copy(Bitmap.Config.ARGB_8888, false)
        canvas.drawBitmap(targetBmp, centreX.toFloat(), centreY.toFloat(), null)
        return combined
    }
    
    
    /**
     * GET DEFAULT BITMAP ...  IF ANY
     * given drawable -- R.drawable.ic_default_beeda_placeholder_qr --- can be replaced
     * @param mContext
     * @return [Bitmap] is the default bitmap to show ..
     */
    
    @Suppress("NAME_SHADOWING")
    private fun getDefaultBitmap(mContext : Context): Bitmap? {
        val drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_default_beeda_placeholder_qr)!!
        return try {
            val bitmap: Bitmap = Bitmap.createBitmap(
                drawable.intrinsicWidth,
                drawable.intrinsicHeight,
                Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            drawable.setBounds(0, 0, canvas.width, canvas.height)
            drawable.draw(canvas)
            bitmap
        } catch (e: OutOfMemoryError) {
            // Handle the error
            null
        }
    }}
    
        2
  •  0
  •   Keval    2 年前

    与其直接打开二维码屏幕,不如尝试直接在应用程序中显示二维码,您可以使用以下库来实现此功能

    com.google.zxing:core:3.4.0

    该库用于创建二维码,要实现这样的二维码,您必须具有连接的wifi网络的SSID、加密类型和密码。

    fun generateWifiQRCode(ssid: String, password: String, encryptionType: String): Bitmap? {
        val wifiNetwork = "WIFI:T:$encryptionType;S:$ssid;P:$password;;"
        val multiFormatWriter = MultiFormatWriter()
        
        try {
            val bitMatrix: BitMatrix = multiFormatWriter.encode(wifiNetwork, BarcodeFormat.QR_CODE, 200, 200)
            val barcodeEncoder = BarcodeEncoder()
            return barcodeEncoder.createBitmap(bitMatrix)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }