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)
- Add graphic resources for F-Droid (#812, #2220)
- Highlight text in the body of the displayed result (#2200)
- Considerably faster QR-code bitmap generation (#2331)
Bugfix 🐛:
- 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,
@ColorInt foregroundColor: Int = Color.BLACK): Bitmap {
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
for (x in 0 until width) {
for (y in 0 until height) {
bmp.setPixel(x, y, if (get(x, y)) foregroundColor else backgroundColor)
val colorBuffer = IntArray(width*height)
var rowOffset = 0
for (y in 0 until height) {
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)
}