I'm try decode a barcode encoded by myself, but i cant. Encoded barcode reading another barcode scanner app. I try bitmatrix and bitmap to convert BinaryBitmap but always returning null result
Yes, sometimes can return null if not decode but how can i strong my encode code for pure decode?
Error is
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
Encoder
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)
}
}
Decoder
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() {
//
}
}
Conveters
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))
}
Barcode encode code;
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
)
Barcode decode code;
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)
I fixed my problem...
intArray is initialized but never populated with pixel values from the Bitmap. This means that the RGBLuminanceSource is not actually receiving the image data.
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))
}