2018-10-24 03:03:46 +02:00
|
|
|
package de.vanitasvitae.omemoqrgenerator;
|
|
|
|
|
2018-11-08 09:19:23 +01:00
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Graphics2D;
|
2018-10-24 03:03:46 +02:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
|
|
import com.google.zxing.WriterException;
|
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
2018-11-03 17:49:06 +01:00
|
|
|
import javafx.collections.ListChangeListener;
|
2018-10-24 03:03:46 +02:00
|
|
|
import javafx.embed.swing.SwingFXUtils;
|
|
|
|
import javafx.fxml.FXML;
|
2018-11-03 17:49:06 +01:00
|
|
|
import javafx.scene.control.ListView;
|
2018-10-24 03:03:46 +02:00
|
|
|
import javafx.scene.image.ImageView;
|
|
|
|
|
|
|
|
public class QrDisplayController {
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private ImageView qr_view;
|
|
|
|
|
|
|
|
@FXML
|
2018-11-03 17:49:06 +01:00
|
|
|
private ListView listView;
|
2018-10-30 00:44:29 +01:00
|
|
|
|
|
|
|
@FXML
|
|
|
|
private ListViewController listViewController;
|
2018-10-24 03:03:46 +02:00
|
|
|
|
2018-11-03 17:49:06 +01:00
|
|
|
private Repository repository = Repository.getInstance();
|
2018-10-24 03:03:46 +02:00
|
|
|
|
2018-11-03 17:49:06 +01:00
|
|
|
@FXML
|
|
|
|
public void initialize() {
|
2018-10-30 00:44:29 +01:00
|
|
|
drawQRCode();
|
2018-11-03 17:49:06 +01:00
|
|
|
repository.getIdentities().addListener((ListChangeListener<OmemoIdentity>) change -> drawQRCode());
|
2018-10-24 03:03:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:44:29 +01:00
|
|
|
public void drawQRCode() {
|
2018-11-03 17:49:06 +01:00
|
|
|
System.out.println("Draw!");
|
|
|
|
Repository repository = Repository.getInstance();
|
|
|
|
|
|
|
|
int width = 360, height = 360;
|
2018-10-24 03:03:46 +02:00
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
|
|
|
2018-11-03 17:49:06 +01:00
|
|
|
String content = "xmpp:" + repository.getJid().toString();
|
2018-10-24 03:03:46 +02:00
|
|
|
|
2018-11-03 17:49:06 +01:00
|
|
|
Iterator<OmemoIdentity> iterator = repository.getIdentities().iterator();
|
2018-10-24 03:03:46 +02:00
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
2018-10-30 00:44:29 +01:00
|
|
|
OmemoIdentity first = iterator.next();
|
|
|
|
if (first.getEnabled()) {
|
|
|
|
content += "?omemo-sid-" + first.getDevice().getDeviceId() + "=" + first.getFingerprint().toString();
|
|
|
|
break;
|
|
|
|
}
|
2018-10-24 03:03:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:44:29 +01:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
OmemoIdentity next = iterator.next();
|
|
|
|
if (next.getEnabled()) {
|
|
|
|
content += ";omemo-sid-" + next.getDevice().getDeviceId() + "=" + next.getFingerprint().toString();
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 03:03:46 +02:00
|
|
|
|
|
|
|
BufferedImage image;
|
|
|
|
|
|
|
|
try {
|
|
|
|
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);
|
|
|
|
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
image.createGraphics();
|
|
|
|
|
|
|
|
Graphics2D graphics = (Graphics2D) image.getGraphics();
|
|
|
|
graphics.setColor(Color.WHITE);
|
|
|
|
graphics.fillRect(0, 0, width, height);
|
|
|
|
graphics.setColor(Color.BLACK);
|
|
|
|
|
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
for (int j = 0; j < width; j++) {
|
|
|
|
if (byteMatrix.get(i, j)) {
|
|
|
|
graphics.fillRect(i, j, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Success...");
|
|
|
|
|
|
|
|
} catch (WriterException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qr_view.setImage(SwingFXUtils.toFXImage(image, null));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|