我正在尝试解码我自己编码的条形码,但我不能。 编码条形码读取另一个条形码扫描仪应用程序。 我尝试使用位矩阵和位图来转换 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)
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))
}