diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt index 0873626..3d9cc18 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -33,4 +33,76 @@ open class EpcQrCode( this.bitmap = bitmap.scaled(heightAndWidth, heightAndWidth, true) } + + open fun qrCodeAsString(): String { + val string = StringBuilder(bitmap.height * bitmap.width) + + for (y in 1 until bitmap.height) { + for (x in 1 until bitmap.width) { + if (isBitSet(x, y)) { + string.append("██") + } else { + string.append(" ") + } + } + + string.appendLine() + } + + return string.toString() + } + + open fun qrCodeAsSmallString(): String { + val string = StringBuilder(bitmap.height * bitmap.width) + + for (y in 1 until bitmap.height - 1 step 2) { + for (x in 1 until bitmap.width) { + val currentRowBit = bitmap.getInt(x, y) + val nextRowBit = bitmap.getInt(x, y + 1) + + if (currentRowBit == nextRowBit) { + if (isBitSet(currentRowBit)) { + string.append("█") + } else { + string.append(" ") + } + } else { + if (isBitSet(currentRowBit)) { + string.append("▀") + } else { + string.append("▄") + } + } + } + + string.appendLine() + } + + if (bitmap.height % 2 == 0) { // if last row is odd + val y = bitmap.height - 1 + for (x in 1 until bitmap.width) { + if (isBitSet(x, y)) { + string.append("▀") + } else { + string.append(" ") + } + } + + string.appendLine() + } + + return string.toString() + } + + protected open fun isBitSet(x: Int, y: Int): Boolean { + val qrCodeBit = bitmap.getInt(x, y) + + return isBitSet(qrCodeBit) + } + + protected open fun isBitSet(qrCodeBitColorCode: Int): Boolean { + // black = -1, white = -16777216 + return qrCodeBitColorCode < -1 + } + } \ No newline at end of file diff --git a/EpcQrCodeNativeApp/build.gradle.kts b/EpcQrCodeNativeApp/build.gradle.kts new file mode 100644 index 0000000..ddf7faf --- /dev/null +++ b/EpcQrCodeNativeApp/build.gradle.kts @@ -0,0 +1,37 @@ +plugins { + kotlin("multiplatform") +} + + +kotlin { + val hostOs = System.getProperty("os.name") + val isMingwX64 = hostOs.startsWith("Windows") + val nativeTarget = when { + hostOs == "Mac OS X" -> macosX64("native") + hostOs == "Linux" -> linuxX64("native") + isMingwX64 -> mingwX64("native") + else -> throw GradleException("Host OS is not supported in Kotlin/Native.") + } + + nativeTarget.apply { + binaries { + executable { + baseName = "EpcQrCode" + + entryPoint = "main" + } + } + } + + + sourceSets { + val commonMain by getting { + dependencies { + implementation(project(":EpcQrCode")) + } + } + + val nativeMain by getting + } + +} \ No newline at end of file diff --git a/EpcQrCodeNativeApp/src/nativeMain/kotlin/Main.kt b/EpcQrCodeNativeApp/src/nativeMain/kotlin/Main.kt new file mode 100644 index 0000000..42cf38a --- /dev/null +++ b/EpcQrCodeNativeApp/src/nativeMain/kotlin/Main.kt @@ -0,0 +1,15 @@ +import net.codinux.banking.epcqrcode.EpcQrCodeConfig +import net.codinux.banking.epcqrcode.NativeApp +import platform.posix.exit + + +fun main(args: Array) { + if (args.size < 3) { + println("Please enter recipient name, recipient IBAN, amount and an optional reference:\n./EpcQrCode.kexe \"Red cross\" BE72000000001616 \"50.00\" \"Urgency fund\"") + exit(0) + } + + val config = EpcQrCodeConfig(args[0], args[1], args[2], if (args.size > 3) args[3] else null) + + NativeApp().generateAndShowEpcQrCode(config) +} \ No newline at end of file diff --git a/EpcQrCodeNativeApp/src/nativeMain/kotlin/net.codinux.banking.epcqrcode/NativeApp.kt b/EpcQrCodeNativeApp/src/nativeMain/kotlin/net.codinux.banking.epcqrcode/NativeApp.kt new file mode 100644 index 0000000..78bb5bb --- /dev/null +++ b/EpcQrCodeNativeApp/src/nativeMain/kotlin/net.codinux.banking.epcqrcode/NativeApp.kt @@ -0,0 +1,12 @@ +package net.codinux.banking.epcqrcode + + +class NativeApp { + + fun generateAndShowEpcQrCode(config: EpcQrCodeConfig) { + val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(config, 40) + + println(epcQrCode.qrCodeAsSmallString()) + } + +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index dbf5b4a..9ee5222 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,21 +1,21 @@ pluginManagement { - repositories { - gradlePluginPortal() - google() - jcenter() - mavenCentral() - } - resolutionStrategy { - eachPlugin { - if (requested.id.namespace == "com.android" || requested.id.name == "kotlin-android-extensions") { - useModule("com.android.tools.build:gradle:4.0.2") - } - } + repositories { + gradlePluginPortal() + google() + jcenter() + mavenCentral() + } + resolutionStrategy { + eachPlugin { + if (requested.id.namespace == "com.android" || requested.id.name == "kotlin-android-extensions") { + useModule("com.android.tools.build:gradle:4.0.2") + } } + } - plugins { - id("io.quarkus") version "1.9.2.Final" // TODO: why doesn't he find quarkusVersion? - } + plugins { + id("io.quarkus") version "1.9.2.Final" // TODO: why doesn't he find quarkusVersion? + } } @@ -25,4 +25,5 @@ rootProject.name = "EpcQrCode" include(":EpcQrCode") include(":EpcQrCodeAndroidApp") include(":EpcQrCodeJavaFxApp") +include(":EpcQrCodeNativeApp") include(":EpcQrCodeRest")