Zxing核心:3.5.2编码条码解码错误 -> NotFoundException

问题描述 投票:0回答:1

我正在尝试解码我自己编码的条形码,但我不能。 编码条形码读取另一个条形码扫描仪应用程序。 我尝试使用位矩阵和位图来转换 BinaryBitmap 但总是返回空结果

是的,如果不解码,有时可能会返回 null,但是

如何增强我的编码代码以进行纯解码?

错误是

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)  Caused by: com.google.zxing.NotFoundException


编码器

class BarcodeWriter: Writer { override fun encode( contents: String?, format: BarcodeFormat?, width: Int, height: Int ): BitMatrix { return MultiFormatWriter().encode(contents, format, width, height) } override fun encode( contents: String?, format: BarcodeFormat?, width: Int, height: Int, hints: MutableMap<EncodeHintType, *>? ): BitMatrix { return MultiFormatWriter().encode(contents, format, width, height, hints) } }
解码器

class BarcodeReader : Reader { override fun decode(image: BinaryBitmap?): Result { return MultiFormatReader().decode(image) } override fun decode(image: BinaryBitmap?, hints: MutableMap<DecodeHintType, *>?): Result { return MultiFormatReader().decode(image, hints) } override fun reset() { // } }
转换器

fun bitMatrixToBmp(bitMatrix: BitMatrix, xColor: Int?, yColor: Int?): Bitmap { val height: Int = bitMatrix.height val width: Int = bitMatrix.width val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565) for (x in 0 until width) { for (y in 0 until height) { bmp.setPixel(x, y, if (bitMatrix.get(x, y)) xColor?:Color.BLACK else yColor?:Color.WHITE) } } return bmp } fun bmpToBinaryBitmap(bitmap: Bitmap) : BinaryBitmap { val intArray = IntArray(bitmap.width * bitmap.height) val source = RGBLuminanceSource(bitmap.width, bitmap.height, intArray) return BinaryBitmap(HybridBinarizer(source)) } fun bitMatrixToBinary(bitMatrix: BitMatrix): BinaryBitmap { val intArray = IntArray(bitMatrix.width * bitMatrix.height) val source = RGBLuminanceSource(bitMatrix.width, bitMatrix.height, intArray) return BinaryBitmap(HybridBinarizer(source)) }
条形码编码;

val hints = hashMapOf( EncodeHintType.CHARACTER_SET to "utf-8" ) BarcodeWriter().encode( contents = "URL:${urlFormModel.url}", format = BarcodeFormat.QR_CODE, width = 100, height = 100, hints = hints )
条形码解码码;

val decodeHints = hashMapOf( DecodeHintType.CHARACTER_SET to "utf-8", //DecodeHintType.PURE_BARCODE to true, //DecodeHintType.TRY_HARDER to true ) val result = BarcodeReader().decode(testBarcode.bitMatrix!!.toBinaryMap(), decodeHints)
    
android kotlin zxing
1个回答
0
投票
我解决了我的问题...

intArray 已初始化,但从未填充位图中的像素值。这意味着 RGBLuminanceSource 实际上并未接收图像数据。

fun bmpToBinaryBitmap(bitmap: Bitmap): BinaryBitmap { val width = bitmap.width val height = bitmap.height val intArray = IntArray(width * height) bitmap.getPixels(intArray, 0, width, 0, 0, width, height) val source = RGBLuminanceSource(width, height, intArray) return BinaryBitmap(HybridBinarizer(source)) }
    
© www.soinside.com 2019 - 2024. All rights reserved.