implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.2.4'
引用的别人编译好的lib,解析本地二维码的时候缺获取不到,其实是读取到的图片格式不匹配,需要转换一下
fun decoderQR(filePath: String): String? {
var decodeFile = BitmapFactory.decodeFile(filePath)
val pixels = IntArray(decodeFile.width * decodeFile.height)
decodeFile.getPixels(pixels, 0, decodeFile.width, 0, 0, decodeFile.width, decodeFile.height)
val barcode = Image(decodeFile.width, decodeFile.height, "RGB4")
barcode.setData(pixels)
// // 指定二维码在图片中的区域,也可以不指定,识别全图。
// barcode.setCrop(200, 200, 640, 640)
//
var qrCodeString: String? = null
//
//
var mImageScanner = ImageScanner()
//
// mImageScanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
//
mImageScanner.setConfig(0, Config.X_DENSITY, 3)
mImageScanner.setConfig(0, Config.Y_DENSITY, 3)
val result = mImageScanner.scanImage(barcode.convert("Y800"))
// Toast.makeText(this, "result--------》》" + result, Toast.LENGTH_SHORT).show()
if (result != 0) {
val symSet = mImageScanner.results
for (sym in symSet)
qrCodeString = sym.data
}
return qrCodeString
}
网友评论