diff --git a/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt b/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt index 2bbe6a4..3ae909f 100644 --- a/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt +++ b/EpcQrCode/src/androidMain/kotlin/net/codinux/banking/epcqrcode/QrCodeGenerator.kt @@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream class QrCodeGenerator { - fun generateQrCode(informationToEncode: String, config: EncodeToQrCodeConfig = EncodeToQrCodeConfig()): ByteArray { + fun generateQrCodeAsByteArray(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) @@ -26,4 +26,17 @@ class QrCodeGenerator { return blob.toByteArray() } + fun generateQrCode(informationToEncode: String, config: EncodeToQrCodeConfig = EncodeToQrCodeConfig()): Bitmap { + 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) + } + } + + return bitmap + } + } \ 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 index b91467d..69b8599 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EncodeToQrCodeConfig.kt @@ -2,8 +2,15 @@ package net.codinux.banking.epcqrcode open class EncodeToQrCodeConfig( - open val width: Int = 500, - open val height: Int = 500, + open val width: Int = DefaultWidth, + open val height: Int = DefaultHeight, open val format: ImageFormat = ImageFormat.PNG, open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8 -) \ No newline at end of file +) { + + companion object { + const val DefaultWidth = 350 + const val DefaultHeight = 350 + } + +} \ No newline at end of file diff --git a/EpcQrCodeAndroidApp/build.gradle.kts b/EpcQrCodeAndroidApp/build.gradle.kts index b9bd3ac..dd9252d 100644 --- a/EpcQrCodeAndroidApp/build.gradle.kts +++ b/EpcQrCodeAndroidApp/build.gradle.kts @@ -12,6 +12,8 @@ dependencies { implementation("androidx.core:core-ktx:1.2.0") implementation("androidx.appcompat:appcompat:1.1.0") implementation("androidx.constraintlayout:constraintlayout:1.1.3") + + implementation("com.google.android.material:material:1.2.1") } android { diff --git a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt new file mode 100644 index 0000000..a069078 --- /dev/null +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt @@ -0,0 +1,21 @@ +package net.codinux.banking.epcqrcode + +import android.content.Context +import android.os.IBinder +import android.view.View +import android.view.inputmethod.InputMethodManager +import com.google.android.material.textfield.TextInputEditText + + +fun Context.hideKeyboard(anyViewInHierarchy: View, flags: Int = 0) { + hideKeyboard(anyViewInHierarchy.windowToken, flags) +} + +fun Context.hideKeyboard(windowToken: IBinder, flags: Int = 0) { + val keyboard = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + keyboard.hideSoftInputFromWindow(windowToken, flags) +} + + +val TextInputEditText.string: String + get() = this.text?.toString() ?: "" \ 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 7890cff..5a132c4 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt @@ -1,25 +1,50 @@ package net.codinux.banking.epcqrcode -import android.graphics.BitmapFactory +import android.graphics.Bitmap import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* +import kotlin.concurrent.thread class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + btnGenerateQrCode.setOnClickListener { generateQrCode() } } - override fun onPostCreate(savedInstanceState: Bundle?) { - super.onPostCreate(savedInstanceState) - txtMessage.text = MppTest().showMessage() + private fun generateQrCode() { + hideKeyboard(edtxtReference) - val qrCodeContent = EpcQrCodeCreator().generateAsString(CreatorParam("Mahatma Gandhi", "IN00123456789876543210", null, 1234.56, "Struggle for independence")) - val imageBytes = QrCodeGenerator().generateQrCode(qrCodeContent) - imgGeneratedQrCode.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)) + val param = CreatorParam(edtxtReceiver.string, edtxtIban.string, edtxtBic.string, map(edtxtAmount.string), edtxtReference.string) + + generateQrCodeAsync(param) { createdQrCode -> + imgGeneratedQrCode.setImageBitmap(createdQrCode) + } + } + + private fun generateQrCodeAsync(param: CreatorParam, done: (Bitmap) -> Unit) { + thread { + val density = resources.displayMetrics.density + val config = EncodeToQrCodeConfig((EncodeToQrCodeConfig.DefaultWidth * density).toInt(), (EncodeToQrCodeConfig.DefaultHeight * density).toInt()) + + val qrCodeContent = EpcQrCodeCreator().generateAsString(param) + + // TODO: this takes a very long time. Improve it + val generatedQrCode = QrCodeGenerator().generateQrCode(qrCodeContent, config) + + runOnUiThread { + done(generatedQrCode) + } + } + } + + private fun map(amount: String): Double? { + return amount.replace(',', '.').toDoubleOrNull() } } diff --git a/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml b/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml index 6cf4216..8146bee 100644 --- a/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml +++ b/EpcQrCodeAndroidApp/src/main/res/layout/activity_main.xml @@ -1,28 +1,126 @@ - + - + tools:context=".MainActivity"> - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +