Considerably faster QR-code bitmap generation

Directly assign colours to a colour buffer then use that buffer to
generate the bitmap. This is much faster than using Bitmap.setPixel for
every pixel.

Signed-off-by: Graeme Power <gjpower@tcd.ie>
This commit is contained in:
Graeme Power 2020-10-31 16:07:23 +01:00
parent a639ac42c4
commit fd4bfaea1f
No known key found for this signature in database
GPG Key ID: 9368D9D3759C5C49
2 changed files with 9 additions and 5 deletions

View File

@ -17,6 +17,7 @@ Improvements 🙌:
- Prepare changelog for F-Droid (#2296) - Prepare changelog for F-Droid (#2296)
- Add graphic resources for F-Droid (#812, #2220) - Add graphic resources for F-Droid (#812, #2220)
- Highlight text in the body of the displayed result (#2200) - Highlight text in the body of the displayed result (#2200)
- Considerably faster QR-code bitmap generation (#2331)
Bugfix 🐛: Bugfix 🐛:
- Messages encrypted with no way to decrypt after SDK update from 0.18 to 1.0.0 (#2252) - Messages encrypted with no way to decrypt after SDK update from 0.18 to 1.0.0 (#2252)

View File

@ -34,12 +34,15 @@ fun String.toBitMatrix(size: Int): BitMatrix {
fun BitMatrix.toBitmap(@ColorInt backgroundColor: Int = Color.WHITE, fun BitMatrix.toBitmap(@ColorInt backgroundColor: Int = Color.WHITE,
@ColorInt foregroundColor: Int = Color.BLACK): Bitmap { @ColorInt foregroundColor: Int = Color.BLACK): Bitmap {
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val colorBuffer = IntArray(width*height)
for (x in 0 until width) { var rowOffset = 0
for (y in 0 until height) { for (y in 0 until height) {
bmp.setPixel(x, y, if (get(x, y)) foregroundColor else backgroundColor) for (x in 0 until width) {
val arrayIndex = x + rowOffset
colorBuffer[arrayIndex] = if (get(x, y)) foregroundColor else backgroundColor
} }
rowOffset += width
} }
return bmp return Bitmap.createBitmap(colorBuffer, width, height, Bitmap.Config.ARGB_8888)
} }