我想要实现qr扫描仪使用zxing和撰写。我试了很多次,但都失败了。我如何实现它呢?
这个密码对我没用。
@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 },
)
}发布于 2021-10-02 11:06:50
从合成互操作文档
注意:在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
)https://stackoverflow.com/questions/69416133
复制相似问题