首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于qr扫描的组合zxing

用于qr扫描的组合zxing
EN

Stack Overflow用户
提问于 2021-10-02 10:42:15
回答 1查看 956关注 0票数 0

我想要实现qr扫描仪使用zxing和撰写。我试了很多次,但都失败了。我如何实现它呢?

这个密码对我没用。

代码语言:javascript
复制
@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 },
    )
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-02 11:06:50

从合成互操作文档

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

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

但主要的问题,为什么它不工作,对你来说,是你没有启动相机。您应该调用resume()来这样做。因此,最终代码可以如下所示:

代码语言:javascript
复制
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
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69416133

复制
相关文章

相似问题

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