@@ -23,36 +23,19 @@ object QRCodeDecoder {
23
23
* create qrcode using zxing
24
24
*/
25
25
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()
43
31
}
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()
54
36
}
55
37
38
+
56
39
/* *
57
40
* 同步解析本地图片二维码。该方法是耗时操作,请在子线程中调用。
58
41
*
@@ -70,40 +53,24 @@ object QRCodeDecoder {
70
53
* @return 返回二维码图片里的内容 或 null
71
54
*/
72
55
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
90
66
} 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
95
68
}
96
- return result.text
97
- } catch (e: Exception ) {
98
- e.printStackTrace()
99
- }
100
- } catch (e: Exception ) {
101
- e.printStackTrace()
69
+ }.getOrNull()
102
70
}
103
-
104
- return null
105
71
}
106
72
73
+
107
74
/* *
108
75
* 将本地图片文件转换成可解码二维码的 Bitmap。为了避免图片太大,这里对图片进行了压缩。感谢 https://github.com/devilsen 提的 PR
109
76
*
@@ -149,6 +116,6 @@ object QRCodeDecoder {
149
116
)
150
117
HINTS [DecodeHintType .TRY_HARDER ] = BarcodeFormat .QR_CODE
151
118
HINTS [DecodeHintType .POSSIBLE_FORMATS ] = allFormats
152
- HINTS [DecodeHintType .CHARACTER_SET ] = " utf-8 "
119
+ HINTS [DecodeHintType .CHARACTER_SET ] = Charsets . UTF_8
153
120
}
154
121
}
0 commit comments