【发布时间】:2020-07-23 08:36:00
【问题描述】:
我正在尝试在我的应用程序中实现二维码+条形码扫描功能,但我只能扫描二维码。
我使用 ZXing 作为扫描库,使用 CameraX 作为相机功能。为了分析 CameraX 捕获的图像,我使用自定义 ImageAnalysis.Analyzer 类来检测和解码 QR 码 + 条形码(这不起作用)。
这是我的分析器:
class MyCodeImageAnalyzer(
private val onCodeDetected: (code: Result) -> Unit
) : ImageAnalysis.Analyzer {
private val yuvFormats = mutableListOf(YUV_420_888)
init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
yuvFormats.addAll(listOf(YUV_422_888, YUV_444_888))
}
}
private val reader = MultiFormatReader()
override fun analyze(image: ImageProxy) {
// We are using YUV format because, ImageProxy internally uses ImageReader to get the image
// by default ImageReader uses YUV format unless changed.
if (image.format !in yuvFormats) {
Timber.e("QRCodeAnalyzer Expected YUV, now = ${image.format}")
return
}
val data = image.planes[0].buffer.toByteArray()
val source = PlanarYUVLuminanceSource(
data,
image.width,
image.height,
0,
0,
image.width,
image.height,
false
)
val binaryBitmap = BinaryBitmap(HybridBinarizer(source))
try {
// Whenever reader fails to detect a QR code in image
// it throws NotFoundException
val result = reader.decode(binaryBitmap)
onCodeDetected(result)
} catch (e: NotFoundException) {
e.printStackTrace()
}
image.close()
}
}
我使用 MultiFormatReader,它考虑了包括 QR 码和条形码在内的多种格式。 我认为问题出在 yuvFormats 列表中,但我不知道要在其中添加什么。
【问题讨论】:
-
如何配置ZXing?还有,框架的尺寸是多少?
-
我认为这些信息不会对您有所帮助。关于 ZXing,我在这个类中使用了 MultiFormatReader。
-
我以前曾与 ZXing 合作过,这些信息真的很有帮助。根据配置和帧大小,检测可以是快/慢或好/坏。
-
对二维码的检测速度非常快,但正如我所说的条形码无法识别。
-
那我建议你配置ZXing只搜索你需要的代码,而不是默认的实现,可能对你来说太慢了。还要确认您尝试读取的条形码格式正确。