Implemented QrCodeGenerator for Android

This commit is contained in:
dankito 2020-11-12 21:51:28 +01:00
parent d51ad29b3a
commit 807228f869
9 changed files with 115 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
@ -12,6 +13,8 @@ kotlin {
}
}
android()
ios {
binaries {
framework {
@ -62,6 +65,14 @@ kotlin {
}
}
val androidMain by getting {
dependencies {
implementation("com.google.zxing:core:3.3.0")
implementation("com.google.zxing:android-core:3.3.0")
implementation("com.google.zxing:android-integration:3.3.0")
}
}
val iosMain by getting
val iosTest by getting
@ -80,6 +91,29 @@ kotlin {
}
}
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
versionCode = 1
versionName = "1.0.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
lintOptions {
isCheckReleaseBuilds = false
//If you want to continue even if errors found use following line
isAbortOnError = false
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"

View File

@ -0,0 +1,29 @@
package net.codinux.banking.epcqrcode
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.Color
import com.google.zxing.BarcodeFormat
import com.google.zxing.qrcode.QRCodeWriter
import java.io.ByteArrayOutputStream
class QrCodeGenerator {
fun generateQrCode(informationToEncode: String, config: EncodeToQrCodeConfig = EncodeToQrCodeConfig()): ByteArray {
val bitMatrix = QRCodeWriter().encode(informationToEncode, BarcodeFormat.QR_CODE, config.width, config.height)
val bitmap = Bitmap.createBitmap(config.width, config.height, Bitmap.Config.RGB_565)
for (x in 0 until config.width) {
for (y in 0 until config.height) {
bitmap.setPixel(x, y, if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE)
}
}
val blob = ByteArrayOutputStream()
bitmap.compress(if (config.format == ImageFormat.JPEG) CompressFormat.JPEG else CompressFormat.PNG, 100 /* Ignored for PNGs */, blob)
return blob.toByteArray()
}
}

View File

@ -0,0 +1,9 @@
package net.codinux.banking.epcqrcode
open class EncodeToQrCodeConfig(
open val width: Int = 500,
open val height: Int = 500,
open val format: ImageFormat = ImageFormat.PNG,
open val encoding: EpcQrCodeEncoding = EpcQrCodeEncoding.Utf8
)

View File

@ -0,0 +1,7 @@
package net.codinux.banking.epcqrcode
enum class EpcQrCodeEncoding {
Utf8
}

View File

@ -0,0 +1,12 @@
package net.codinux.banking.epcqrcode
enum class ImageFormat {
// BMP, // .bmp is not supported on Android
JPEG,
PNG
}

View File

@ -7,4 +7,8 @@ class MppTest {
return "Na hallo, da sieh mal an"
}
fun getTestEpcQrCodeContent(): String {
return "BCD\n002\n1\nSCT\nRIEKDEMMBRV\nRieka\nDE11720512109876543210\nEUR1234.56\nCHAR\n\nDanke von codinux"
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="net.codinux.banking.epcqrcode">
</manifest>

View File

@ -1,5 +1,6 @@
package net.codinux.banking.epcqrcode
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
@ -15,6 +16,9 @@ class MainActivity : AppCompatActivity() {
super.onPostCreate(savedInstanceState)
txtMessage.text = MppTest().showMessage()
val imageBytes = QrCodeGenerator().generateQrCode(MppTest().getTestEpcQrCodeContent())
imgGeneratedQrCode.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size))
}
}

View File

@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
@ -17,4 +18,11 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/imgGeneratedQrCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
/>
</LinearLayout>