Instantly share code, notes, and snippets.
Last active
June 13, 2023 10:54
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save alexanderankin/13c707a4d4729e36da8f30165e4cce22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.zxing.BarcodeFormat; | |
| import com.google.zxing.client.j2se.MatrixToImageWriter; | |
| import com.google.zxing.common.BitMatrix; | |
| import com.google.zxing.qrcode.QRCodeWriter; | |
| import lombok.SneakyThrows; | |
| import reactor.core.publisher.Mono; | |
| import reactor.netty.DisposableServer; | |
| import reactor.netty.http.server.HttpServer; | |
| import java.io.ByteArrayOutputStream; | |
| import java.util.Arrays; | |
| import java.util.Map; | |
| import java.util.stream.Collectors; | |
| public class QrCodeSharer { | |
| @SneakyThrows | |
| public static void main(String[] args) { | |
| DisposableServer disposableServer = HttpServer.create() | |
| .route(rb -> { | |
| rb.get("/", (req, res) -> res.sendRedirect("/index.html")); | |
| rb.get("/index.html", (req, res) -> res.sendString(Mono.just(""" | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>Hi</title> | |
| </head> | |
| <body> | |
| <div> | |
| <form id=form action=POST> | |
| <button id="button" type=submit>Generate</button> | |
| <input id="input" type=text /> | |
| </form> | |
| </div> | |
| <div><img id="img"></img></div> | |
| <script> | |
| window.addEventListener('DOMContentLoaded', () => { | |
| window.form.addEventListener('submit', async (e) => { | |
| e.preventDefault(); | |
| var response = await fetch('/image.png?message=' + window.input.value); | |
| var blob = await response.blob(); | |
| window.img.src = URL.createObjectURL(blob); | |
| }); | |
| var url = new URL(window.location); | |
| if (url.searchParams.get('message')) | |
| window.input.value = url.searchParams.get('message'); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |
| """))); | |
| rb.get("/image.png", (req, res) -> { | |
| // todo use real parser | |
| String q = req.uri().contains("?") ? req.uri().substring(req.uri().indexOf('?') + 1) : ""; | |
| Map<String, String> params = Arrays.stream(q.split("&")) | |
| .map(s -> Arrays.asList(s.split("="))) | |
| .collect(Collectors.toMap(e -> e.get(0), e -> e.get(1))); | |
| String message = params.get("message"); | |
| if (message == null) | |
| return res.status(400).send(); | |
| var ha = req.hostAddress(); | |
| if (ha == null) return res.status(400).send(); | |
| String contents = "%s://%s:%d/index.html?message=%s".formatted(req.scheme(), | |
| ha.getHostString(), | |
| ha.getPort(), | |
| message); | |
| return res.header("content-type", "image/png") | |
| .sendByteArray(Mono.just(generate(contents))); | |
| }); | |
| }) | |
| .port(8080) | |
| .bindNow(); | |
| disposableServer.onDispose().block(); | |
| } | |
| @SneakyThrows | |
| private static byte[] generate(String input) { | |
| QRCodeWriter qrCodeWriter = new QRCodeWriter(); | |
| BitMatrix matrix = qrCodeWriter.encode(input, BarcodeFormat.QR_CODE, 320, 320); | |
| ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
| String formatName = "png"; | |
| MatrixToImageWriter.writeToStream(matrix, formatName, byteArrayOutputStream); | |
| return byteArrayOutputStream.toByteArray(); | |
| } | |
| } | |
| /* | |
| // build.gradle: | |
| plugins { | |
| id 'java' | |
| id 'application' | |
| } | |
| repositories.mavenCentral() | |
| application.mainClass.set 'QrCodeGenLL' | |
| dependencies { | |
| implementation('com.google.zxing:core:3.5.1') | |
| implementation('com.google.zxing:javase:3.5.1') | |
| implementation('io.projectreactor.netty:reactor-netty:1.0.24') | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment