From 807228f86929f6c5416ad40038d22aabd11766d5 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 12 Nov 2020 21:51:28 +0100 Subject: [PATCH] Implemented QrCodeGenerator for Android --- EpcQrCode/build.gradle.kts | 34 +++++++++++++++++++ .../banking/epcqrcode/QrCodeGenerator.kt | 29 ++++++++++++++++ .../banking/epcqrcode/EncodeToQrCodeConfig.kt | 9 +++++ .../banking/epcqrcode/EpcQrCodeEncoding.kt | 7 ++++ .../codinux/banking/epcqrcode/ImageFormat.kt | 12 +++++++ .../net/codinux/banking/epcqrcode/MppTest.kt | 4 +++ EpcQrCode/src/main/AndroidManifest.xml | 6 ++++ .../codinux/banking/epcqrcode/MainActivity.kt | 4 +++ .../src/main/res/layout/activity_main.xml | 12 +++++-- 9 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt create mode 100644 EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt create mode 100644 EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeEncoding.kt create mode 100644 EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt create mode 100644 EpcQrCode/src/main/AndroidManifest.xml diff --git a/EpcQrCode/build.gradle.kts b/EpcQrCode/build.gradle.kts index ab73da9..24e8475 100644 --- a/EpcQrCode/build.gradle.kts +++ b/EpcQrCode/build.gradle.kts @@ -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" diff --git a/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt b/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt new file mode 100644 index 0000000..2bbe6a4 --- /dev/null +++ b/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt @@ -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() + } + +} \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt new file mode 100644 index 0000000..627e049 --- /dev/null +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt @@ -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 +) \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeEncoding.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeEncoding.kt new file mode 100644 index 0000000..63e8539 --- /dev/null +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeEncoding.kt @@ -0,0 +1,7 @@ +package net.codinux.banking.epcqrcode + +enum class EpcQrCodeEncoding { + + Utf8 + +} \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt new file mode 100644 index 0000000..c59672e --- /dev/null +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt @@ -0,0 +1,12 @@ +package net.codinux.banking.epcqrcode + + +enum class ImageFormat { + +// BMP, // .bmp is not supported on Android + + JPEG, + + PNG + +} \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/MppTest.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/MppTest.kt index 57f9847..9425ee7 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/MppTest.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/MppTest.kt @@ -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" + } + } \ No newline at end of file diff --git a/EpcQrCode/src/main/AndroidManifest.xml b/EpcQrCode/src/main/AndroidManifest.xml new file mode 100644 index 0000000..4b154e5 --- /dev/null +++ b/EpcQrCode/src/main/AndroidManifest.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt index fcbe824..87e2516 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt @@ -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)) } } diff --git a/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml b/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml index 58f23f3..6cf4216 100644 --- a/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml +++ b/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml @@ -1,8 +1,9 @@ - @@ -17,4 +18,11 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> - \ No newline at end of file + + + \ No newline at end of file