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 a7725e6..0873626 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -4,10 +4,33 @@ import com.soywiz.korim.bitmap.Bitmap32 open class EpcQrCode( - open val bitmap: Bitmap32 + bitmap: Bitmap32, + heightAndWidth: Int = DefaultHeightAndWidth ) { + companion object { + + const val DefaultHeightAndWidth = 500 + + } + + + open var bitmap: Bitmap32 = bitmap // declare before init() method, otherwise application will crash + protected set + open val bytes: ByteArray get() = bitmap.extractBytes() + + constructor(bitmap: Bitmap32) : this(bitmap, DefaultHeightAndWidth) // convenience constructor for languages that don't support default parameters + + init { + scale(heightAndWidth) + } + + + open fun scale(heightAndWidth: Int) { + this.bitmap = bitmap.scaled(heightAndWidth, heightAndWidth, true) + } + } \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt index 50282ee..65b344a 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt @@ -7,12 +7,16 @@ import com.soywiz.korim.qr.QRErrorCorrectLevel open class EpcQrCodeGenerator { - open fun generateEpcQrCode(param: EpcQrCodeConfig): EpcQrCode { + open fun generateEpcQrCode(config: EpcQrCodeConfig): EpcQrCode { + return generateEpcQrCode(config, EpcQrCode.DefaultHeightAndWidth) + } + + open fun generateEpcQrCode(config: EpcQrCodeConfig, heightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth): EpcQrCode { val qrCode = QR(correctLevel = QRErrorCorrectLevel.M) - val qrCodeBitmap = qrCode.msg(generateAsString(param)) + val qrCodeBitmap = qrCode.msg(generateAsString(config)) - return EpcQrCode(qrCodeBitmap) + return EpcQrCode(qrCodeBitmap, heightAndWidth) } open fun generate(param: EpcQrCodeConfig): EpcQrCodeValues { 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 b0f4038..0d1d492 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt @@ -42,9 +42,9 @@ class MainActivity : AppCompatActivity() { private fun generateQrCodeAsync(param: EpcQrCodeConfig, done: (Bitmap) -> Unit) { GlobalScope.launch(Dispatchers.IO) { val density = resources.displayMetrics.density - val config = EncodeToQrCodeConfig((EncodeToQrCodeConfig.DefaultWidth * density).toInt(), (EncodeToQrCodeConfig.DefaultHeight * density).toInt()) + val heightAndWidth = (350 * density).toInt() - val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(param) + val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(param, heightAndWidth) withContext(Dispatchers.Main) { done(epcQrCode.androidBitmap) diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt index 9cb000f..d9737fb 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt @@ -1,9 +1,7 @@ package net.codinux.banking.epcqrcode.rest import net.codinux.banking.epcqrcode.EpcQrCodeConfig -import net.codinux.banking.epcqrcode.EncodeToQrCodeConfig import net.codinux.banking.epcqrcode.EpcQrCodeGenerator -import net.codinux.banking.epcqrcode.QrCodeGenerator import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequestDto import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeResponseDto import org.slf4j.LoggerFactory @@ -24,15 +22,13 @@ class EpcQrCodeResource { protected val epcQrCodeGenerator = EpcQrCodeGenerator() - protected val qrCodeGenerator = QrCodeGenerator() - @POST @Produces(MediaType.APPLICATION_JSON) fun createEpcQrCode(requestDto: GenerateEpcQrCodeRequestDto): Response { try { - val qrCodeContent = epcQrCodeGenerator.generateAsString(mapToEpcQrCodeConfig(requestDto)) - val qrCodeBytes = qrCodeGenerator.generateQrCode(qrCodeContent, map(requestDto)) + val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(requestDto), requestDto.imageHeightAndWidth) + val qrCodeBytes = epcQrCode.bytes // TODO: convert to Base64 return Response.ok(GenerateEpcQrCodeResponseDto(qrCodeBytes)) .header("Access-Control-Allow-Origin", "*") @@ -40,7 +36,7 @@ class EpcQrCodeResource { } catch (e: Exception) { log.error("Could not create QR code for $requestDto", e) - return Response.serverError() + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.statusCode, e.localizedMessage) // TODO: it's almost always a bad idea to pass exception message to the outside .header("Access-Control-Allow-Origin", "*") .build() } @@ -57,13 +53,4 @@ class EpcQrCodeResource { ) } - private fun map(dto: GenerateEpcQrCodeRequestDto): EncodeToQrCodeConfig { - return EncodeToQrCodeConfig( - dto.imageWidth, - dto.imageHeight, - dto.imageFormat, - dto.encoding - ) - } - } \ No newline at end of file diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt index 14f264d..819a42f 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt @@ -1,5 +1,6 @@ package net.codinux.banking.epcqrcode.rest.dto +import net.codinux.banking.epcqrcode.EpcQrCode import net.codinux.banking.epcqrcode.EpcQrCodeCharacterSet import net.codinux.banking.epcqrcode.ImageFormat @@ -18,9 +19,7 @@ class GenerateEpcQrCodeRequestDto { var noteToUser: String? = null - var imageWidth: Int = 500 - - var imageHeight: Int = 500 + var imageHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth var imageFormat: ImageFormat = ImageFormat.PNG