From b3fd0a006855410409be6693d573460a403450ea Mon Sep 17 00:00:00 2001 From: Rein van der Woerd Date: Tue, 7 Apr 2020 23:08:37 +0200 Subject: [PATCH] improve orx-rabbit-control such that writing the QR code to a file is no longer necessary * orx-rabbit-control: writing the QR code to a file is no longer necessary * Smaller QR-code image with MagnifyingFilter.NEAREST --- .../src/main/kotlin/RabbitControlServer.kt | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/orx-rabbit-control/src/main/kotlin/RabbitControlServer.kt b/orx-rabbit-control/src/main/kotlin/RabbitControlServer.kt index 293cb829..1993e28c 100644 --- a/orx-rabbit-control/src/main/kotlin/RabbitControlServer.kt +++ b/orx-rabbit-control/src/main/kotlin/RabbitControlServer.kt @@ -1,12 +1,9 @@ import com.google.zxing.BarcodeFormat -import com.google.zxing.client.j2se.MatrixToImageWriter import com.google.zxing.qrcode.QRCodeWriter import org.openrndr.Extension import org.openrndr.Program import org.openrndr.color.ColorRGBa -import org.openrndr.draw.ColorBufferProxy -import org.openrndr.draw.Drawer -import org.openrndr.draw.isolated +import org.openrndr.draw.* import org.openrndr.extra.compositor.* import org.openrndr.extra.fx.blend.Darken import org.openrndr.extra.parameters.Parameter @@ -14,7 +11,6 @@ import org.openrndr.extra.parameters.ParameterType import org.openrndr.extra.parameters.listParameters import org.openrndr.extras.imageFit.FitMethod import org.openrndr.extras.imageFit.imageFit -import org.openrndr.internal.colorBufferLoader import org.openrndr.math.Vector2 import org.openrndr.math.Vector3 import org.openrndr.math.Vector4 @@ -24,11 +20,8 @@ import org.rabbitcontrol.rcp.model.interfaces.IParameter import org.rabbitcontrol.rcp.model.parameter.* import org.rabbitcontrol.rcp.transport.websocket.server.WebsocketServerTransporterNetty import java.awt.Color -import java.io.File import java.net.InetSocketAddress import java.net.Socket -import java.nio.file.FileSystems -import java.nio.file.Path import kotlin.reflect.KMutableProperty1 @@ -38,8 +31,7 @@ class RabbitControlServer(private val showQRUntilClientConnects: Boolean = true, private var parameterMap = mutableMapOf>() - private var qrCodeImageProxy: ColorBufferProxy? = null - private var qrImagePath: Path? = null + private var qrCodeImage: ColorBuffer? = null private var qrOverlayComposition: Composite? = null @@ -71,7 +63,7 @@ class RabbitControlServer(private val showQRUntilClientConnects: Boolean = true, val ip = socket.localAddress.toString().replace("/", "") val clientUrlWithHash = "https://rabbitcontrol.github.io/client/#$ip:$port" - qrCodeImageProxy = getQRCodeImageProxy(barcodeText = clientUrlWithHash) + qrCodeImage = getQRCodeImage(barcodeText = clientUrlWithHash) println("RabbitControl Web Client: $clientUrlWithHash") /** @@ -139,7 +131,7 @@ class RabbitControlServer(private val showQRUntilClientConnects: Boolean = true, } draw { - qrCodeImageProxy!!.colorBuffer?.let { + qrCodeImage?.let { program.drawer.imageFit(it, program.width / 4.0,program.height / 4.0, program.width * .5, program.height * .5, 0.0,0.0, FitMethod.Contain) } } @@ -221,17 +213,24 @@ class RabbitControlServer(private val showQRUntilClientConnects: Boolean = true, override fun shutdown(program: Program) { transporter.dispose() - // Delete the temporary file - File(qrImagePath!!.toUri()).delete() } - // FIXME is it possible to avoid the file entirely? - private fun getQRCodeImageProxy(barcodeText: String): ColorBufferProxy { + private fun getQRCodeImage(barcodeText: String): ColorBuffer { val qrCodeWriter = QRCodeWriter() - val bitMatrix = qrCodeWriter.encode(barcodeText, BarcodeFormat.QR_CODE, 500, 500) - qrImagePath = FileSystems.getDefault().getPath("./qr.JPG") - MatrixToImageWriter.writeToPath(bitMatrix, "JPG", qrImagePath) - return colorBufferLoader.loadFromUrl(qrImagePath!!.toUri().toURL().toString()) + val bitMatrix = qrCodeWriter.encode(barcodeText, BarcodeFormat.QR_CODE, 30, 30) + val cb = colorBuffer(bitMatrix.width, bitMatrix.height) + cb.filterMag = MagnifyingFilter.NEAREST + val shad = cb.shadow + + + for (y in 0 until bitMatrix.width) { + for (x in 0 until bitMatrix.height) { + shad[x, y] = if (bitMatrix[x, y]) ColorRGBa.BLACK else ColorRGBa.WHITE + } + } + + shad.upload() + return cb } override fun afterDraw(drawer: Drawer, program: Program) {