prints test qr-code to virtual printer

android-scanner
Laura Radaelli 2017-01-25 12:50:01 +02:00
parent e60e76f104
commit 0cf9390fa5
10 changed files with 176 additions and 269 deletions

View File

@ -1,220 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.select_candidate_by_picture.SelectCandidateByPictureController">
<children>
<GridPane>
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0">
<center>
<Label text="Organization Logo" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
<left>
<Label text="This is the Name of the Election" BorderPane.alignment="CENTER">
<font>
<Font size="16.0" />
</font>
<opaqueInsets>
<Insets left="300.0" />
</opaqueInsets>
</Label>
</left>
</BorderPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="193.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="416.0" minWidth="10.0" prefWidth="407.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<GridPane GridPane.rowIndex="2">
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2">
<right>
<Button mnemonicParsing="false" onMousePressed="#GetToBallotSummary" prefHeight="39.0" prefWidth="128.0" text="Next" BorderPane.alignment="CENTER">
<font>
<Font size="18.0" />
</font>
</Button>
</right>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0">
<left>
<Button mnemonicParsing="false" onMousePressed="#GetToSelectByName" text="Back" BorderPane.alignment="CENTER">
<font>
<Font size="18.0" />
</font>
</Button>
</left>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
<center>
<Label text="Page 2 / 5" BorderPane.alignment="CENTER">
<font>
<Font size="18.0" />
</font>
</Label>
</center>
</BorderPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="102.0" minHeight="0.0" prefHeight="53.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="214.0" minHeight="10.0" prefHeight="161.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<center>
<Label text="To select, touch/press one of the buttons below" BorderPane.alignment="CENTER">
<font>
<Font size="15.0" />
</font>
</Label>
</center>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0">
<center>
<Label text="Select Candidate" BorderPane.alignment="CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</center>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2">
<bottom>
<Label text="Name of Contest" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
</children>
</GridPane>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="60.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="469.0" minWidth="10.0" prefWidth="469.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="63.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="166.0" minHeight="10.0" prefHeight="166.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="41.0" minHeight="5.0" prefHeight="5.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="134.0" minHeight="10.0" prefHeight="134.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="76.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<BorderPane fx:id="picture_0" prefHeight="200.0" prefWidth="200.0" onMousePressed="#submitTheSelection">
<center>
<ImageView BorderPane.alignment="CENTER">
<image>
<Image url="@../pics/profile.png" />
</image>
</ImageView>
</center>
</BorderPane>
<BorderPane fx:id="picture_1" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" onMousePressed="#submitTheSelection">
<center>
<ImageView BorderPane.alignment="CENTER">
<image>
<Image url="@../pics/profile.png" />
</image>
</ImageView>
</center>
</BorderPane>
<BorderPane fx:id="picture_2" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" onMousePressed="#submitTheSelection">
<center>
<ImageView BorderPane.alignment="CENTER">
<image>
<Image url="@../pics/profile.png" />
</image>
</ImageView>
</center>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<top>
<Label fx:id="name0" text="Name" BorderPane.alignment="CENTER" >
<font>
<Font size="15.0" />
</font>
</Label>
</top>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<top>
<Label fx:id="name1" text="Name" BorderPane.alignment="CENTER">
<font>
<Font size="15.0" />
</font>
</Label>
</top>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
<top>
<Label fx:id="name2" text="Name" BorderPane.alignment="CENTER">
<font>
<Font size="15.0" />
</font>
</Label>
</top>
</BorderPane>
</children>
</GridPane>
</center>
</BorderPane>
</children>
</GridPane>
</children>
</GridPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="129.0" minHeight="0.0" prefHeight="31.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="326.0" minHeight="10.0" prefHeight="315.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="75.0" minHeight="10.0" prefHeight="35.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>

View File

@ -56,6 +56,7 @@ dependencies {
// Jar that creates barcodes
compile group: 'net.sourceforge.barbecue', name: 'barbecue', version: '1.5-beta1'
compile 'com.google.zxing:core:3.3.0'
// Google protobufs
compile 'com.google.protobuf:protobuf-java:3.+'

View File

@ -3,40 +3,65 @@ package meerkat.voting.gui.ui.controllersFX;
/**
* Created by Laura on 01/12/2017.
*/
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import meerkat.voting.gui.ui.CommandPend;
import meerkat.voting.gui.ui.uicommands.UICommand;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
/**
* Main controller class for the entire layout.
*/
public class PrinterController {
/** Holder of a switchable vista. */
/** Holder of vista. */
@FXML
private StackPane printerHolder;
private VBox vBox;
@FXML
private Text content;
/**
* Replaces the vista displayed in the vista holder with a new vista.
*
* @param node the vista node to be swapped in.
*/
public void setVista(Node node) {
printerHolder.getChildren().setAll(node);
}
public void setContent(String s) {
content.setText(s);
Text txt = new Text(s);
vBox.getChildren().clear();
vBox.getChildren().add(txt);
vBox.getScene().getWindow().sizeToScene();
}
public void addContent(String s) {
String old_content = content.getText();
content.setText(old_content+"\n"+s);
Text txt = new Text(s);
vBox.getChildren().add(txt);
vBox.getScene().getWindow().sizeToScene();
}
public void setContent(BufferedImage i) {
ImageView imageView = new ImageView();
Image image = SwingFXUtils.toFXImage(i, null);
imageView.setImage(image);
vBox.getChildren().clear();
vBox.getChildren().add(imageView);
vBox.getScene().getWindow().sizeToScene();
}
public void addContent(BufferedImage i) {
ImageView imageView = new ImageView();
Image image = SwingFXUtils.toFXImage(i, null);
imageView.setImage(image);
vBox.getChildren().add(imageView);
// String old_content = content.getText();
// content.setText(old_content+"\n"+s);
vBox.getScene().getWindow().sizeToScene();
}
}

View File

@ -9,6 +9,7 @@ import javafx.scene.Node;
import meerkat.voting.gui.ui.GraphicalUI;
import meerkat.voting.gui.ui.uicommands.UICommand;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
@ -145,6 +146,26 @@ public class VistaNavigator {
);
}
/**
* Prints the content into the printer application layout.
*
* @param content a String with the content that should be printed.
* @param mode if value 'a' means append, any other character will erase current content of printer
*/
public static void printFX(BufferedImage content, char mode) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (mode=='a') {
printerController.addContent(content);
} else {
printerController.setContent(content);
}
}
}
);
}
/**
* Loads the 'LOADING' vista
* vistaHolder pane of the main application layout.

View File

@ -1,6 +1,7 @@
package meerkat.voting.output;
import com.google.common.util.concurrent.FutureCallback;
import com.google.zxing.WriterException;
import meerkat.protobuf.Voting.BallotSecrets;
import meerkat.protobuf.Voting.PlaintextBallot;
import meerkat.protobuf.Voting.SignedEncryptedBallot;
@ -11,6 +12,7 @@ import meerkat.voting.output.outputcommands.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
/**

View File

@ -1,6 +1,7 @@
package meerkat.voting.output;
import com.google.protobuf.ByteString;
import com.google.zxing.WriterException;
import meerkat.protobuf.Crypto.EncryptionRandomness;
import meerkat.protobuf.Crypto.RandomnessGenerationProof;
import meerkat.protobuf.Voting.BallotSecrets;
@ -14,6 +15,9 @@ import meerkat.voting.output.outputcommands.CommitOutputCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* A toy OutputDevice class
* outputs everything simply to the System console
@ -35,31 +39,32 @@ public class FXWindowOutputDevice extends AsyncRunnableOutputDevice {
public void doCommitToBallot(CommitOutputCommand command) {
logger.debug("entered method doCommitToBallot");
PlaintextBallot plaintextBallot = command.getPlaintext();
long plaintextSerialNumber = plaintextBallot.getSerialNumber();
String toPrint = "";
ByteString channel = command.getChannelIdentifierByteString();
printPlainText(plaintextBallot, channel);
toPrint+="Commitment of Ballot #" + plaintextSerialNumber;
toPrint+="\n";
toPrint+="(channel): ";
toPrint+="\n";
toPrint+=bytesToString(command.getChannelIdentifierByteString());
toPrint+="\n";
toPrint+="(plaintext): ";
toPrint+="\n";
toPrint+=plaintextBallot;
SignedEncryptedBallot signedEncryptedBallot = command.getSignedEncryptedBallot();
long encryptedSerialNumber = signedEncryptedBallot.getEncryptedBallot().getSerialNumber();
toPrint+="Commitment of Ballot #" + encryptedSerialNumber + " (ciphertext):";
toPrint+="\n";
if (plaintextSerialNumber != encryptedSerialNumber) {
logger.error("plaintext and encryption serial numbers do not match!! plaintext# = " +
plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber);
String toQRCode;
// SignedEncryptedBallot signedEncryptedBallot = command.getSignedEncryptedBallot();
// long encryptedSerialNumber = signedEncryptedBallot.getEncryptedBallot().getSerialNumber();
// toPrint+="Commitment of Ballot #" + encryptedSerialNumber + " (ciphertext):";
// toPrint+="\n";
// if (plaintextSerialNumber != encryptedSerialNumber) {
// logger.error("plaintext and encryption serial numbers do not match!! plaintext# = " +
// plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber);
// }
// ByteString encryptedData = signedEncryptedBallot.getEncryptedBallot().getData().getData();
// toPrint+=bytesToString(encryptedData);
// toPrint+="\n";
// VistaNavigator.printFX(toPrint, 'w');
toQRCode = "test";
try {
printQRCode(toQRCode, 250);
command.getCallback().onSuccess(null);
} catch (IOException e) {
command.getCallback().onFailure(e);
} catch (WriterException e) {
command.getCallback().onFailure(e);
}
ByteString encryptedData = signedEncryptedBallot.getEncryptedBallot().getData().getData();
toPrint+=bytesToString(encryptedData);
toPrint+="\n";
VistaNavigator.printFX(toPrint, 'w');
command.getCallback().onSuccess(null);
}
@ -105,6 +110,26 @@ public class FXWindowOutputDevice extends AsyncRunnableOutputDevice {
command.getCallback().onSuccess(null);
}
private void printPlainText(PlaintextBallot plaintextBallot, ByteString channel) {
long plaintextSerialNumber = plaintextBallot.getSerialNumber();
String toPrint = "";
toPrint+="Commitment of Ballot #" + plaintextSerialNumber;
toPrint+="\n";
toPrint+="(channel): ";
toPrint+="\n";
toPrint+=bytesToString(channel);
toPrint+="\n";
toPrint+="(plaintext): ";
toPrint+="\n";
toPrint+=plaintextBallot;
VistaNavigator.printFX(toPrint, 'w');
}
private void printQRCode(String toQRCode, int size) throws IOException, WriterException {
BufferedImage image = null;
image = GenerateQRCode.createQRImage(toQRCode, size);
VistaNavigator.printFX(image, 'a');
}
private void printEncryptionRandomness (EncryptionRandomness encryptionRandomness) {
String msg = "Encryption Randomness = ";

View File

@ -0,0 +1,64 @@
package meerkat.voting.output;
/**
* Created by Laura on 1/25/2017.
*/
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class GenerateQRCode {
/**
* @param args
* @throws WriterException
* @throws IOException
*/
public static void main(String[] args) throws WriterException, IOException {
String qrCodeText = "http://www.journaldev.com";
int size = 125;
createQRImage(qrCodeText, size);
System.out.println("DONE");
}
public static BufferedImage createQRImage(String qrCodeText, int size) throws WriterException, IOException {
// Create the ByteMatrix for the QR-Code that encodes the given String
Hashtable hintMap = new Hashtable();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText,
BarcodeFormat.QR_CODE, size, size, hintMap);
// Make the BufferedImage that are to hold the QRCode
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth,
BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// Paint and save the image using the ByteMatrix
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
return image;
// ImageIO.write(image, fileType, qrFile);
}
}

View File

@ -1,21 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?scenebuilder-stylesheet vista.css?>
<VBox prefHeight="472.0" prefWidth="354.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.PrinterController">
<VBox fx:id="vBox" prefHeight="200.0" prefWidth="350.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.PrinterController">
<children>
<ScrollPane prefHeight="595.0" prefWidth="354.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="830.0" prefWidth="337.0">
<children>
<Text fx:id="content" layoutX="23.0" layoutY="32.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Printer will print here..." wrappingWidth="256.13671875" />
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</VBox>