Improve code

This commit is contained in:
Benoit Marty 2020-01-22 19:00:19 +01:00
parent c323b61575
commit d2fab91e9d
2 changed files with 12 additions and 7 deletions

View File

@ -18,27 +18,31 @@ package im.vector.riotx.core.qrcode
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Color import android.graphics.Color
import androidx.annotation.ColorInt
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
import com.google.zxing.common.BitMatrix import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter import com.google.zxing.qrcode.QRCodeWriter
fun String.toQrCode(width: Int = 200, fun String.toQrCode(width: Int,
height: Int = 200): Bitmap { height: Int,
@ColorInt backgroundColor: Int = Color.WHITE,
@ColorInt foregroundColor: Int = Color.BLACK): Bitmap {
return QRCodeWriter().encode( return QRCodeWriter().encode(
this, this,
BarcodeFormat.QR_CODE, BarcodeFormat.QR_CODE,
width, width,
height height
).toBitmap() ).toBitmap(backgroundColor, foregroundColor)
} }
fun BitMatrix.toBitmap(): Bitmap { fun BitMatrix.toBitmap(@ColorInt backgroundColor: Int = Color.WHITE,
@ColorInt foregroundColor: Int = Color.BLACK): Bitmap {
val height: Int = height val height: Int = height
val width: Int = width val width: Int = width
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565) val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
for (x in 0 until width) { for (x in 0 until width) {
for (y in 0 until height) { for (y in 0 until height) {
bmp.setPixel(x, y, if (get(x, y)) Color.BLACK else Color.WHITE) bmp.setPixel(x, y, if (get(x, y)) foregroundColor else backgroundColor)
} }
} }

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemVerificationBigImage" android:id="@+id/itemVerificationBigImage"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="180dp" android:layout_height="180dp"
android:layout_margin="8dp" android:layout_margin="8dp"
android:src="@drawable/ic_shield_trusted" /> tools:src="@drawable/ic_shield_trusted" />