This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QR code Generation Component (EXPOSUREAPP-5961) (#2689)
* added qr code generator component * linting * updated error handling * linting * updated exception handling in view model Co-authored-by: harambasicluka <64483219+harambasicluka@users.noreply.github.com>
- Loading branch information
1 parent
5cc2126
commit 2532abf
Showing
2 changed files
with
67 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...p/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QrCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package de.rki.coronawarnapp.ui.eventregistration.organizer.details | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.Color | ||
import com.google.zxing.BarcodeFormat | ||
import com.google.zxing.EncodeHintType | ||
import com.google.zxing.MultiFormatWriter | ||
import com.google.zxing.common.BitMatrix | ||
import dagger.Reusable | ||
import de.rki.coronawarnapp.appconfig.AppConfigProvider | ||
import de.rki.coronawarnapp.util.di.AppContext | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@Reusable | ||
class QrCodeGenerator @Inject constructor( | ||
private val appConfigProvider: AppConfigProvider, | ||
@AppContext private val context: Context, | ||
) { | ||
|
||
suspend fun createQrCode(input: String, size: Int = 1000): Bitmap? { | ||
|
||
val qrCodeErrorCorrectionLevel = appConfigProvider | ||
.getAppConfig() | ||
.presenceTracing | ||
.qrCodeErrorCorrectionLevel | ||
Timber.i("QrCodeErrorCorrectionLevel: $qrCodeErrorCorrectionLevel") | ||
val hints = mapOf( | ||
EncodeHintType.ERROR_CORRECTION to qrCodeErrorCorrectionLevel | ||
) | ||
|
||
return MultiFormatWriter().encode( | ||
input, | ||
BarcodeFormat.QR_CODE, | ||
size, | ||
size, | ||
hints | ||
).toBitmap() | ||
} | ||
|
||
private fun BitMatrix.toBitmap(): Bitmap { | ||
val bitmap = Bitmap.createBitmap( | ||
context.resources.displayMetrics, | ||
width, | ||
height, | ||
Bitmap.Config.ARGB_8888 | ||
) | ||
|
||
for (x in 0 until width) { | ||
for (y in 0 until height) { | ||
val color = if (get(x, y)) Color.BLACK else Color.WHITE | ||
bitmap.setPixel(x, y, color) | ||
} | ||
} | ||
return bitmap | ||
} | ||
} |