Skip to content

Commit

Permalink
✨ supports displaying QR code (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Nov 19, 2023
1 parent fe5e97a commit f53981b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ kotlin {
implementation(compose.desktop.currentOs)
implementation("net.java.dev.jna:jna:5.13.0")
implementation("net.java.dev.jna:jna-platform:5.13.0")
implementation("com.google.zxing:core:3.5.2")
implementation("com.google.zxing:javase:3.5.2")
}
commonMain.dependencies {
implementation(compose.runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toComposeImageBitmap
import com.clipevery.clip.AbstractClipboard
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import java.awt.image.BufferedImage

@OptIn(ExperimentalResourceApi::class)
@Composable
fun ClipeveryApp(clipboard: AbstractClipboard, copyText: MutableState<String>) {
fun ClipeveryApp(clipboard: AbstractClipboard, copyText: MutableState<String>, generateQRCode: BufferedImage) {
MaterialTheme {
val pid: Long = ProcessHandle.current().pid()
var showImage by remember { mutableStateOf(false) }
Expand All @@ -44,7 +45,8 @@ fun ClipeveryApp(clipboard: AbstractClipboard, copyText: MutableState<String>) {
}
AnimatedVisibility(showImage) {
Image(
painterResource("compose-multiplatform.xml"),
generateQRCode.toComposeImageBitmap(),
// painterResource("compose-multiplatform.xml"),
null
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.clipevery.utils

import com.google.zxing.BarcodeFormat
import com.google.zxing.qrcode.QRCodeWriter
import java.awt.Color
import java.awt.image.BufferedImage

fun generateQRCode(text: String, width: Int, height: Int): BufferedImage {
val writer = QRCodeWriter()
val bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height)
val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
for (x in 0 until width) {
for (y in 0 until height) {
image.setRGB(x, y, if (bitMatrix.get(x, y)) Color.BLACK.rgb else Color.WHITE.rgb)
}
}
return image
}
10 changes: 8 additions & 2 deletions composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import com.clipevery.ClipeveryApp
import com.clipevery.getClipboard
import com.clipevery.utils.generateQRCode
import org.jetbrains.skia.Image
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable
import java.awt.image.BufferedImage
import java.io.InputStream
import java.util.function.Consumer
import kotlin.system.exitProcess
Expand Down Expand Up @@ -44,12 +46,14 @@ fun main() = application {
}
)

val generateQRCode: BufferedImage = generateQRCode("Hello World!", 200, 200)

Window(onCloseRequest = ::exitApplication,
title = "Clipevery",
icon = imageBitmap,
undecorated = true,
resizable = false) {
ClipeveryApp(clipboard, copyText)
ClipeveryApp(clipboard, copyText, generateQRCode)
}
}

Expand Down Expand Up @@ -77,5 +81,7 @@ fun AppDesktopPreview() {
}
}
val clipboard = getClipboard(consumer)
ClipeveryApp(clipboard, copyText)
val generateQRCode: BufferedImage = generateQRCode("Hello World!", 200, 200)

ClipeveryApp(clipboard, copyText, generateQRCode)
}

0 comments on commit f53981b

Please sign in to comment.