https://kotlinlang.org logo
Title
t

Tung97 Hl

01/30/2023, 9:19 AM
Hi guys, do you know any library which can generate QR codes and support KMM?
r

r4phab

01/30/2023, 9:33 AM
If you don’t find a suitable library right now for this, I think the easiest would be to use Ktor with an online API such as : https://www.qr-code-generator.com/qr-code-api
t

Tung97 Hl

01/30/2023, 9:38 AM
Thanks for your suggestion. My case can not depend on third-party API
h

hfhbd

01/30/2023, 9:41 AM
m

Matt Nelson

01/30/2023, 11:19 AM
Yeah, Korim seems to be it. Here's a link to community resourced library list. Very helpful, if anyone was not aware of it. https://github.com/AAkira/Kotlin-Multiplatform-Libraries#image
k

kqr

01/30/2023, 12:32 PM
m

Matt Nelson

01/30/2023, 7:52 PM
Unfortunately there is no support for native yet. Looks pretty cool though
t

Tung97 Hl

01/31/2023, 3:06 AM
thanks for all suggestions. I’m trying them one by one and I will provide feedback later.
After investigating them all, I decided to not use multi-platform lib and implement it on the native side instead. I choose https://github.com/g0dkar/qrcode-kotlin for android and JVM and this is work well. For the iOS I’m looking for the one.
s

Swapnil Madavi

01/31/2023, 1:17 PM
For iOS you can use the CoreImage platform API
CIFilter.filterWithName("CIQRCodeGenerator")
to generate QR code.
t

Tung97 Hl

01/31/2023, 4:17 PM
Thank you 😎
b

Big Chungus

02/02/2023, 12:38 AM
Looks like adding whatever targets you're missing to https://github.com/g0dkar/qrcode-kotlin would be a relatively easy contribution since it's pure kotlin. Might be worth contributing to it instead of foregoing kmp approach for qrs
j

Javokhir Savriev

05/25/2023, 1:09 PM
@Tung97 Hl Can you share your solution for generating QR code on iOS?
t

Tung97 Hl

05/26/2023, 2:54 AM
actual fun genQrCode(value: String): ByteArray {
    val filter = CIFilter.QRCodeGenerator().apply {
        setValue(value, forKey = "inputMessage")
    }
    val outputImg = filter.outputImage ?: return byteArrayOf()
    val nsData = CIContext().JPEGRepresentationOfImage(
        outputImg,
        CGColorSpaceCreateDeviceRGB(),
        mapOf<Any?, String>()
    ) ?: return byteArrayOf()
    return ByteArray(nsData.length.toInt()).apply {
        usePinned {
            memcpy(it.addressOf(0), nsData.bytes, nsData.length)
        }
    }
}
I suggest you search solution for ojective-C, then convert it to kotlin by kotlin/native