【问题标题】:zxing with compose for qr scanzxing 与 compose 进行 qr 扫描
【发布时间】:2021-10-02 10:42:15
【问题描述】:

我想使用 zxing 和 compose 实现 qr 扫描仪。 我尝试了很多次,但都失败了。 我该如何实现它?

此代码对我不起作用。

@Composable
fun AdminClubMembershipScanScreen(navController: NavHostController) {
    val context = LocalContext.current
    var scanFlag by remember {
        mutableStateOf(false)
    }

    val compoundBarcodeView = remember {
        CompoundBarcodeView(context).apply {
            val capture = CaptureManager(context as Activity, this)
            capture.initializeFromIntent(context.intent, null)
            this.setStatusText("")
            capture.decode()
            this.decodeContinuous { result ->
                if(scanFlag){
                    return@decodeContinuous
                }
                scanFlag = true
                result.text?.let { barCodeOrQr->
                    //Do something and when you finish this something
                    //put scanFlag = false to scan another item
                    scanFlag = false
                }
                //If you don't put this scanFlag = false, it will never work again.
                //you can put a delay over 2 seconds and then scanFlag = false to prevent multiple scanning 
                
            }
        }
    }

    AndroidView(
        modifier = Modifier,
        factory = { compoundBarcodeView },
    )
}

【问题讨论】:

    标签: kotlin zxing android-jetpack-compose


    【解决方案1】:

    来自 Compose 互操作documentation

    注意:在 AndroidView viewBlock 中构造视图是最佳实践。不要持有或记住 AndroidView 之外的直接视图引用。

    相反,您应该在 factory 中创建视图 - 它还会为您提供上下文,因此您不需要 LocalContext

    但它对您不起作用的主要问题是您没有启动相机。您应该致电resume() 这样做。所以最终的代码可以是这样的:

    var scanFlag by remember {
        mutableStateOf(false)
    }
    
    AndroidView(
        factory = { context ->
            CompoundBarcodeView(context).apply {
                val capture = CaptureManager(context as Activity, this)
                capture.initializeFromIntent(context.intent, null)
                this.setStatusText("")
                capture.decode()
                this.decodeContinuous { result ->
                    if (scanFlag) {
                        return@decodeContinuous
                    }
                    println("scanFlag true")
                    scanFlag = true
                    result.text?.let { barCodeOrQr ->
                        println("$barCodeOrQr")
                        //Do something and when you finish this something
                        //put scanFlag = false to scan another item
                        scanFlag = false
                    }
                    //If you don't put this scanFlag = false, it will never work again.
                    //you can put a delay over 2 seconds and then scanFlag = false to prevent multiple scanning
                }
                this.resume()
            }
        },
        modifier = Modifier
    )
    

    【讨论】:

    • 感谢您的友好回复。它解决了我的问题。
    • @Kyu-HoHwang 不客气。由于这解决了您的问题,请使用投票计数器下的复选标记接受此答案 =)
    • 我希望能问一个问题。它在 kindle fire 上不起作用,但是,以前的代码正在处理它。它说,“java.lang.RuntimeException:无法连接到相机服务”“E/CameraInstance:无法配置相机”“E/WindowManager:应用程序试图使用不安全的 INPUT_FEATURE_NO_INPUT_CHANNEL 标志。忽略”
    • @Kyu-HoHwang 之前的代码是什么?这与现在的有什么不同?此代码是否适用于常规的 android 设备?首先检查您在清单中的权限是否相同,包括您是否遵循了zxing documentation 说明。如果这没有帮助,您应该问另一个问题,因为它似乎与这个问题无关。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多