Skip to content

Instantly share code, notes, and snippets.

@Jerry0022
Last active March 5, 2022 00:20
Show Gist options
  • Select an option

  • Save Jerry0022/f786075b682a0d3dee90 to your computer and use it in GitHub Desktop.

Select an option

Save Jerry0022/f786075b682a0d3dee90 to your computer and use it in GitHub Desktop.
How to generate a QR Code from a String and show it in JavaFX
/**
* Created by Jerry on 24.09.2015.
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
// QRCode Gradle dependency: compile 'net.glxn:qrgen:1.4'
public class QRCodeGenerator extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// GENERATE QR CODE
ByteArrayOutputStream out = QRCode.from("LT Jerry0022").to(ImageType.PNG).withSize(200, 200).stream();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// SHOW QR CODE
BorderPane root = new BorderPane();
Image image = new Image(in);
ImageView view = new ImageView(image);
view.setStyle("-fx-stroke-width: 2; -fx-stroke: blue");
root.setCenter(view);
Scene scene = new Scene(root, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
@muhamedoufi
Copy link
Copy Markdown

how to save generated image in my project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment