Skip to content

Commit 4464903

Browse files
codewithtamimAnGgIt886
authored andcommitted
refactor(): QRCodeDecoder for readability and performance
- Improved the `createQRCode` function by replacing manual loops with Kotlin idioms and using `runCatching` for safer error handling. - Refactored `syncDecodeQRCode` to simplify control flow and avoid redundant null checks. - Enhanced error handling and logging by using `runCatching` and more concise exception handling.
1 parent 7bb4ebf commit 4464903

File tree

1 file changed

+24
-57
lines changed

1 file changed

+24
-57
lines changed

app/src/main/kotlin/com/neko/v2ray/util/QRCodeDecoder.kt

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,19 @@ object QRCodeDecoder {
2323
* create qrcode using zxing
2424
*/
2525
fun createQRCode(text: String, size: Int = 800): Bitmap? {
26-
try {
27-
val hints = HashMap<EncodeHintType, String>()
28-
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
29-
val bitMatrix = QRCodeWriter().encode(
30-
text,
31-
BarcodeFormat.QR_CODE, size, size, hints
32-
)
33-
val pixels = IntArray(size * size)
34-
for (y in 0 until size) {
35-
for (x in 0 until size) {
36-
if (bitMatrix.get(x, y)) {
37-
pixels[y * size + x] = 0xff000000.toInt()
38-
} else {
39-
pixels[y * size + x] = 0xffffffff.toInt()
40-
}
41-
42-
}
26+
return runCatching {
27+
val hints = mapOf(EncodeHintType.CHARACTER_SET to Charsets.UTF_8)
28+
val bitMatrix = QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints)
29+
val pixels = IntArray(size * size) { i ->
30+
if (bitMatrix.get(i % size, i / size)) 0xff000000.toInt() else 0xffffffff.toInt()
4331
}
44-
val bitmap = Bitmap.createBitmap(
45-
size, size,
46-
Bitmap.Config.ARGB_8888
47-
)
48-
bitmap.setPixels(pixels, 0, size, 0, 0, size, size)
49-
return bitmap
50-
} catch (e: Exception) {
51-
e.printStackTrace()
52-
return null
53-
}
32+
Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888).apply {
33+
setPixels(pixels, 0, size, 0, 0, size, size)
34+
}
35+
}.getOrNull()
5436
}
5537

38+
5639
/**
5740
* 同步解析本地图片二维码。该方法是耗时操作,请在子线程中调用。
5841
*
@@ -70,40 +53,24 @@ object QRCodeDecoder {
7053
* @return 返回二维码图片里的内容 或 null
7154
*/
7255
fun syncDecodeQRCode(bitmap: Bitmap?): String? {
73-
if (bitmap == null) {
74-
return null
75-
}
76-
var source: RGBLuminanceSource? = null
77-
try {
78-
val width = bitmap.width
79-
val height = bitmap.height
80-
val pixels = IntArray(width * height)
81-
bitmap.getPixels(pixels, 0, width, 0, 0, width, height)
82-
source = RGBLuminanceSource(width, height, pixels)
83-
val qrReader = QRCodeReader()
84-
try {
85-
val result = try {
86-
qrReader.decode(
87-
BinaryBitmap(GlobalHistogramBinarizer(source)),
88-
mapOf(DecodeHintType.TRY_HARDER to true)
89-
)
56+
return bitmap?.let {
57+
runCatching {
58+
val pixels = IntArray(it.width * it.height).also { array ->
59+
it.getPixels(array, 0, it.width, 0, 0, it.width, it.height)
60+
}
61+
val source = RGBLuminanceSource(it.width, it.height, pixels)
62+
val qrReader = QRCodeReader()
63+
64+
try {
65+
qrReader.decode(BinaryBitmap(GlobalHistogramBinarizer(source)), mapOf(DecodeHintType.TRY_HARDER to true)).text
9066
} catch (e: NotFoundException) {
91-
qrReader.decode(
92-
BinaryBitmap(GlobalHistogramBinarizer(source.invert())),
93-
mapOf(DecodeHintType.TRY_HARDER to true)
94-
)
67+
qrReader.decode(BinaryBitmap(GlobalHistogramBinarizer(source.invert())), mapOf(DecodeHintType.TRY_HARDER to true)).text
9568
}
96-
return result.text
97-
} catch (e: Exception) {
98-
e.printStackTrace()
99-
}
100-
} catch (e: Exception) {
101-
e.printStackTrace()
69+
}.getOrNull()
10270
}
103-
104-
return null
10571
}
10672

73+
10774
/**
10875
* 将本地图片文件转换成可解码二维码的 Bitmap。为了避免图片太大,这里对图片进行了压缩。感谢 https://github.com/devilsen 提的 PR
10976
*
@@ -149,6 +116,6 @@ object QRCodeDecoder {
149116
)
150117
HINTS[DecodeHintType.TRY_HARDER] = BarcodeFormat.QR_CODE
151118
HINTS[DecodeHintType.POSSIBLE_FORMATS] = allFormats
152-
HINTS[DecodeHintType.CHARACTER_SET] = "utf-8"
119+
HINTS[DecodeHintType.CHARACTER_SET] = Charsets.UTF_8
153120
}
154121
}

0 commit comments

Comments
 (0)