Show QR code into the console

This commit is contained in:
Andrea Cavalli 2021-09-27 22:48:52 +02:00
parent 1cba9f7e38
commit 88ca5ce8ac
6 changed files with 64 additions and 4 deletions

View File

@ -25,7 +25,7 @@
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>2.7.8.3</version>
<version>2.7.8.6</version>
</dependency>
<!-- TDLight natives -->

View File

@ -0,0 +1,38 @@
package it.tdlight.client;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Hashtable;
class QrCodeTerminal {
public static String getQr(String url) {
int width = 40;
int height = 40;
Hashtable<EncodeHintType, Object> qrParam = new Hashtable<>();
qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
qrParam.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, qrParam);
return toAscii(bitMatrix);
} catch (WriterException ex) {
throw new IllegalStateException("Can't encode QR code", ex);
}
}
public static String toAscii(BitMatrix bitMatrix) {
StringBuilder sb = new StringBuilder();
for (int rows = 0; rows < bitMatrix.getHeight(); rows++) {
for (int cols = 0; cols < bitMatrix.getWidth(); cols++) {
boolean x = bitMatrix.get(rows, cols);
sb.append(x ? " " : "██");
}
sb.append("\n");
}
return sb.toString();
}
}

View File

@ -62,8 +62,11 @@ final class ScannerClientInteraction implements ClientInteraction {
System.out.println(passwordMessage);
break;
case NOTIFY_LINK:
System.out.println("Please confirm this login link on another device: "
+ ((ParameterInfoNotifyLink) parameterInfo).getLink());
String link = ((ParameterInfoNotifyLink) parameterInfo).getLink();
System.out.println("Please confirm this login link on another device: " + link);
System.out.println();
System.out.println(QrCodeTerminal.getQr(link));
System.out.println();
return "";
case TERMS_OF_SERVICE:
TermsOfService tos = ((ParameterInfoTermsOfService) parameterInfo).getTermsOfService();

View File

@ -3,6 +3,7 @@ module tdlight.java {
requires org.reactivestreams;
requires org.slf4j;
requires it.unimi.dsi.fastutil;
requires com.google.zxing;
exports it.tdlight.tdlight;
exports it.tdlight.tdnative;
exports it.tdlight.tdlib;

View File

@ -68,6 +68,10 @@
<dependency>
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
@ -81,6 +85,11 @@
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -69,7 +69,11 @@
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
@ -81,6 +85,11 @@
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
</dependencyManagement>